MATLAB xUnit Test Framework: How to Put Multiple Test Cases in One M-file
The Quick Start example showed how you can write a simple M-file to be a single test case. This example shows you how to put multiple test cases in one M-file.
Name your M-file beginning or ending with "test", like "testMyFunc". Start by putting the following two lines at the beginning of the file. It's important that the output variable name on line 1 be testSuite.
function testSuite = testMyFunc testSuite = buildFunctionHandleTestSuite(localfunctions);
Next, add subfunctions to the file. Each subfunction beginning or ending with "test" becomes an individual test case.
The directory example_subfunction_tests contains a test M-file containing subfunction test cases for the fliplr function.
cd example_subfunction_tests type testFliplr
function testSuite = testFliplr testSuite = buildFunctionHandleTestSuite(localfunctions); function testFliplrMatrix in = magic(3); assertEqual(fliplr(in), in(:, [3 2 1])); function testFliplrVector assertEqual(fliplr([1 4 10]), [10 4 1]);
As usual, run the test cases using runxunit:
runxunit
Test suite: C:\Users\psexton\Documents\GitHub\matlab-xunit-doctest\doc\example_subfunction_tests 20-Feb-2014 19:59:09 Starting test run with 2 test cases. .. PASSED in 0.013 seconds.