Unit testing/continuous integration with Simulink/Stateflow

With 2015a Matlab introduces a new product name "Simulink Test". Perhaps that'll simplify this mess.

http://www.mathworks.com/products/simulink-test/features.html#manage-test-plans-and-test-execution


EDIT: This is now much easier and getting easier all the time with the Jenkins plugin for MATLAB

ORIGINAL ANSWER:

As Craig mentioned there is indeed a framework in MATLAB introduced in R2013a. Furthermore, this framework added a TAPPlugin in R2014a which outputs the Test Anything Protocal. Using that protocol you can set up your CI build with a TAPPlugin (eg. Jenkins, TeamCity) so that the CI system can fail the build if the tests fail.

Your CI build may look like a shell command to start MATLAB and run all your tests:

/your/path/to/matlab/bin/matlab -nosplash -nodisplay -nodesktop -r "runAllMyTests"

Then the runAllMyTests creates the suite to run and runs it with the tap output being redirected to a file. You'll need to tweak specifics here, but perhaps this can help you get started:

function runAllMyTests

import matlab.unittest.TestSuite;
import matlab.unittest.TestRunner;
import matlab.unittest.plugins.TAPPlugin;
import matlab.unittest.plugins.ToFile;

try
    % Create the suite and runner
    suite = TestSuite.fromPackage('packageThatContainsTests', 'IncludingSubpackages', true);
    runner = TestRunner.withTextOutput;
    
    % Add the TAPPlugin directed to a file in the Jenkins workspace
    tapFile = fullfile(getenv('WORKSPACE'), 'testResults.tap');
    runner.addPlugin(TAPPlugin.producingOriginalFormat(ToFile(tapFile)));

    runner.run(suite); 
catch e;
    disp(e.getReport);
    exit(1);
end;
exit force;

EDIT: I used this topic as the first two posts of a new developer oriented blog launched this year


Unit testing Simulink is not straightforward, unfortunately. Mathworks have the SystemTest. Alternatively, you can roll-your-own Simulink testing framework, which is the approach that we've followed and is not too difficult, but you may need to built test-harnesses programmatically.

In order to integrate with CI, you need to create a function/script that executes all the tests, then you can use the command-line parameters for MATLAB.exe to run a script on start-up. I'm not sure anyone has a good way of integrating the test reports with the CI software, though. Just look at the number of comments in Unit-testing framework for MATLAB.