To create a test harness for a top-level model (including Subsystem and Model reference models):
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.
Create a new test file in the Test Manager.
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.
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 and Test Harness for a Component or Model.
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.
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 and Test Harness for a Component or Model.
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.
Select the function interface to associate with the reusable library subsystem test harness. This option appears only if the component under test is a reusable library subsystem with a function interface.
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.
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. This option does not apply to Subsystem models.
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.
Test harnesses for Subsystem models are created without compiling the model.
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.
Subsystem model test harnesses do not support SIL or PIL verification.
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 Model Workspace.
If generated code for the SIL/PIL block already exists, select this property to use that existing code instead of regenerating the code. This option is available only for subsystem harnesses. It does not apply to Subsystem model test harnesses.
Specify the folder that contains the generated code for the SIL/PIL block. This option is available only if you selected Use generated code to create SIL/PIL block.
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. This option does not apply to Subsystem model test harnesses.
When you select this property, the test harness rebuilds every
time you open it. If you specified to use existing generated
code for a SIL/PIL subsystem using
sltest.harness.create
or
sltest.harness.set
, the harness
rebuild uses that code instead of regenerating it. For
details on the rebuild process, see Synchronize Changes Between Test Harness and Model. This option does not apply to Subsystem model test
harnesses.
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. This option does not apply to Subsystem model test harnesses.
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. This option does not apply to Subsystem model 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.
Synchronize on harness open — When the test harness opens, the test harness components, configuration set, and parameters synchronize from the model to the test harness. This option is available for:
Block diagrams
Subsystems, including Stateflow® charts and MATLAB Function blocks
For Subsystem Reference blocks, only the block parameters are synced, not the block contents.
Model reference blocks
S-function blocks
The Synchronize on harness open option is not available for:
SIL/PIL harnesses
Subsystem model harnesses
Synchronize on harness open and close — When the test harness opens, the test harness components, configuration set, and parameters synchronize from the model to the test harness. When the test harness closes, the same elements synchronize from the harness to the model. This option is available for:
Block diagrams
Subsystems, including Stateflow charts and MATLAB Function blocks
For Subsystem Reference blocks, only the block parameters are synced, not the block contents.
Model reference blocks
S-function blocks
Subsystem model harnesses
The Synchronize on harness open and close option is not available for:
Block diagrams
SIL/PIL harnesses
Subsystem model harnesses
Synchronize only during push and rebuild — Synchronizes when you click Push Changes or Rebuild Harness. Push Changes synchronizes changes from the test harness to the model. Rebuild Harness synchronizes changes from the model to the test harness. This option is available for:
Subsystems, including Stateflow charts and MATLAB Function blocks
Model reference blocks
S-function blocks
Subsystem models, which always synchronize on push and rebuild only.
The Synchronize only during push and rebuild option is not available for:
Block diagrams
SIL/PIL harnesses
Components in libraries
Synchronize only during rebuild — Synchronizes only when you click Rebuild Harness. Rebuild Harness synchronizes changes from the model to the test harness. This option is available for:
Block diagrams
Subsystems, including Stateflow charts and MATLAB Function blocks
Model reference blocks
S-function blocks
SIL/PIL verification mode components
The Synchronize only during rebuild option is not available for:
Components in libraries
Synchronize Changes Between Test Harness and Model | Test Sequence