Create a Test Harness

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.

Create the Harness

In this example, you create a harness to test the shift_logic subsystem of the model sltestCarRootInport.

  1. Open the model sltestCarRootInport from the folder matlab/examples/simulinktest.

  2. Right-click the shift_logic subsystem. From the context menu, select TestHarness > Create for 'shift_logic'.

  3. In the Create Test Harness dialog box, specify the inputs, outputs, and other options:

    1. Use Constant blocks to provide input signals. Under Sources and Sinks, set the source to Constant and the sink to Scope.

    2. 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.

  4. Click OK to create the test harness.

At the center of the harness is a copy of the shift_logic subsystem. 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.

Simulate the Test Harness

Assign values to the Constant blocks to test the component:

  1. Change the value of the speed block to 50.

  2. Change the value of the throttle block to 30.

  3. Simulate the harness.

  4. Open the scope and look at the result. The shift controller selects third gear.

Test Using the Test Manager

In the previous case, you supplied test inputs with Constant blocks. You can also use test inputs from external data files.

  1. Create a test harness that uses Inport sources.

  2. Create a test case that uses the test harness as the System Under Test.

  3. 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.

  1. Open the Test Manager by selecting Analysis > Test Manager from the Simulink menu.

  2. Select New > Test File from the Test Manager toolstrip.

  3. Name the file ShiftLogicTest.

  4. Select New Test Case 1.In the System Under Test section, click the Use current model button .

  5. For Test Harness, select ShiftLogic_InportHarness from the drop down list. The test harness already exists in the model.

  6. In the Inputs section, click Create. Name the input data file shift_logic_input and select MAT file format.

  7. In the Signal Editor, enter the values for the inputs:

    1. Select the speed signal and enter 50 for times 0 and 30. Click Apply to update the plot.

    2. Select the throttle signal and enter 30 for times 0 and 30. Click Apply to update the plot.

    3. Click Save in the Signal Editor toolstrip.

  8. Select output data to capture.

    1. In the Simulation Outputs section of the Test Manager, click Add

    2. In the test harness block diagram, select the gear signal line. Select the signal in the Connect dialog box.

    3. Click Done to add the signal to the test case outputs.

  9. Click Run in the Test Manager toolstrip.

  10. Expand the results and highlight the gear signal output. The plot shows the controller selects third gear.

Related Topics