To create a test harness for a top-level model:
Right-click in the Simulink® model and click Test Harness > Create for Model to open the Create Test Harness dialog box.
After selecting the desired options, click OK to create the test harness.
To create a test harness for a single model component:
On the Apps tab, under Model Verification, Validation, and Test, click Simulink Test.
On the Tests tab, click Simulink Test Manager to open the Test Manager.
Select the desired
Click New > Test for Model Component, which opens the Create Test For Model Component workflow wizard.
Create the test harness by completing the wizard pages.
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.
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.
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.
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.
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.
Test harnesses must use valid MATLAB® filenames.
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.
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:
Custom sources and sinks build the test harness with one block per port.
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.
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.
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.
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.
Clear Open Harness After Creation to create the test harness without opening it. This can be useful creating multiple test harnesses in succession.
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.
The test harness verification mode determines the type of block generated in the test harness.
Simulink block diagram.
(SIL): The component under test
references generated code, operating as
software-in-the-loop. Requires Embedded
(PIL): The component under test
references generated code for a specific processor
instruction set, operating as
processor-in-the-loop. Requires Embedded
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 .
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.
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.
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.
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 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.