To create a test harness for the top-level model, select Analysis > Test Harness > Create for Model. To create a test harness for a subsystem, select the subsystem and select Analysis > Test Harness > Create for <subsystem name>. Set test harness properties using the Create Test Harness dialog box.
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.
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, and you
can also use custom sources and sinks from the
Simulink® Sources or Sinks library. Select
Custom source or sink,
and enter the path to the custom block, such
Custom sources and sinks build the test harness with one block per port.
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. If you use a Test Sequence block source, you can also author assessments directly in the Test Sequence block. See Reuse Test Assessments.
Clear Open Harness After Creation to create the test harness without opening it. This can be useful creating multiple test harnesses in succession.
When you select this property, the main model does not compile when generating the test harness. The test harness does not contain conversion subsystems, configuration parameters, or model workspace data for the component under test. The test harness can require additional modification for it to compile, such as adding signal conversion blocks.
You can customize your test harness using a post-create callback, which 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, which 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 selecting Analysis > Test Harness > Push Component and Parameters to Main Model.
During rebuild — Synchronization occurs manually, by selecting Analysis > Test Harness > Rebuild Harness from Main Model.
Testing a model with initialize, terminate, or reset behavior
can require calling Initialize, Terminate, or Reset subsystems to
set the desired state. You can use the Test Sequence block
to schedule function calls using the
and function-call outputs.
You can automatically create a Test Sequence block configured to schedule function calls to Initialize, Terminate, or Reset inputs. When you create a test harness, select Generate scheduler for Initialize, Reset, and Terminate tasks in the Advanced Properties tab of the create harness dialog box. After test harness creation, the Test Sequence block contains template function calls for use in your test sequence.
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.
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
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. Regenerate code for the SIL or PIL block in the test harness by selecting Analysis > Test Harness > Rebuild Harness from Main Model.
Click the badge in the test harness block diagram and click Test harness properties to open the harness properties dialog box.