MATLAB Examples

Increase Test Coverage for a Model

Increase test coverage by generating test inputs.

Contents

If your tests achieve incomplete model coverage, you can increase coverage by generating test inputs using Simulink® Design Verifier™. This example shows how to increase test coverage beyond an initial test case. You measure initial coverage of a test case. Then, you generate new test cases, add them to the test suite, run the tests, and review aggregate coverage.

This example uses Simulink Design Verifier and Simulink® Coverage™.

Workflow

This example tests a component of an autopilot system using a test harness. Timeseries data from the base workspace is mapped to root inports in the test harness. The test file is configured to collect coverage.

The example workflow is:

  1. Measure model coverage of the initial test case.
  2. Generate additional tests to achieve greater coverage.
  3. Add the new test cases to the test file.
  4. Run all test cases and review aggregate coverage.

Paths and Example Files

Set paths and filenames for the example.

filePath = fullfile(matlabroot,'toolbox','simulinktest','simulinktestdemos');
rollModel = 'RollAutopilotRevised';
testHarness = 'RollReference_LoggedDataTest';
testFile = 'RollRefTest.mldatx';

Run the Initial Test and Review Coverage

1. Ensure the working folder is writable.

2. Open the test file.

sltest.testmanager.view;
sltest.testmanager.load(fullfile(filePath,testFile));

3. Run the test. In the Test Browser, highlight the Logged Data and Coverage test suite. Click Run.

4. After the test completes, in the test results, expand the Coverage Results section. The test achieves partial coverage for the Roll Reference subsystem.

  • Decision coverage: 80%
  • Condition coverage: 70%
  • MCDC 25%

Generate Tests to Increase Model Coverage

Generate additional tests for missing coverage.

1. Below the coverage result, click Add Tests for Missing Coverage.

2. In the Add Tests for Missing Coverage dialog box, set these options:

  • Harness: RollReference_LoggedDataTest. This maps the new test inputs to the existing test harness.
  • Test Case: Create a new test case. This creates a new test case with the generated test inputs.
  • Test Type:: Baseline Test. This gives the option to capture baseline data output from the model for the generated tests.
  • Test File:: RollRefTest. This re-uses the existing test file.

3. Click OK. A dialog box shows progress of the test case generation. When test case generation is complete, a new test case appears in the Test Manager.

Run the New Test Case

1. Cut and paste the new test case into the Logged Data and Coverage test suite.

2. Run the Logged Data and Coverage test suite again.

3. When simulation completes, in the Results and Artifacts section, select the result set and expand the Aggregated Coverage Results. The test suite achieves complete coverage:

  • Decision: 100%
  • Condition: 100%
  • MCDC: 100%

Cleanup

Clear variables and test results, and close the model.

clear filePath reqDoc rollModel testFile testHarness topModel;
sltest.testmanager.clearResults;
sltest.testmanager.close;
close_system('RollAutopilotRevised',0);