Perform Functional Testing and Analyze Test Coverage
Functional testing begins with building test cases based on requirements. These tests can cover key aspects of your design and verify that individual model components meet requirements. Test cases include inputs, expected outputs, and acceptance criteria.
By collecting individual test cases within test suites, you can run functional tests systematically. To check for regression, add baseline criteria to the test cases and test the model iteratively. Coverage measurement reflects the extent to which these tests have fully exercised the model. Coverage measurement also helps you to add tests and requirements to meet coverage targets.
Incrementally Increase Test Coverage Using Test Case Generation
This example shows a functional testing-based testing workflow for a cruise control model. You start with a model that has tests linked to an external requirements document, analyze the model for coverage in Simulink® Coverage™, incrementally increase coverage with Simulink Design Verifier™, and report the results.
Explore the Test Harness and the Model
Create a copy of the project in a working folder. At the command line, enter:
path = fullfile(matlabroot,'toolbox','shared','examples',... 'verification','src','cruise') run(fullfile(path,'slVerificationCruiseStart'))
Open the model and the test harness. At the command line, enter:
open_system simulinkCruiseAddReqExample sltest.harness.open('simulinkCruiseAddReqExample','SafetyTest_Harness1')
Load the test suite from Test Model Against Requirements and Report Results (Simulink Test) and open the Simulink Test Manager. At the command line, enter:
Open the test sequence block. The sequence tests that the system disengages when the:
Brake pedal is pressed
Speed exceeds a limit
Some test sequence steps are linked to requirements document
Measure Model Coverage
In the Simulink Test Manager, click the
To enable coverage collection for the test file, in the right page under Coverage Settings:
Select Record coverage for referenced models
Use Coverage filter filename to specify a coverage filter to use for the coverage analysis. The default setting honors the model configuration parameter settings. Leaving the field empty attaches no coverage filter.
Select Decision, Condition, and MCDC.
To run the tests, on the Test Manager toolstrip, click Run.
When the test finishes select the Results in the Test Manager. The aggregated coverage results show that the example model achieves 50% decision coverage, 41% condition coverage, and 25% MCDC coverage.
Generate Tests to Increase Model Coverage
Use Simulink Design Verifier to generate additional tests to increase model coverage. In Results and Artifacts, select the
slReqTeststest file and open the Aggregated Coverage Results section located in the right pane.
Right-click the test results and select Add Tests for Missing Coverage.
Under Harness, choose
Create a new harness.
Click OK to add tests to the test suite using Simulink Design Verifier. The model being tested must either be on the MATLAB® path or in the working folder.
On the Test Manager toolstrip, click Run to execute the updated test suite. The test results include coverage for the combined test case inputs, achieving increased model coverage.
Alternatively, you can create and use tests to increase coverage programmatically
sltest.testmanager.addTestsForMissingCoverage (Simulink Test) and
sltest.testmanager.TestOptions (Simulink Test).