Manage Test Harnesses
Internal and External Test Harnesses
You can save test harnesses internally as part of your model SLX file, or externally in separate SLX files. A model stores all test harnesses either internally or externally; it is not possible to use both types of harness storage in one model. You select internal or external test harness storage when you create the first test harness. If your model already has test harnesses, you can convert between the harness storage types.
If you store your model in a configuration management system, consider using external test harnesses. External test harnesses enable you to create or change a harness without changing the model file. If you plan to share your model often, consider using internal test harnesses to simplify file management. Creating or changing an internal test harness changes your model SLX file. Both internal and external test harnesses offer the same synchronization, push, rebuild, and badge interface functionality.
Manage External Test Harnesses
Harnesses stored externally use a separate SLX file for each harness, and a
<modelName>_harnessInfo.xml file that contains metadata
linking the model and the harnesses. Changing test harnesses can change the
harnessInfo.xml file. The metadata and the model are linked using
a unique ID for the model. The test harness metadata XML file is created and stored by
default in the same folder as the model. You can move the metadata XML file to a
different folder on the MATLAB path, if desired. The link between the model and its
harnesses persists as long as the harness metadata file, model, and harnesses are
all on the MATLAB path.
Follow these guidelines for external test harnesses:
Do not delete or make any manual changes to
harnessInfo.xml file. Deleting the
harnessInfo.xml file might sever the relationship between the
model and harnesses, which cannot be regenerated from the model.
harnessInfo.xmlfile must be writable to save changes to the test harness or the main model.
Folders containing test harness SLX files must be on the MATLAB® path.
If the test harness
harnessInfo.xmlfile is not in the same folder as the model, the XML file or its folder must be on the MATLAB path.
If you convert internal test harnesses to external test harnesses, the new SLX files save to the current working folder.
If you convert external test harnesses to internal test harnesses, the external SLX files can be anywhere on the MATLAB path.
If your model uses external test harnesses, only create a copy of your model using Save > Save as. Using Save as copies external test harnesses to the destination folder of the new model, renames the harnesses, and keeps the harness information current.
Copying the model file on disk will not copy external harnesses associated with the model.
Only change or delete test harnesses using the Simulink® UI or commands:
To delete test harnesses, use the thumbnail UI or the
To rename test harnesses, use the harness properties UI or the
To make a copy of an externally saved test harness, use the
sltest.harness.clonecommand or save the test harness to a new name using Save > Save as.
Deleting or renaming harness files outside of Simulink causes an inaccurate
harnessInfo.xmlfile and problems loading test harnesses.
Convert Between Internal and External Test Harnesses
You can change how your model stores test harnesses at different phases of your model lifecycle. For example:
Develop your model using internal test harnesses so that you can more easily share the model for review. When you complete your design and place the model under change control, convert to external harnesses.
Use the configuration management model as the starting point for a new design. Test the existing model with external harnesses to avoid modifying it. Then, create a copy of the existing model. Convert to internal harnesses for the new development phase.
To change the test harness storage to external (or internal):
Navigate to the top of the main model.
On the Apps tab, under Model Verification, Validation, and Test, click Simulink Test. Then, on the Tests tab, click Manage Test Harnesses > Convert to External Harnesses or Convert to Internal Harnesses.
A dialog box provides information on the conversion procedure and the affected test harnesses. Click Yes to continue.
The harnesses are converted.
The conversion to external test harnesses creates an SLX file for each test harness and a harness information XML file
Inversely, conversion to internal test harnesses moves the test harness SLX files and the
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.
Find Test Cases Associated with a Test Harness
To list open test cases that refer to the test harness, click the badge in the test harness canvas. You can click a test case name and navigate to the test case in the Test Manager.
Export Test Harnesses to Standalone Models
You can export test harnesses to standalone models, which is useful for archiving test harnesses or sharing a test harness design without sharing the model.
To export an individual test harness:
From the individual harness model, on the Apps tab, under Model Verification, Validation, and Test, click Simulink Test.
In the Harness tab, click Detach and Export.
In the Export Test Harness to Independent Model dialog box, click OK.
In the Save As dialog box, enter a file name for the standalone harness model and click OK.
The harness converts to a standalone model.
Converting removes the harness from the main model and breaks the relationship to the main model. If a model has only one harness, its
harnessInfo.xmlfile is deleted. If a model has more than one harness and you delete one of them, the
harnessInfo.xmlfile is updated.
To export all harnesses in a model:
Navigate to the top level of the model. Do not select any blocks.
On the Apps tab, under Model Verification, Validation, and Test, click Simulink Test.
In the Harness tab, click Detach and Export.
In the Export Test Harness to Independent Model dialog box, click OK.
In the Save As dialog box, enter a file name for the separate model and click OK.
All test harnesses are exported and converted into standalone models.
Exporting removes the harnesses from the main model, deletes the
harnessInfo.xmlfile, and breaks the relationships to the main model.
Clone and Export a Test Harness to a Separate Model
This example demonstrates cloning an existing test harness and exporting the cloned harness to a separate model. This can be useful if you want to create a copy of a test harness as a separate model, but leave the test harness associated with the model component.
If you don't know the exact properties of the test harness you want to clone, get them using
sltest.harness.find. You need the harness owner ID and the harness name.
Clone the test harness using sltest.harness.clone.
Export the test harness to a separate model using
sltest.harness.export. Note that there is no association between the exported model and the original model. The exported model stands alone.
Open the Model and Save a Local Copy
model = 'sltestTestSequenceExample'; open_system(model)
Save the local copy in a writable location on the MATLAB path.
Get the Properties of the Source Test Harness
properties = sltest.harness.find([model '/shift_controller'])
properties = struct with fields: model: 'sltestTestSequenceExample' name: 'controller_harness' description: '' type: 'Testing' ownerHandle: 10.0007 ownerFullPath: 'sltestTestSequenceExample/shift_controller' ownerType: 'Simulink.SubSystem' isOpen: 0 canBeOpened: 1 verificationMode: 0 saveExternally: 0 rebuildOnOpen: 0 rebuildModelData: 0 postRebuildCallback: '' graphical: 0 origSrc: 'Test Sequence' origSink: 'Test Assessment' synchronizationMode: 0 existingBuildFolder: '' functionInterfaceName: ''
Clone the Test Harness
Clone the test harness using sltest.harness.clone, the
ownerFullPath and the
name fields of the harness properties structure.
Save the Model
Before exporting the harness, save changes to the model.
Export the Test Harness to a Separate Model
Export the test harness using sltest.harness.export. The exported model name is
sltest.harness.export([model '/shift_controller'],'ControllerHarness2',... 'Name','ControllerTestModel')
clear('model') clear('properties') close_system('sltestTestSequenceExample',0)
Delete Test Harnesses Programmatically
This example shows how to delete test harnesses programmatically. Deleting using the programmatic interface can be useful when your model has multiple test harnesses at different hierarchy levels. This example demonstrates how to create four test harnesses and then, delete them.
1. Open the model
2. Create two harnesses for the
transmission subsystem, and two harnesses for the
transmission ratio subsystem.
sltest.harness.create('sltestCar/transmission'); sltest.harness.create('sltestCar/transmission'); sltest.harness.create('sltestCar/transmission/transmission ratio'); sltest.harness.create('sltestCar/transmission/transmission ratio');
3. Find the harnesses in the model.
test_harness_list = sltest.harness.find('sltestCar')
test_harness_list = 1x5 struct array with fields: model name description type ownerHandle ownerFullPath ownerType isOpen canBeOpened verificationMode saveExternally rebuildOnOpen rebuildModelData postRebuildCallback graphical origSrc origSink synchronizationMode existingBuildFolder functionInterfaceName
4. Delete the harnesses.
for k = 1:length(test_harness_list) sltest.harness.delete(test_harness_list(k).ownerFullPath,... test_harness_list(k).name) end
Move and Clone Test Harnesses
Simulink Test™ gives you the ability to move or clone test harnesses from a source owner to a destination owner without having to compile the model. You can move or clone:
Subsystem harnesses across subsystems. The destination subsystem could also be in a different model.
Harnesses for library components across libraries.
Subsystem Reference block harnesses to other Subsystem Reference block harnesses.
Subsystem Reference block harnesses to and from Subsystem model harnesses.
To move or clone harnesses, right-click the Simulink canvas and select Test Harness > Manage Test Harnesses. The Manage Test Harness dialog box opens and lists the test harnesses associated with the subsystem or block specified in Filter by harness owner. Click Actions to access the Move and Clone options.
Select the destination path and name your test harness.
Test Harness Transfer When Converting Subsystems to Model References
When you convert a Subsystem or Subsystem Reference block to a Model block, the test harnesses are transferred to the model being referenced. Harnesses on the Subsystem block are converted to block diagram harnesses. Nested harnesses in subsystems are copied to identical blocks in the Model block. All transferred harnesses are internal harnesses in the model reference. Test harnesses might be renamed when they are transferred. You can see feedback about the harness transfer in the Complete Conversion pane of the Conversion Advisor or at the MATLAB command line.
These limitations apply to converting a Subsystem or Subsystem Reference block to a Model block:
SIL and PIL harnesses are not transferred.
Requirements in a test harness for a subsystem are not transferred. You must copy them manually.
sltest.harness.move | Model Reference Conversion Advisor