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 how to perform requirements-based tests for a cruise control model. The tests link to a requirements document. You:
Run the tests.
Determine test coverage by using Simulink® Coverage™.
Increase coverage with additional tests generated by Simulink Design Verifier™.
Report the results.
Open the Test Harness and Model
Open the project:
openExample("shared_vnv/CruiseControlVerificationProjectExample"); pr = openProject("SimulinkVerificationCruise");
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.
pf = fullfile(pr.RootFolder,"tests","slReqTests.mldatx"); tf = sltest.testmanager.TestFile(pf); sltest.testmanager.view
Open the Test Sequence block. The sequence verifies system disengagement when either:
The brake pedal is pressed.
Speed exceeds a limit.
Measure Model Coverage
In the Simulink Test Manager, select the
To enable coverage collection, in the right page under Coverage Settings:
Select Record coverage for referenced models.
Specify a coverage filter by using Coverage filter filename.
Select Decision, Condition, and MCDC.
Click Run on the Test Manager toolstrip.
After the test completes, select Results. The test 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).
- Link to Requirements (Simulink Test)
- Assess Model Simulation Using verify Statements (Simulink Test)
- Compare Model Output to Baseline Data (Simulink Test)
- Generate Test Cases for Model Decision Coverage (Simulink Design Verifier)
- Increase Test Coverage for a Model (Simulink Test)