MATLAB Examples

Test Harness Callback Example

This example shows how to use a post-create callback to customize a test harness. The callback changes one harness source from an Inport block to Constant block and enables signal logging in the test harness.


The Model

In this example, you create a test harness for the Roll Reference subsystem.


Get Path to the Harness Customization Function

cbFile = fullfile(matlabroot,'examples','simulinktest','main',...

The Customization Function and Test Harness Information

The function harnessSourceLogCustomization changes the third source block, and enables signal logging on the component under test inputs and outputs. You can read the function by entering:


The function uses an argument. The argument is a struct listing test harness information. The information includes handles to blocks in the test harness, including:

  • Component under test
  • Input subsystems
  • Sources and sinks
  • The harness owner in the main model

For example, harnessInfo.Sources lists the handles to the test harness source blocks.

Create the Customized Test Harness

1. Copy the harness customization function to the temporary working directory.


2. In the RollAutopilotMdlRef model, right-click the Roll Reference subsystem and select Test Harness > Create for Roll Reference.

3. In the harness creation dialog box, for Post-create callback method, enter harnessSourceLogCustomization.

4. Click OK to create the test harness. The harness shows the signal logging and simulation stop time specified in the callback function.

You can also use the sltest.harness.create function to create the test harness, specifying the callback function with the 'PostCreateCallback' name-value pair.

sltest.harness.create('RollAutopilotMdlRef/Roll Reference',...
    'PostCreateCallback','harnessSourceLogCustomization');'RollAutopilotMdlRef/Roll Reference','LoggingHarness');