Unit testing/continuous integration with Simulink/Stateflow

后端 未结 8 1323
没有蜡笔的小新
没有蜡笔的小新 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条回答
  • 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

    0 讨论(0)
  • 2021-02-01 05:37

    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`.

    0 讨论(0)
  • 2021-02-01 05:44

    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.

    0 讨论(0)
  • 2021-02-01 05:46

    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.

    0 讨论(0)
  • 2021-02-01 05:48

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题