Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 36 ยท Testing and Verification

Simulink Test: Test Harnesses, Test Sequences and the Test Manager

Test Simulink components with Simulink Test: create a test harness, write inputs with Test Sequence blocks, add assessments, build baseline and equivalence tests in the Test Manager.

Pressing Run and eyeballing a Scope is not a test. A real test has defined inputs, an expected result and a pass/fail verdict that a machine can check every time the model changes. Simulink Test gives you the tools: a test harness that isolates a component, blocks that generate inputs and assess outputs, and the Test Manager that runs many tests and reports. This lesson covers each, with labelled Python stand-ins that show the logic. Simulink Test is a commercial product and does not run on this site.

Why isolate a component

Testing a controller inside the whole closed-loop model is slow and indirect: a failure could come from anywhere. A test harness is a separate model that contains the component under test (CUT) plus its test inputs and checks. The harness is stored with the original model (inside the same file), does not change it and can be opened, run and deleted without touching the design.

Create a test harness

  1. Select a block, subsystem, Stateflow chart or model reference in the model.
  2. Right-click > Test Harness > Create for Selected Subsystem... (or Create for model, on the Apps tab > Simulink Test > Create Test Harness).
  3. In the dialog choose:
    • Name of the harness (for example Controller_Harness).
    • Source: where inputs come from: Inport, Signal Builder, Signal Editor, Test Sequence, Test Assessment, Constant, Chirp/Step/Ramp, From Workspace.
    • Sink: Outport, Scope, or a Test Assessment block.
    • Post-create: open the harness.
  4. Click OK. Simulink creates the harness with the CUT copied or referenced (a synchronization option can keep it in step with the original when the design changes).

The harness appears under Test Harnesses in the model's Modeling > Test Harness Manager, and you can reopen it later. Use the Push/Pull buttons to synchronize changes.

flowchart LR
    I["Test inputs<br/>(Test Sequence / Signal Editor)"] --> C["Component under test"]
    C --> A["Test Assessment<br/>(verify statements)"]
    C --> L["Logged outputs"]
    L --> B["Baseline comparison"]

A harness wraps the component with inputs, assessments and logging.

Test Sequence block

A Test Sequence block (Simulink Test library) writes input signals as a sequence of steps, in MATLAB-like text with a step diagram. You list steps, each with actions (what to output), transitions (when to move on) and duration:

TEXT
Step: Idle              % output 0 for 1 second
  Entry: speed_set = 0;
  Transition: after(1, sec) -> Ramp
Step: Ramp
  During: speed_set = speed_set + 0.5 * dt;
  Transition: [speed_set >= 20] -> Hold
Step: Hold
  Entry: speed_set = 20;
  Transition: after(5, sec) -> Done

(The exact textual form in the block is a table with Step, Transition, Next Step and Action columns; the pseudo code above shows the meaning.) The same block can assess output using verify statements, so the inputs and checks live together.

Assessments

Use verify (not assert: verify keeps running, reports all failures; assert stops) in a Test Assessment block or inside a Test Sequence step:

TEXT
verify(speed >= 0, 'speed must not be negative');
verify(abs(speed - speed_set) < 0.5, 'tracking error too large');
verify(duration(speed > 25) < 0.2, 'overshoot above 25 for too long');   % temporal assessment

Temporal assessments use temporal logic such as after, before, duration and at (as in Stateflow) so you can state rules like "the overshoot lasts less than 0.2 seconds". A failed verify marks a step in the result, shows where and when it failed and stores a log.

The stand-in below mimics verify and duration over a logged time series.

Python Stand-in: verify and duration assessments over a signal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The Test Manager

Open it from Apps > Simulink Test > Test Manager or sltest.testmanager.view. It organizes everything in a test file (.mldatx) with test suites and test cases. In the left pane choose New > Test File for Model; in the right pane set up:

Section What you set
System Under Test Model or harness, release (even older MATLAB releases), simulation mode (Normal, Accelerator, SIL, PIL)
Simulation Settings and Release Overrides Stop time, solver, and overrides
Parameter Overrides Values for model/base workspace parameters
Inputs Signal Editor scenarios, MAT-files, Excel
Outputs Signals to log
Baseline Criteria Expected data and tolerances (baseline tests)
Assessments Enable logical and temporal assessments
Coverage Collect model coverage (see model coverage)
Iterations Run the case multiple times with different parameters/inputs
Callbacks MATLAB scripts before/after

Three test case types:

  • Baseline test: run the CUT, compare logged outputs with saved baseline data under tolerances (absolute, relative, leading and lagging time). Use Capture Baseline the first time (after you reviewed the result by hand).
  • Equivalence test: run two simulations (Simulation 1 and Simulation 2) and compare: for MIL versus SIL, model versus a previous release, normal versus accelerator, or two different parameter sets.
  • Simulation test: runs the model and checks only assessments (verify statements), with no baseline.

Run with the green Run button (or sltest.testmanager.run). Results appear in the Results and Artifacts pane: pass or fail per test, plots of compare signals with the tolerance band, assessment results and coverage. Create Report exports a PDF, DOCX or HTML report with plots, which many teams attach to a review.

Iterations and parameter sweeps

Add Iterations to run the same case with different parameter values, input signals or configuration sets. For example run the controller test with speed_set of 10, 20 and 30, or with each sensor-fault scenario. In command line scripts:

MATLAB
tf = sltest.testmanager.TestFile('controllerTests.mldatx');
results = sltest.testmanager.run;
sltest.testmanager.report(results, 'report.pdf');

The stand-in below shows a baseline test with absolute and relative tolerance and a parameter iteration.

Python Stand-in: baseline test with tolerances and parameter iterations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The test passes or fails depending on the tolerance you chose, which shows why tolerances must be argued from requirements and not widened until the test is green.

Good test design

  • Trace each test to a requirement (see requirements and formal verification).
  • Cover normal, boundary and fault inputs: zero, maximum, just beyond the limits, sensor dropout, invalid values.
  • Make each test independent and repeatable: seed random sources and set initial conditions.
  • Use small tests for units (a subsystem) and scenario tests for the whole system.
  • Keep tests under source control next to the model; run them in continuous integration (matlab -batch "runtests" or the sltest.testmanager.run command).

Common mistakes

  • Capturing a baseline without reviewing it: you encode the bug as the expectation.
  • Huge tolerances that always pass.
  • Using assert where verify is better, hiding later failures.
  • Testing the plant instead of the component under test.
  • Not testing fault and boundary cases.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a test harness in Simulink Test?

    The harness isolates the component so that you can drive and check it without changing the design.
  2. 2. Which test case type compares the model with generated code (for example MIL versus SIL)?

    An equivalence test runs two simulations and compares them.
  3. 3. Why prefer verify over assert in assessments?

    assert stops at the first failure; verify records it and keeps running.
  4. 4. What is a good practice when capturing a baseline?

    A baseline from an unreviewed run turns a bug into the expected value.

Summary

  • A test harness isolates the component under test and holds inputs, assessments and logging.
  • Test Sequence blocks generate step-wise inputs; verify and temporal assessments give pass or fail verdicts.
  • The Test Manager organizes baseline, equivalence and simulation tests, iterations, coverage and reports.
  • Choose tolerances deliberately, trace tests to requirements, and run them automatically.

Next: Model coverage