Create Test Harnesses and Select Properties

Create a Test Harness For a Top Level Model

To create a test harness for a top-level model:

  1. Right-click in the Simulink® model and click Test Harness > Create for Model to open the Create Test Harness dialog box.

  2. After selecting the desired options, click OK to create the test harness.

Create a Test Harness For a Model Component

To create a test harness for a single model component:

  1. On the Apps tab, under Model Verification, Validation, and Test, click Simulink Test.

  2. On the Tests tab, click Simulink Test Manager to open the Test Manager.

  3. Select the desired

  4. Click New > Test for Model Component, which opens the Create Test For Model Component workflow wizard.

  5. Create the test harness by completing the wizard pages.

Note

The Create Test For Model Component workflow wizard exposes a subset of test harness options. If your test harness does not need to use non-default options, use the wizard to create a harness quickly. If you need to change other options, use the Test Manager for the test harness you created with the wizard.

For information on using the wizard and the properties it sets, see Generate Tests for a Component.

Preview and Open Test Harnesses

When a model component has a test harness, a badge appears in the lower right of the block. To view the test harnesses, click the badge. To open a test harness, click a tile.

To view test harnesses for a model block diagram, click the pullout icon in the model canvas. To open a test harness, click a tile.

Change Test Harness Properties

To change properties of an open test harness, click the badge in the test harness block diagram and click Test harness properties to open the harness properties dialog box.

To change properties of test harnesses from the main model, click the Harness operations icon from the test harness preview.

Considerations for Selecting Test Harness Properties

Before selecting test harness properties, consider the following:

  • What data source you want to use for your test case input

  • How you want to view or store test output

  • Whether you want to copy parameters and workspaces from the main model to the harness

  • Whether you plan to edit the component under test

  • How you want to synchronize changes between the test harness and model

Except for sources and sinks, you can change harness properties later using the harness properties dialog box. To change sources and sinks after harness creation, manually remove the blocks from the test harness and replace them with new sources and sinks.

Note

The following sections describe the test harness properties in the Create Test Harness dialog box. For information on the test harness properties in the Create Test for Model Component wizard, see Generate Tests for a Component.

Harness Name

Test harnesses must use valid MATLAB® filenames.

Save Test Harnesses Externally

This option controls how the model stores test harnesses. A model stores all its test harnesses either internally or externally. If a model already has test harnesses, this item states the harness storage type as Harnesses saved <internally|externally>.

  • When cleared, the model saves test harnesses as part of the model SLX file.

  • When selected, the model saves test harnesses in separate SLX files to the current working folder, and adds a harness information XML file to the model folder. The harness information file must remain in the same folder as the model.

See Manage Test Harnesses.

Sources and Sinks

In the Create Test Harness dialog box, under Sources and Sinks, select the source and sink from the respective menus. The menus provide common sources and sinks.

You can use source and sink blocks from the Simulink Sources or Sinks library. Select Custom source or sink, and enter the path to the block. For example:

simulink/Sources/Sine Wave
simulink/Sinks/Terminator

Custom sources and sinks build the test harness with one block per port.

Create scalar inputs

When you select this property, the test harness creates scalar inputs for multidimensional signals. The individual scalar inputs are reshaped to match the dimension of the input signals to the component under test. This option applies to test harnesses with Inport, Constant, Signal Builder, From Workspace, or From File source blocks.

Add scheduler for function-calls and rates / Generate function-call signals using

The title of this option depends on whether the component under test is a subsystem or a model. To include a scheduler block in your test harness, select a block from the drop-down list.

  • Add scheduler for function-calls and rates: For a model, you can use the block to call functions and set sample times for model inputs and outputs.

  • Generate function-call signals using: For a subsystem, you can use the block to call functions in the subsystem.

Enable Initialize, Reset, and Terminate ports

Selecting this option exposes initialize, terminate, or reset function-call ports in the component under test and connects the scheduler block to the ports.

This option appears when you create a test harness for a top-level model and select a block for the Add scheduler for function-calls and rates option.

Add Separate Assessment Block

Select Add separate assessment block to include a separate Test Assessment block in the test harness.

A Test Assessment block is a separate Test Sequence block configured with properties commonly used for verifying the component under test. For more information, see Assess Simulation and Compare Output Data and Assess Model Simulation Using verify Statements.

Open Harness After Creation

Clear Open Harness After Creation to create the test harness without opening it. This can be useful creating multiple test harnesses in succession.

Create without compiling the model

Creating a test harness without compiling the model can be useful if you are prototyping a design that cannot yet compile. When you create a test harness without compiling the main model:

  • Parameters are not copied to the test harness workspace.

  • The main model configuration is not copied to the test harness.

  • The test harness does not contain conversion subsystems.

You may need to add blocks such as signal conversion blocks to the test harness. You can rebuild the harness when you are ready to compile the main model. For more information, see Synchronize Changes Between Test Harness and Model.

Verification Modes

The test harness verification mode determines the type of block generated in the test harness.

  • Normal: A Simulink block diagram.

  • Software-in-the-Loop (SIL): The component under test references generated code, operating as software-in-the-loop. Requires Embedded Coder®.

  • Processor-in-the-Loop (PIL): The component under test references generated code for a specific processor instruction set, operating as processor-in-the-loop. Requires Embedded Coder.

Note

Keep the SIL or PIL code in the test harness synchronized with the latest component design. If you select SIL or PIL verification mode without selecting Rebuild harness on open, your SIL or PIL block code might not reflect recent updates to the main model design. To regenerate code for the SIL or PIL block in the test harness, select Rebuild Harness > Update Harness Configuration Settings and Modal Workspace .

Post-create callback method

You can customize your test harness using a post-create callback. A post-create callback is a function that runs after the harness is created. For example, your callback can set up signal logging, add custom blocks, or change the harness simulation times. For more information, see Customize Test Harnesses.

Rebuild harness on open

When you select this property, the test harness rebuilds every time you open it. For details on the rebuild process, see Synchronize Changes Between Test Harness and Model.

Update Configuration Parameters and Model Workspace data on rebuild

When you select this property, configuration parameters and model workspace data update when you rebuild the harness. For details on the rebuild process, see Synchronize Changes Between Test Harness and Model.

Post-rebuild callback method

You can customize your test harness using a post-rebuild callback. A post-rebuild callback is a function that runs after the harness is rebuilt. For example, your callback can set up signal logging, add custom blocks, or change the harness simulation times. For more information, see Customize Test Harnesses.

Synchronization Mode

Synchronization mode controls when changes to the component under test are synced to the main model, and when changes to the harness owner are synced into a test harness.

  • On harness open — The component in the test harness is updated when the harness opens. Synchronizing on harness open is useful if you update the design in the main model.

  • On harness close — The component in the main model is updated when the harness closes. Synchronizing on harness close is useful if you make design changes in the test harness. Avoid synchronizing on harness close if you want to prevent inadvertent changes to the component in the main model.

  • During push — Synchronization occurs manually, by clicking Push Changes.

  • During rebuild — Synchronization occurs manually, by selecting Rebuild Harness > Rebuild Harness without Compiling Main Model or Update Harness Configuration Settings and Model Workspace.

See Also

|