TE

MATLAB Testing

MATLAB testing conventions using the built-in matlab.unittest framework

Details

Language / Topic
matlabMATLAB
Category
Testing

Rules

balanced
- Create test classes that inherit from `matlab.unittest.TestCase` and prefix test methods with `test` so the runner discovers them automatically.
- Use `testCase.verifyEqual(actual, expected, 'AbsTol', 1e-9)` for floating-point comparisons instead of `==` — specify tolerance explicitly.
- Run tests with `results = runtests('tests/')` and check `[results.Failed]` in CI — a non-empty result means the build should fail.
- Use `TestMethodSetup` and `TestMethodTeardown` methods to create and destroy fixtures (temp files, figures) for each test method independently.
- Use `matlab.unittest.constraints.IsEqualTo` with `Within` tolerance constraint for structured numeric comparisons in assertions.
- Parameterize tests with `matlab.unittest.parameters.Parameter.optionList` to run the same test method with multiple input sets without duplicating code.
- Mock external dependencies with `matlab.unittest.TestCase.createMock` to isolate units under test from file I/O, network, and hardware.