Question

According to "How to Write Tests That Share Common Set-Up Code" is it possible to:

function test_suite = testSetupExample
 initTestSuite;

function fh = setup
 fh = figure;

function teardown(fh)
 delete(fh);

function testColormapColumns(fh)
 assertEqual(size(get(fh, 'Colormap'), 2), 3);

function testPointer(fh)
 assertEqual(get(fh, 'Pointer'), 'arrow');

But I couldn't make it work with more parameters:

function test_suite = testSetupExample
 initTestSuite;

function [fh,fc] = setup
 fh = figure;
 fc = 2;
end

function teardown(fh,fc)
 delete(fh);

function testColormapColumns(fh,fc)
 assertEqual(size(get(fh, 'Colormap'), fc), 3);

function testPointer(fh,fc)
 assertEqual(get(fh, 'Pointer'), 'arrow');

When I runtests it says:

Input argument "fc" is undefined.

Why is that? I done something wrong or it is unsupported in the current version of Matlab xUnit? How to circumvent that?

PS: Actually my MATLAB requires each function to have an end. I didn't wrote them here to keep consistency with the manual examples.

Was it helpful?

Solution

Just use a struct:

function test_suite = testSetupExample
 initTestSuite;

function [fh] = setup
 fh.one = figure;
 fh.two = 2;
end

function teardown(fh)
 delete(fh.one);


function testColormapColumns(fh)
 assertEqual(size(get(fh.one, 'Colormap'), fc.two), 3);

etc.

OTHER TIPS

The framework only calls your setup function with a single output argument. If you want to pass more information out from your setup function, bundle everything into a struct.

Also, here are the rules for terminating a function with end. (These rules were introduced in MATLAB 7.0 in 2004 and have not changed since then.)

If any function in a file is terminated with an end, then all functions in that file must be terminated with an end.

Nested functions must always be terminated with an end. Therefore, if a file contains a nested function, then all functions in that file must be terminated with an end.

All functions and methods in classdef files must be terminated with an end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top