0
0
SimulinkHow-ToBeginner · 4 min read

How to Use Simulink Test: Step-by-Step Guide

To use Simulink Test, create test cases that define inputs and expected outputs for your Simulink model, then run these tests to verify model behavior. You can manage tests using test suites and generate reports to analyze results.
📐

Syntax

The basic workflow in Simulink Test involves these steps:

  • Create Test File: A container for test cases and test suites.
  • Define Test Cases: Specify inputs, expected outputs, and test criteria.
  • Run Tests: Execute test cases on your Simulink model.
  • Analyze Results: View pass/fail status and detailed logs.

Each test case can be created programmatically or using the Simulink Test Manager app.

matlab
import simulinktest.*

% Create a test file
testFile = sltest.testmanager.TestFile.create('MyTestFile.mldatx');

% Add a test suite
testSuite = testFile.addTestSuite('MyTestSuite');

% Add a test case
testCase = testSuite.addTestCase('MyTestCase');

% Set model to test
testCase.Model = 'mySimulinkModel';

% Define inputs and expected outputs
inputData = struct('time', [0 1 2], 'signals', struct('values', [0;1;2], 'dimensions', 1));
testCase.Inputs = inputData;

% Define criteria (e.g., signal tolerance)
testCase.addSignalComparison('OutputSignal', 'ExpectedOutputSignal', 'Tolerance', 0.01);

% Save test file
testFile.save();
💻

Example

This example shows how to create a simple test case for a model named mySimulinkModel that tests if the output signal matches expected values within a tolerance.

matlab
import simulinktest.*

% Create test file and suite
file = sltest.testmanager.TestFile.create('ExampleTest.mldatx');
suite = file.addTestSuite('ExampleSuite');

% Add test case
case1 = suite.addTestCase('TestCase1');
case1.Model = 'mySimulinkModel';

% Define input signal
input.time = (0:0.1:1)';
input.signals.values = sin(2*pi*input.time);
input.signals.dimensions = 1;
case1.Inputs = input;

% Define expected output signal
expectedOutput.time = input.time;
expectedOutput.signals.values = sin(2*pi*input.time);
expectedOutput.signals.dimensions = 1;

% Add signal comparison criterion
case1.addSignalComparison('yout', expectedOutput, 'Tolerance', 1e-3);

% Save and run tests
file.save();
results = file.run();

% Display results
disp(results.getSummary());
Output
Test Summary: Total Tests: 1 Passed: 1 Failed: 0 Warnings: 0
⚠️

Common Pitfalls

  • Not specifying the model name: Always set testCase.Model to the correct Simulink model.
  • Incorrect input signal format: Inputs must be structured with time and signals fields.
  • Missing criteria: Without criteria like signal comparisons, tests won't validate outputs.
  • Not saving test files: Always save your test files before running tests.
matlab
import simulinktest.*

% Wrong: Missing model name
file = sltest.testmanager.TestFile.create('BadTest.mldatx');
suite = file.addTestSuite('Suite');
case1 = suite.addTestCase('Case1');
% case1.Model = 'myModel'; % Missing model assignment

% Right: Assign model
case1.Model = 'myModel';
📊

Quick Reference

Here is a quick summary of key commands and concepts in Simulink Test:

Command/ConceptDescription
sltest.testmanager.TestFile.create('filename')Create a new test file
testFile.addTestSuite('name')Add a test suite to the test file
testSuite.addTestCase('name')Add a test case to the suite
testCase.Model = 'modelName'Assign the Simulink model to test
testCase.Inputs = inputStructSet input signals for the test case
testCase.addSignalComparison(signalName, expectedSignal, 'Tolerance', value)Add output verification criteria
testFile.save()Save the test file
testFile.run()Run all tests in the test file
results.getSummary()Get a summary of test results

Key Takeaways

Create test files, suites, and cases to organize your Simulink tests.
Always assign the correct model and define inputs and expected outputs.
Use signal comparison criteria to validate model outputs automatically.
Save your test files before running tests to avoid losing changes.
Analyze test results using summaries and detailed reports for verification.