Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing/continuous integration with Simulink/Stateflow

How can I perform unit testing in Simulink, or preferably, Stateflow?

I'm a fan of agile software methods, including test driven development. I'm responsible for the development of safety critical control software and we're using Matlab/Simulink/Stateflow for the development of it. This toolset is selected because of the link with plant (hardware) models. (model-in-the-loop, hardware-in-the-loop)

I have found some links on Stackoverflow: Unit-testing framework for MATLAB: xunit, slunit and doctest.

  • Does anyone have experience in using those or different unit test frameworks?
  • How to link this to continuous integration systems (i.e. Hudson)?
like image 459
Adriaan Avatar asked Sep 07 '11 14:09

Adriaan


People also ask

How to integrate Simulink test with MATLAB unit testing framework?

R2016b introduces integration between Simulink Test and MATLAB Unit Testing framework. Tests created with Simulink Test using Test Manager (*.mldatx) are recognized by and can be run natively using the MATLAB Unit Test Runner and thus you can generate JUnit style XML test results or TAP test results facilitating Continuous integration workflows.

What happens when a Simulink test session fails?

If the test passes, the Jenkins server creates a merge request in GitLab. If a test fails, the Simulink Test results are saved and a bug issue in GitLab is created. This last step of saving the Simulink Test session is very important.

How can I generate JUnit Style XML test results using Simulink test?

Tests created with Simulink Test using Test Manager (*.mldatx) are recognized by and can be run natively using the MATLAB Unit Test Runner and thus you can generate JUnit style XML test results or TAP test results facilitating Continuous integration workflows.

Is it possible to use xUnit to test Simulink models?

Matlab (since 2013b) has built-in support for xUnit, in the form of the Unit Testing Framework . I haven't used it but since it's possible to run simulink from Matlab with sim () then this framework can be used to test your simulink models.


2 Answers

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

like image 173
Andy Campbell Avatar answered Dec 22 '22 00:12

Andy Campbell


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.

like image 41
Nzbuu Avatar answered Dec 21 '22 23:12

Nzbuu