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

Create a test harness for the Roll Reference subsystem.


The Callback Function

The function harnessSourceLogCustomization changes the third source block, and enables signal logging on the inputs and outputs to the component under test.

function harnessSourceLogCustomization(harnessInfo)

%% Customize Test Harness Sources and Logging

%% Add a Constant Source Block to One Input

% Get harness model name:
    harnessName = get_param(harnessInfo.HarnessModel,'Name');
% Add Constant block:
    constBlock = add_block('simulink/Sources/Constant',[harnessName '/ConstInput']);
% Get handles for relevant ports and lines:
    constPorts = get_param(constBlock,'PortHandles');
    icsPorts = get_param(harnessInfo.InputConversionSubsystem,'PortHandles');
    icsLineHandles = get_param(harnessInfo.InputConversionSubsystem,'LineHandles');

% Delete the existing Inport block and the adjacent line:

% Connect the Constant block to the input conversion subsystem:

%% Enable signal logging for all inputs and outputs to the component under test

% Get port handles for the component under test
    CUTPorts = get_param(harnessInfo.HarnessCUT,'PortHandles');
        % Get CUT port handles
    ICSPorts = get_param(harnessInfo.InputConversionSubsystem,'PortHandles');
        % Get ICS port handles

% Enable signal logging on the outputs of the CUT
    numOut = length(CUTPorts.Outport);
    for j = 1:numOut
% Enable signal logging on the outputs of the input conversion subsystem,
% which are the same as the inputs to the CUT. Logging must be done on
% output handles only.
    numIn = length(ICSPorts.Outport);
    for k = 1:numIn

Test Harness Information for the Callback

The callback 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, if you define your callback function as harnessBlockAdd(harnessInfo), harnessInfo.Sources will list the handles to the test harness source blocks.

Create the Test Harness and Customize it with the Callback

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

cbFile = which('harnessSourceLogCustomization.m');

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',...
    'Name','LoggingHarness','PostCreateCallback','harnessSourceLogCustomization');'RollAutopilotMdlRef/Roll Reference','LoggingHarness');