$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-testingGenerate and run MATLAB unit tests using matlab.unittest and matlab.uitest. Parameterized tests, fixtures, mocking, coverage analysis, CI/CD with buildtool, app testing with gestures. Use when creating tests, writing test classes, running test suites, checking coverage, testing a
| 1 | # Testing |
| 2 | |
| 3 | Generate, structure, and run MATLAB unit tests using the `matlab.unittest` framework. Covers class-based tests, parameterized testing, fixtures, mocking, coverage analysis, CI/CD integration, and app testing via MCP. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - User asks to write tests for a MATLAB function or class |
| 8 | - User wants to run an existing test suite |
| 9 | - User needs coverage analysis or CI/CD configuration |
| 10 | - Test-driven development — writing tests before implementation |
| 11 | - Testing App Designer apps with programmatic gestures (see [reference/app-testing-guidance.md](reference/app-testing-guidance.md)) |
| 12 | |
| 13 | ## When NOT to Use |
| 14 | |
| 15 | - Testing Simulink models — use Simulink test skills |
| 16 | - Performance benchmarking — use profiling workflows |
| 17 | |
| 18 | ## Must-Follow Rules |
| 19 | |
| 20 | - **Present a test plan first** — For non-trivial test suites, propose test methods and edge cases for user approval before writing code |
| 21 | - **Always use class-based tests** — Every test file must inherit from `matlab.unittest.TestCase`. Never use script-based tests |
| 22 | - **No logic in test methods** — No `if`, `switch`, `for`, or `try/catch`. Follow **Arrange-Act-Assert**. If a test needs conditionals, split into separate methods |
| 23 | - **Test public interfaces, not implementation** — Never test private methods directly |
| 24 | - **Execute via MCP** — Use `run_matlab_test_file` or `evaluate_matlab_code` to run tests |
| 25 | |
| 26 | ## Workflow |
| 27 | |
| 28 | ### Simple tests (clear behavior, limited scope) |
| 29 | 1. Briefly state what you'll test (methods + key edge cases) |
| 30 | 2. Write the test file after user confirms |
| 31 | |
| 32 | ### Standard tests (large codebase, multiple files) |
| 33 | 1. **Gather requirements** — Code to test, expected behaviors, error conditions, scope, dependencies |
| 34 | 2. **Present test plan** — List test methods, edge cases, parameterization strategy for approval |
| 35 | 3. **Implement** — Write tests following the patterns below |
| 36 | 4. **Run** — Execute via `run_matlab_test_file` MCP tool |
| 37 | 5. **Check coverage** — Identify untested paths, add tests |
| 38 | |
| 39 | ## Key Functions |
| 40 | |
| 41 | | Category | Functions | Purpose | |
| 42 | |----------|-----------|---------| |
| 43 | | Equality | `verifyEqual`, `verifyNotEqual` | Compare values (use `AbsTol` for floats) | |
| 44 | | Boolean | `verifyTrue`, `verifyFalse` | Check logical conditions | |
| 45 | | Size/type | `verifySize`, `verifyClass`, `verifyEmpty` | Structural checks | |
| 46 | | Errors | `verifyError` | Confirm error is thrown with correct ID | |
| 47 | | Warnings | `verifyWarning`, `verifyWarningFree` | Check warning behavior | |
| 48 | | Infra | `runtests`, `TestSuite`, `TestRunner` | Run and organize tests | |
| 49 | | Coverage | `CodeCoveragePlugin`, `CoverageResult` | Measure test coverage | |
| 50 | |
| 51 | ### Qualification Levels |
| 52 | |
| 53 | | Level | On failure | When to use | |
| 54 | |-------|-----------|-------------| |
| 55 | | `verify` | Continues test | Default — most assertions | |
| 56 | | `assert` | Stops current test | Setup validation | |
| 57 | | `fatal` | Stops entire suite | Environment preconditions | |
| 58 | | `assume` | Skips test | Conditional execution (e.g., toolbox check) | |
| 59 | |
| 60 | ## Patterns |
| 61 | |
| 62 | ### Basic Test Class |
| 63 | |
| 64 | ```matlab |
| 65 | classdef computeAreaTest < matlab.unittest.TestCase |
| 66 | %computeAreaTest Tests for the computeArea function. |
| 67 | |
| 68 | methods (Test) |
| 69 | function testSquare(testCase) |
| 70 | result = computeArea(5, 5); |
| 71 | testCase.verifyEqual(result, 25); |
| 72 | end |
| 73 | |
| 74 | function testFloatingPoint(testCase) |
| 75 | result = computeArea(1/3, 3); |
| 76 | testCase.verifyEqual(result, 1, AbsTol=1e-12); |
| 77 | end |
| 78 | |
| 79 | function testNegativeInputErrors(testCase) |
| 80 | testCase.verifyError( ... |
| 81 | @() computeArea(-1, 5), 'computeArea:negativeInput'); |
| 82 | end |
| 83 | end |
| 84 | end |
| 85 | ``` |
| 86 | |
| 87 | ### Parameterized Tests |
| 88 | |
| 89 | Parameterize only when assertion logic is identical across all cases — only the data varies. Use struct for readable test names: |
| 90 | |
| 91 | ```matlab |
| 92 | classdef unitConverterTest < matlab.unittest.TestCase |
| 93 | |
| 94 | properties (TestParameter) |
| 95 | conversionCase = struct( ... |
| 96 | 'freezing', struct('input', 0, 'expected', 32), ... |
| 97 | 'boiling', struct('input', 100, 'expected', 212), ... |
| 98 | 'bodyTemp', struct('input', 37, 'expected', 98.6)); |
| 99 | end |
| 100 | |
| 101 | methods (Test) |
| 102 | function testCelsiusToFahrenheit(testCase, conversionCase) |
| 103 | result = celsiusToFahrenheit(conversionCase.input); |
| 104 | testCase.verifyEqual(result, conversionCase.expected, AbsTol=1e-10); |
| 105 | end |
| 106 | end |
| 107 | end |
| 108 | ``` |
| 109 | |
| 110 | For advanced parameterization (combinations, dynamic parameters, `ClassSetupParameter`), see [reference/parameterized-tests-guidance.md](reference/parameterized-tests-guidance.md). |
| 111 | |
| 112 | ### Setup, Teardown, and Fixtures |
| 113 | |
| 114 | Prefer `addTeardown` o |