How to Do Regression Testing in Simulink: Step-by-Step Guide
To do
regression testing in Simulink, use the Simulink Test toolbox to create test cases and harnesses that run your model with known inputs and compare outputs to baseline results. This ensures your model behaves consistently after changes by automatically detecting differences.Syntax
Regression testing in Simulink mainly involves these components:
Test Harness: An isolated environment to run tests on a model or subsystem.Baseline: A saved set of expected outputs from a previous test run.Test Manager: Tool to create, run, and manage test cases.verifyEqualorcomparefunctions: Used to compare current outputs with baselines.
Typical workflow syntax:
1. Create test harness:sltest.harness.create(model, subsystem)
2. Run test and capture output:simOut = sim(model)
3. Compare output to baseline:verifyEqual(testCase, simOut.yout, baselineOutput)
matlab
import sltest model = 'myModel'; subsystem = 'myModel/mySubsystem'; # Create test harness sltest.harness.create(model, subsystem); # Run simulation simOut = sim(model); # Compare output to baseline (example) # verifyEqual(testCase, simOut.yout, baselineOutput);
Example
This example shows how to create a test harness, run a simulation, save baseline data, and then run regression testing by comparing new results to the baseline.
matlab
model = 'simpleModel'; subsystem = 'simpleModel/Subsystem1'; % Create test harness sltest.harness.create(model, subsystem); % Run simulation and save baseline output simOut = sim(model); baselineOutput = simOut.get('yout'); save('baselineData.mat', 'baselineOutput'); % Later: Load baseline and run regression test load('baselineData.mat', 'baselineOutput'); newSimOut = sim(model); newOutput = newSimOut.get('yout'); % Compare outputs if isequal(newOutput, baselineOutput) disp('Regression test passed: Outputs match baseline.'); else disp('Regression test failed: Outputs differ from baseline.'); end
Output
Regression test passed: Outputs match baseline.
Common Pitfalls
- Not isolating tests: Running tests without a test harness can cause interference from other model parts.
- Ignoring baseline updates: Always update baselines intentionally; accidental changes can mask real issues.
- Comparing raw floating-point data directly: Use tolerance-based comparison to avoid false failures due to minor numerical differences.
- Not automating tests: Manual testing is error-prone; use
Simulink Testautomation features.
matlab
%% Wrong: Direct equality check without tolerance if isequal(newOutput, baselineOutput) disp('Test passed'); else disp('Test failed'); end %% Right: Use tolerance for numeric comparison tolerance = 1e-6; diff = abs(newOutput - baselineOutput); if all(diff < tolerance) disp('Test passed with tolerance'); else disp('Test failed'); end
Quick Reference
Key steps for regression testing in Simulink:
- Create a
test harnessfor the model or subsystem. - Run simulation and save outputs as
baseline. - After changes, rerun tests and compare outputs to baseline.
- Use tolerance-based comparison for numeric data.
- Automate tests with
Simulink Testfor repeatability.
Key Takeaways
Use test harnesses to isolate and run regression tests in Simulink.
Save baseline outputs to compare against future simulation results.
Use tolerance-based comparisons to handle floating-point differences.
Automate regression tests with Simulink Test for consistent validation.
Update baselines only when model changes are verified and intentional.