Testing applications related to programming design using MATLAB.
“The MATLAB syntax itself can be up and running in 1-2 weeks, but to make the testing professional, reusable, and embeddable in CI — it requires another 1-2 months to master the testing framework, reporting, and automation routines; overall, it is considered ‘easy to start, moderately difficult to master’.”
Here’s a quick guide to building a deliverable testing system from scratch, just follow the steps directly.
1. Difficulty and Time (Tested Personally)
Copy
| Phase | Content | Time Required | Difficulty |
|---|---|---|---|
| ① Syntax + Matrices | Variables, scripts, functions, plotting | 8-10 h | ★☆☆ |
| ② Testing Framework | matlab.unittest + mock + parameterized | 2-3 days | ★★☆ |
| ③ Automation | CLI + JUnit XML + Cobertura + CI (Jenkins/GitHub) | 3-5 days | ★★☆ |
| ④ Reporting | HTMLTestReport + PDF + Requirement Traceability | 1-2 days | ★★☆ |
| ⑤ Project Practice | Integrate ①-④ into a real algorithm/APP | 1-2 weeks | ★★☆ |
Conclusion: ① Very easy; ②-⑤ have patterns, just copy the templates; overall, it has 2 more pitfalls than Python testing (license + CLI), but the learning curve is still friendly.
2. Three-Step Quick Start (with Commands)
➊ Evening of Day 1: Write the First Function + Unit Test
matlab
Copy
%% Function to be tested power2.m
function y =power2(x)
y = x.^2;
end
%% Test file test_power2.m (class-based)
classdef test_power2 < matlab.unittest.TestCase
methods(Test)
function test_scalar(testCase)
act = power2(3);
testCase.verifyEqual(act, 9);
end
function test_vector(testCase)
act = power2([123]);
testCase.verifyEqual(act, [149]);
end
end
end
Run:
bash
Copy
matlab -batch "runtests('test_power2', 'ProduceJUnitReport', true)"
→ Generates power2_test.xml (CI recognizes it directly)
➋ Day 2: Parameterization + Coverage
matlab
Copy
classdef test_power2 < matlab.unittest.TestCase
properties(TestParameter)
x = {0, 1, -2, [12; 34]};
end
methods(Test, ParameterCombination='exhaustive')
function test_power2_param(testCase, x)
testCase.verifyEqual(power2(x), x.^2);
end
end
end
Coverage:
bash
Copy
matlab -batch "coverage = codecov(power2); disp(coverage)"
→ Outputs Cobertura XML for SonarQube to read.
➌ Day 3: One-Click HTML Report + CLI Embedded in CI
matlab
Copy
import matlab.unittest.TestRunner;
import matlab.unittest.plugins.HTMLTestReportPlugin;
runner = TestRunner.withTextOutput;
runner.addPlugin(HTMLTestReportPlugin.producing('./report.html'));
result = runner.run(packageFolder);
GitHub Action Snippet:
yaml
Copy
- name: Run MATLAB tests
uses: matlab-actions/run-tests@v1
with:
testResultsJUnit: test-results.xml
codeCoverageCobertura: coverage.xml
sourceFolder: src
Open <span>report.html</span> in the evening to see:
-
Each test case PASS/FAIL trace
-
Coverage heatmap
-
Requirement ↔ Test bidirectional trace (if you write the Requirement number in the test properties)
3. Common Pitfalls & Quick Solutions
Copy
| Pitfall | Quick Solution |
|---|---|
| No license | Use MATLAB Online (web version) or apply for a 30-day trial; use <span>-batch</span> in CI to avoid GUI pop-ups. |
| Want to test Simulink models | Use <span>sltest.testmanager</span> to drag test cases, which can also export JUnit + coverage. |
| Need to mock external hardware | Use <span>matlab.mock.TestCase</span> framework, or simply <span>assignin('base', 'hwRead', @mockRead)</span><span> to inject mock data.</span> |
| Need PDF reports | <span>exportHTMLReport</span> then use <span>html2pdf</span> in one command. |
4. Advanced Roadmap (1-2 Months)
-
Write requirement keywords into
<span>Test</span>tags → automatically generate traceability matrix (IEC-61508/ISO-26262 likes this). -
Use MATLAB Test Manager to graphically maintain test cases → non-programmers can also add or delete cases.
-
Integrate with Simulink Coverage and Polyspace → achieve a full process of model-code-coverage-static checks.
-
Link to Jenkins/SonarQube → automatically run regression, generate reports, and send emails on each push.
5. Summary in One Sentence
MATLAB testing is “simple syntax, mature framework, beautiful reports,” by following the above 3-day template, you can deliver a CI-reusable automated testing project; subsequently, add requirements, coverage, and safety standards, and you can reach enterprise delivery level in 1-2 months.