Unit testing/continuous integration with Simulink/Stateflow
Asked Answered
P

8

24

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)?
Penland answered 7/9, 2011 at 14:7 Comment(0)
M
9

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

Myriammyriameter answered 28/4, 2014 at 17:43 Comment(2)
yes, finally. R2013 starts to become a real platform for professional software developments, and R2014 has become even better. Also basic configuration management is a great addition. Back in 2011 this was seriously missing, glad they caught up.Penland
I am also trying to setup unit testing on simulink models, and use the matlab.unittest framework, using above example I get an tapFile with the testresults, but woul like to also export the coverage info, that I get from running cvsim from the test. Any ideas?Winograd
W
4

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.

Wherry answered 29/9, 2011 at 9:31 Comment(1)
Very true. Often Simulink models treated as monolithical blocks; also Mathworks yet my preferred approach is small, independently testable blocks which can be worked on in parallel by a team of people. Also, Mathworks tends to come up with yet another new tool (with independent license scheme, not seldomly required for all team members). Some of the best things in life come for free; I believe xUnit is one of them.....May be the Matlab/Simulink/Stateflow community is not yet ready for agile/test driven development. Thanks for introducing a bounty for this.Penland
P
3

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

Petrous answered 7/3, 2015 at 12:16 Comment(2)
Simulink Test looks really useful and addresses the testing part of the question, do you have any idea about integration with CI systems?Lange
You can run matlab scripts from command line. I use teamcity for orchestration and matlab scripts for running tests. Take a look at matlab.unittest.plugins.TAPPlugin for viewing your test report in CI frontend.Petrous
F
3

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.

See this reference for more information: https://www.mathworks.com/help/sltest/ug/run-test-files-using-matlab-unit-test.html?s_tid=gn_loc_drop

The documentation shows an example of producing TAP results using matlab.unittest.plugins.TAPPlugin but you can use XMLPlugin (https://www.mathworks.com/help/matlab/ref/matlab.unittest.plugins.xmlplugin-class.html) instead just as easily.

This should open up a better integration within just MATLAB environment even without CI in the picture with the ability to have MATLAB and Simulink Tests together in the same test suite and have them run together seamlessly. For example if you have a directory MYDIR with both native MATLAB unit tests and Simulink Tests, you can do something as simple as the follows to execute both kinds of tests in one shot:

results = runtests(MYDIR)

Fieldwork answered 14/1, 2017 at 14:36 Comment(0)
S
2

If your system is complex, you should decompose it using Model Reference and test each of these independently.

An other solution (more "old school") is to put your main blocks in a library and to create small models.

To test these submodels and especially those with a State Machine (Stateflow), the best is to create temporal test cases with the Signal builder block. You have a powerful function signalbuilder to interact with this block and load test cases. My method is to get for each case of each submodel an input file and an output file. Your outputs of the model are the "correct" output and the one from the blocks. The model is run with sim (no external inputs) and the 2 outputs are compared with a script the indicated which signal is different (and when).

You could use an existent system but I prefer to use my own to run each case (or some of them).

I don't have any public code for that but that's the way I use. I don't use a CIS so I can't answer the second part of your question.

Skiagraph answered 26/10, 2011 at 7:30 Comment(2)
This answer doesn't really help, but looks like it's going to get the bounty, unless someone comes up with something better.Wherry
I've done this, for the purpose of version management using SVN. For tests, I foresee big issues. Especially, since a small change will require compilation of a new "correct" output.Penland
L
2

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. You libraries and possibly models will need a wrapper to execute it as the other answerers have noted.

There are plenty of examples on the Mathworks site, unfortunately non of them run simulink models. I'd code an example for you, but I don't have ML2013b :-(

In order to initiate your tests from a CI (I use Jenkins) then you can call matlab to run a .m file that runs your test suite, this example cmd script will call Run_Tests.m from Matlab:

IF EXIST "C:\Program Files (x86)\MATLAB\R2013b\bin\win32\matlab.exe" (
    REM WinXP
    "C:\Program Files (x86)\MATLAB\R2013b\bin\win32\matlab.exe" -r "Run_Tests;exit" -logfile matlab.log
) ELSE (
    REM Win7
    "C:\Program Files\MATLAB\R2013b\bin\win32\matlab.exe" -r "Run_Tests;exit" -logfile matlab.log
)

Note that if a startup.m exists in the directory that you call Matlab from, then it'll be executed automatically beforeRun_Tests.m`.

Lange answered 14/4, 2014 at 16:22 Comment(1)
Quick correction. The MATLAB unit test framework was introduced in R2013a. Additional features to it however been added in R2013b and R2014a.Myriammyriameter
C
0

I think you are searching for something like EZTEST. It is intended for your special purpose: Test driven development for Simulink and Stateflow on unit level. For safety critical software, there is also a Safety Manual included, which describes what is covered regarding ISO 26262.

Edit: I am a developer of this software, so my opinion may be biased. But I am not involved in the marketing or sale of the product. I am just posting this, because I know that there are little to none unit test frameworks out there, meeting the questioner's needs (as the answers might suppose).

Testing units using SIL and PIL is also supported. Unfortunately I am not familiar with Hudson, so I cannot address this part of the question.

Cracknel answered 15/2, 2016 at 7:33 Comment(0)
I
0

I've seen different solutions to the problem of unit testing Simulink models. Simulink Verification & Validation which did not support xUnit concepts of test runners and test suites at the time of examination, TPT being overloaded with functionality, not easy to use and very hard in terms of changeability and maintainability.

Furthermore I've seen custom solutions with Matlab scripts and Excel tables which were lightweight but also difficult in terms of understandability and maintainability. I'd still not recommend using any of these approaches, at least not for unit testing.

In the end, we ended up using a C unit testing framework (CUnit) testing the generated code. While this definitely has the disadvantage that you have to generate code before testing, it also has a lot of advantages, like easy integration into CI systems, high flexibility of writing unit tests, fast execution of unit tests and last but not least refactoring capabilities in terms of switching from Simulink to another model-based environment or to hand-written code. Especially the last point should not be underestimated since I have seen many Simulink models that should have been hand-written modules in the first place. Nowadays, I'd recommend using GoogleTest instead of CUnit.

Incrassate answered 24/6, 2016 at 12:16 Comment(1)
Sure, you can unit test generated code. But I was looking for proof for model and arithmetic around it. Unit tests could be reused after code generation to proof that the generated code yields the same results. I've done this with integration tests with large datasets.Penland

© 2022 - 2024 — McMap. All rights reserved.