Test Harness and Model Relationship

A test harness is a model block diagram that you can use to test, edit, or debug a Simulink® model. In the main model, you associate a harness with a model component or the top-level model. The test harness contains a separate model workspace and configuration set. The test harness is associated with the main model and can be accessed through the model canvas.

You build the test harness model around the component under test, which links the harness to the main model. If you edit the component under test in the harness, the main model updates when you close the harness. You can generate a test harness for:

  • A model component, such as a subsystem, library block, or Model block. The test harness isolates the component in a separate simulation environment.

  • A top-level model. The component under test is a Model block referencing the main model.

Harness-Model Relationship for a Model Component

When you associate a test harness with a model component, the harness model workspace contains copies of the parameters associated with the component. For example, suppose that you create a test harness for a component that contains a Gain block and then add a second Gain block to the harness.

  • The parameter g defines part of the component under test, so the harness model workspace contains a copy of g.

  • The parameter a defines part of the main model outside of the component under test, so the harness model workspace does not contain a copy of a.

  • The parameter h is the gain of the Gain block that you added to the harness. Because this block is outside the component under test, h exists only in the harness model workspace.

Harness-Model Relationship for a Top-Level Model

When you associate a harness with the top level of the main model, the harness model workspace does not contain copies of parameters relevant to the component. The component under test is a Model block that references the main model, so the parameters remain in the main model workspace. For example, suppose that you create a test harness for a top-level model that contains a Gain block and then add a second Gain block to the harness.

  • The component under test references the main model, and the parameter g exists in the main model workspace. The harness model workspace does not contain a copy of g.

  • The parameter h is the gain of the Gain block that you added to the harness. Because this block is outside the component under test, h exists only in the harness model workspace.

Resolving Parameters

Parameters in the test harness resolve to the most local workspace. Parameters resolve to the harness model workspace, then the system model workspace, then the base MATLAB® workspace.

Test Harness Considerations

  • You can build a test harness for these types of model components:

    • Model Reference blocks

    • Subsystem blocks

    • Stateflow® blocks, including Stateflow charts, Truth Table blocks, State Transition Table blocks, and Test Sequence blocks

    • C Caller blocks

    • MATLAB Function blocks

    • User-defined function blocks

  • Open only one test harness at a time for each Simulink model.

  • Do not comment out the component under test in the test harness. Commenting out the component under test can cause unexpected behavior.

  • If a subsystem has a test harness, you cannot expand the subsystem contents into the model that contains the subsystem. Delete the test harness before expanding the subsystem. For more information, see Subsystem Expansion (Simulink).

  • Test harnesses are not supported for these types of Stateflow objects:

    • Atomic subcharts

    • Simulink based states

    • Simulink functions

    • MATLAB functions

  • The Upgrade Advisor and XML differencing are not supported for test harness models.

  • A test harness with a Signal Builder block source does not support:

    • Frame-based signals

    • Complex signals

    • Variable-dimension signals

  • For a test harness with a Test Sequence block source, all inputs to the component under test must operate with the same sample time.

Related Topics