Unit testing/continuous integration with Simulink/Stateflow

后端 未结 8 1349
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 05:22

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 d

8条回答
  •  星月不相逢
    2021-02-01 05:49

    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

提交回复
热议问题