A test harness is a model that isolates the component under test, with inputs, outputs, and verification blocks configured for testing scenarios. You can create a test harness for a model component or for a full model. A test harness gives you a separate testing environment for a model or a model component. For example:
You can unit-test a subsystem by isolating it from the rest of the model.
You can create a closed-loop testing scenario for a controller by adding a plant model to the test harness.
You can keep your main model clear of unneeded verification blocks by placing Model Verification and Test Assessment blocks in the test harness.
To assign a test harness to a test case, select Test Harness in the System Under Test section of the Test Manager.
You can save the harness with your model, or you can save it in an external file. If your model is under change management, consider saving the test harness in an external file. The harness works the same whether it is internal or external to the model. For more information, see Manage Test Harnesses and Synchronize Changes Between Test Harness and Model.
In this example, you create a harness to test the shift_logic subsystem of the model
Open the model
sltestCarRootInport from the folder
Right-click the shift_logic subsystem. From the context menu, select TestHarness > Create for 'shift_logic'.
In the Create Test Harness dialog box, specify the inputs, outputs, and other options:
Use Constant blocks to provide input signals. Under
Sources and Sinks, set the source to
Constant and the sink to
Leave the other options with their default selections. By default:
The harness saves with the model file.
The harness synchronizes with the model on open, which means that changes to the model update the harness.
Click OK to create the test harness.
At the center of the harness is a copy of the
shift_logic subsystem is the component under test.
The two vertical subsystems contain signal specification and routing. For more information on test harness architecture, see Test Harness Construction for Specific Model Elements.
Assign values to the Constant blocks to test the component:
Change the value of the speed block to
Change the value of the throttle block to
Simulate the harness.
Open the scope and look at the result. The shift controller selects third gear.
In the previous case, you supplied test inputs with Constant blocks. You can also use test inputs from external data files.
Create a test harness that uses Inport sources.
Create a test case that uses the test harness as the System Under Test.
Map external inputs to the test case.
Using a test case in the Test Manager allows you to iterate with different test vectors, add test cases, run batches of test cases, and organize your results. This example shows you how to use external data with a test harness, and simulate from the Test Manager.
Open the Test Manager by selecting Analysis > Test Manager from the Simulink menu.
Select New > Test File from the Test Manager toolstrip.
Name the file
Select New Test Case 1.In the System Under Test section, click the Use current model button .
For Test Harness, select
ShiftLogic_InportHarness from the drop down list. The test harness
already exists in the model.
In the Inputs section, click Create.
Name the input data file
shift_logic_input and select MAT file
In the Signal Editor, enter the values for the inputs:
Select the speed signal and enter
Apply to update the plot.
Select the throttle signal and enter
Apply to update the plot.
Click Save in the Signal Editor toolstrip.
Select output data to capture.
In the Simulation Outputs section of the Test Manager, click Add
In the test harness block diagram, select the
line. Select the signal in the Connect dialog box.
Click Done to add the signal to the test case outputs.
Click Run in the Test Manager toolstrip.
Expand the results and highlight the gear signal output. The plot shows the controller selects third gear.