0
0
SimulinkHow-ToBeginner · 4 min read

How to Create Test Harness in Simulink: Step-by-Step Guide

To create a test harness in Simulink, right-click the subsystem or model you want to test and select Test Harness > Create for Subsystem. This creates an isolated environment where you can add test inputs and outputs without affecting the main model.
📐

Syntax

Creating a test harness in Simulink involves using the context menu commands on a subsystem or model. The main steps are:

  • Right-click the subsystem or model block.
  • Select Test Harness > Create for Subsystem or Create for Model.
  • Configure the harness options such as name and location.

This creates a separate test environment linked to the original component.

matlab
model = 'vdp';
load_system(model);

% Right-click the subsystem block in the Simulink GUI
% Select 'Test Harness > Create for Subsystem'

% Alternatively, create a harness programmatically:
harnessName = sltest.harness.create(model, 'Subsystem');

% Open the harness
open_system(harnessName);
Output
Harness 'vdp/Subsystem_harness' created and opened.
💻

Example

This example shows how to create a test harness programmatically for a subsystem named 'Subsystem' inside the 'vdp' model. The harness allows you to test the subsystem independently.

matlab
model = 'vdp';
load_system(model);

% Create a test harness for the 'Subsystem' block
harnessName = sltest.harness.create(model, 'Subsystem');

% Open the test harness
open_system(harnessName);

% Add a test input block inside the harness
add_block('simulink/Sources/Step', [harnessName '/StepInput']);

% Connect the Step input to the harness input
add_line(harnessName, 'StepInput/1', 'In1/1');
Output
Harness 'vdp/Subsystem_harness' created and opened. Block 'StepInput' added and connected to harness input.
⚠️

Common Pitfalls

  • Not selecting the correct subsystem or model before creating the harness can create an unrelated test environment.
  • Forgetting to connect test input blocks to the harness inputs results in no stimulus to the subsystem.
  • Modifying the harness does not affect the original model until you update or apply changes.
  • Closing the harness without saving can lose your test setup.
matlab
try
    % Wrong: Creating harness on a non-existent block
    sltest.harness.create('vdp', 'NonExistentBlock');
catch ME
    disp(['Error: ' ME.message]);
end

% Correct way:
harnessName = sltest.harness.create('vdp', 'Subsystem');
open_system(harnessName);
Output
Error: Block 'vdp/NonExistentBlock' not found. Harness 'vdp/Subsystem_harness' created and opened.
📊

Quick Reference

ActionCommand / Menu PathDescription
Create Harness (GUI)Right-click subsystem > Test Harness > Create for SubsystemCreates isolated test environment for subsystem
Create Harness (Code)sltest.harness.create(model, blockPath)Programmatically create test harness
Open Harnessopen_system(harnessName)Open the created test harness window
Add Test Inputadd_block('simulink/Sources/Step', harnessName + '/StepInput')Add input stimulus block inside harness
Connect Inputadd_line(harnessName, 'StepInput/1', 'In1/1')Connect input block to harness input port

Key Takeaways

Use the right-click menu or sltest.harness.create to create a test harness in Simulink.
Test harnesses isolate subsystems for focused testing without changing the main model.
Always connect test input blocks to harness inputs to provide stimulus.
Save your harness changes to avoid losing test configurations.
Use the Quick Reference table to remember common commands and steps.