You can customize a test harness by using a function that runs as a callback after creating or rebuilding the test harness. In the function, write the commands to customize your test harness. For example, you can create functions to:
Connect custom source or sink blocks.
Add a plant subsystem for closed-loop testing.
Change the configuration set.
Enable signal logging.
Change the simulation stop time.
The test harness customization function runs as a test harness post-create callback or post-rebuild callback. To customize a test harness using a callback function:
Create the callback function.
In the function, use the Simulink® programmatic interface to script the commands to customize the test harness. For more information, see the functions listed in Programmatic Model Editing.
Specify the function as the post-create or post-rebuild callback:
For a new test harness,
If you are using the UI, enter the function name in the Post-create callback method or Post-rebuild callback method in the Advanced Properties of the harness creation dialog box.
If you are using
sltest.harness.create, specify the
function as the
For an existing test harness,
If you are using the UI, enter the function name in Post-rebuild callback method in the harness properties dialog box.
If you are using
sltest.harness.set, specify the
function as the
Another way to customize test harnesses is by setting your own defaults for creating harnesses. For more information, see Create Test Harnesses and Select Properties.
The callback function declaration is
myfunis the function name and
xis a struct of information about the test harness automatically created when the test harness uses the callback. You can choose the function and argument names.
For example, define a harness callback function
function harnessCustomization(harnessInfo) % Script commands here to customize your test harness. end
harnessInfois the struct name and
harnessCustomizationis the function name. When the create or rebuild operation calls
harnessInfois populated with information about the test harness, including handles to the test harness model, main model, and blocks in the test harness.
For example, using
harnessCustomization as a callback for the
following test harness:
harnessInfo with handles to three sources, one sink, the
main model, harness model, harness owner, component under test, and conversion
harnessInfo = struct with fields: MainModel: 2.0001 HarnessModel: 1.1290e+03 Owner: 17.0001 HarnessCUT: 201.0110 DataStoreMemory:  DataStoreRead:  DataStoreWrite:  Goto:  From:  GotoTag:  SimulinkFunctionCaller:  SimulinkFunctionStub:  Sources: [1.1530e+03 1.1540e+03 1.1550e+03] Sinks: 1.1630e+03 AssessmentBlock:  InputConversionSubsystem: 1.1360e+03 OutputConversionSubsystem: 1.1560e+03 CanvasArea: [215 140 770 260]
Use the struct fields to customize the test harness. For example:
To add a Constant block named
to the test harness, get the name of the test harness model, then use the
harnessName = get_param(harnessInfo.HarnessModel,'Name'); block = add_block('simulink/Sources/Constant',... [harnessName '/ConstInput']);
To get the port handles for the component under test, get the
'PortHandles' parameter for
CUTPorts = get_param(harnessInfo.HarnessCUT,'PortHandles');
To get the simulation stop time for the test harness, get the
'StopTime' parameter for
st = get_param(harnessInfo.HarnessModel,'StopTime');
To set a
16 second simulation stop time for the test
harness, set the
'StopTime' parameter for
To list the harness information for your test harness:
In the callback function, add the line
Create or rebuild a test harness using the callback function.
When you create or rebuild the test harness, the harness information structure contents are displayed on the command line.
assignin('base','a',2); a = evalin('base','a');
For parallel execution, post-load and cleanup callbacks are evaluated on the parallel MATLAB worker, where the callbacks have their own base workspaces. Variables created in a test case pre-load callback and test file and test suite setup callbacks are also available in the parallel MATLAB worker base workspace. Prior to execution, the base workspace variables are transferred from the client MATLAB to the parallel MATLAB workers.
This example harness callback function connects a Constant block to the third component input of this example test harness.
The function follows the procedure:
Get the harness model name.
Add a Constant block.
Get the port handles for the Constant block.
Get the port handles for the input conversion subsystem.
Get the handles for lines connected to the input conversion subsystem.
Delete the existing Inport block.
Delete the remaining line.
Connect a new line from the Constant block to input
3 of the input conversion subsystem.
function harnessCustomization(harnessInfo) % 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: delete_block(harnessInfo.Sources(3)); delete_line(icsLineHandles.Inport(3)); % Connect the Constant block to the input % conversion subsystem: add_line(harnessInfo.HarnessModel,constPorts.Outport,... icsPorts.Inport(3),'autorouting','on'); end
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.
In this example, you create a test harness for the
Roll Reference subsystem.
Get Path to the Harness Customization Function
cbFile = 'harnessSourceLogCustomization.m';
The Customization Function and Test Harness Information
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:
As an alternative to including output logging code in the callback, you can use Log Output Signals in the Create New Harness dialog box, or use
'LogHarnessOutputs',true as an input to
sltest.harness.create. These options log all component under test output signals in the test harness and return test results for those signals.
harnessSourceLogCustomization 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
Sources and sinks
The harness owner in the main model
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
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'); sltest.harness.open('RollAutopilotMdlRef/Roll Reference','LoggingHarness');