Main Content

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.

Functional testing workflow

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:

  1. Run the tests.

  2. Determine test coverage by using Simulink® Coverage™.

  3. Increase coverage with additional tests generated by Simulink Design Verifier™.

  4. Report the results.

Open the Test Harness and Model

  1. Open the project:

    pr = openProject("SimulinkVerificationCruise");

  2. Open the model and the test harness. At the command line, enter:

    open_system simulinkCruiseAddReqExample"simulinkCruiseAddReqExample","SafetyTest_Harness1")
  3. 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);

  4. Open the Test Sequence block. The sequence verifies system disengagement when either:

    • The brake pedal is pressed.

    • Speed exceeds a limit.

Measure Model Coverage

  1. In the Simulink Test Manager, select the slReqTests test file.

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

  3. Click Run on the Test Manager toolstrip.

  4. After the test completes, select Results. The test achieves 50% decision coverage, 41% condition coverage, and 25% MCDC coverage.

    Coverage results

Generate Tests to Increase Model Coverage

  1. Use Simulink Design Verifier to generate additional tests to increase model coverage. In Results and Artifacts, select the slReqTests test file and open the Aggregated Coverage Results section located in the right pane.

  2. Right-click the test results and select Add Tests for Missing Coverage.

  3. Under Harness, choose Create a new harness.

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

  5. 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 by using sltest.testmanager.addTestsForMissingCoverage (Simulink Test) and sltest.testmanager.TestOptions (Simulink Test).

Related Topics