Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Generate 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 apps, or validating MATLAB code.
.claude/skills/matlab-matlab-testing/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 24% | 0% |
| case-02 | ✗→✓ | ▲ Improved | 59% | 0% |
| case-04 | ✗→✓ | ▲ Improved | 67% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 209% | 0% |
| case-07 | ✗→✓ | ▲ Improved | 169% | 0% |
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.
matlab.unittest.TestCase. Never use script-based testsif, switch, for, or try/catch. Follow Arrange-Act-Assert. If a test needs conditionals, split into separate methodsrun_matlab_test_file or evaluate_matlab_code to run testsrun_matlab_test_file MCP tool| Category | Functions | Purpose | |----------|-----------|---------| | Equality | verifyEqual, verifyNotEqual | Compare values (use AbsTol for floats) | | Boolean | verifyTrue, verifyFalse | Check logical conditions | | Size/type | verifySize, verifyClass, verifyEmpty | Structural checks | | Errors | verifyError | Confirm error is thrown with correct ID | | Warnings | verifyWarning, verifyWarningFree | Check warning behavior | | Infra | runtests, TestSuite, TestRunner | Run and organize tests | | Coverage | CodeCoveragePlugin, CoverageResult | Measure test coverage |
| Level | On failure | When to use | |-------|-----------|-------------| | verify | Continues test | Default — most assertions | | assert | Stops current test | Setup validation | | fatal | Stops entire suite | Environment preconditions | | assume | Skips test | Conditional execution (e.g., toolbox check) |
matlabclassdef computeAreaTest < matlab.unittest.TestCase %computeAreaTest Tests for the computeArea function. methods (Test) function testSquare(testCase) result = computeArea(5, 5); testCase.verifyEqual(result, 25); end function testFloatingPoint(testCase) result = computeArea(1/3, 3); testCase.verifyEqual(result, 1, AbsTol=1e-12); end function testNegativeInputErrors(testCase) testCase.verifyError( ... @() computeArea(-1, 5), 'computeArea:negativeInput'); end end end
Parameterize only when assertion logic is identical across all cases — only the data varies. Use struct for readable test names:
matlabclassdef unitConverterTest < matlab.unittest.TestCase properties (TestParameter) conversionCase = struct( ... 'freezing', struct('input', 0, 'expected', 32), ... 'boiling', struct('input', 100, 'expected', 212), ... 'bodyTemp', struct('input', 37, 'expected', 98.6)); end methods (Test) function testCelsiusToFahrenheit(testCase, conversionCase) result = celsiusToFahrenheit(conversionCase.input); testCase.verifyEqual(result, conversionCase.expected, AbsTol=1e-10); end end end
For advanced parameterization (combinations, dynamic parameters, ClassSetupParameter), see reference/parameterized-tests-guidance.md.
Prefer addTeardown over TestMethodTeardown blocks. Use PathFixture to add source folders:
matlabclassdef fileProcessorTest < matlab.unittest.TestCase methods (TestClassSetup) function addSourceToPath(testCase) srcFolder = fullfile(fileparts(fileparts(mfilename('fullpath'))), 'src'); testCase.applyFixture(matlab.unittest.fixtures.PathFixture(srcFolder, ... IncludingSubfolders=true)); end end methods (Test) function testProcessFile(testCase) tmpDir = string(tempname); mkdir(tmpDir); testCase.addTeardown(@() rmdir(tmpDir, 's')); testFile = fullfile(tmpDir, "data.csv"); writematrix(rand(10, 3), testFile); result = processFile(testFile); testCase.verifySize(result, [10 3]); end end end
For built-in fixtures, custom fixtures, and shared fixtures, see reference/fixtures-guidance.md.
Seed the RNG and restore it in teardown for reproducible tests:
matlabmethods (TestMethodSetup) function resetRandomSeed(testCase) originalRng = rng; testCase.addTeardown(@() rng(originalRng)); rng(42, "twister"); end end
Use TestTags for selective execution:
matlabmethods (Test, TestTags = {'Unit'}) function testFastCalculation(testCase) % ... end end methods (Test, TestTags = {'Integration', 'Slow'}) function testFullPipeline(testCase) % ... end end
Run by tag: runtests('tests', Tag='Unit') or runtests('tests', ExcludeTag='Slow').
Use the run_matlab_test_file MCP tool for test files. For inline runs with filtering:
matlabresults = runtests('tests'); % all tests in folder results = runtests('tests', Tag='Unit'); % by tag results = runtests('tests', Name='*Calculator*'); % by name pattern results = runtests('tests', UseParallel=true); % parallel execution results = runtests('tests', Strict=true); % warnings = failures
matlabdisp(results); for r = results([results.Failed]) fprintf('\nFAILED: %s\n', r.Name); disp(r.Details.DiagnosticRecord.Report); end
matlabimport matlab.unittest.TestRunner import matlab.unittest.plugins.CodeCoveragePlugin import matlab.unittest.plugins.codecoverage.CoverageResult import matlab.unittest.plugins.codecoverage.CoverageReport runner = TestRunner.withTextOutput; covFormat = CoverageResult; runner.addPlugin(CodeCoveragePlugin.forFolder('src', ... Producing=[covFormat, CoverageReport('coverage-report')])); results = runner.run(testsuite('tests')); covResults = covFormat.Result; disp(covResults);
For coverage gap analysis, use the printCoverageGaps script in reference/test-execution-guidance.md.
Use buildtool with a buildfile.m for CI pipelines. See reference/test-execution-guidance.md for buildfile.m templates and CI configs (GitHub Actions, Azure DevOps, GitLab CI).
For testing apps with programmatic UI gestures (press, choose, type, drag), see reference/app-testing-guidance.md.
Key points:
matlab.uitest.TestCase (not matlab.unittest.TestCase)drawnow after app creation, before first gestureuilabel.Text with char ('text'), not string ("text").Enable with matlab.lang.OnOffSwitchState.on/.offLoad these on demand — most tests only need what's in this file.
| Load when... | Reference | |---|---| | Tests need setup/teardown, temp dirs, path management, shared state | reference/fixtures-guidance.md | | Floating-point tolerance selection, constraint objects, custom constraints | reference/constraints-guidance.md | | Multiple parameters, dynamic parameters, combination strategies | reference/parameterized-tests-guidance.md | | Code depends on external services, needs mock objects or dependency injection | reference/mocking-guidance.md | | Running tests in CI, buildtool config, coverage gap analysis | reference/test-execution-guidance.md | | Testing App Designer apps with gestures, dialogs, async callbacks | reference/app-testing-guidance.md |
matlab.unittest.TestCase<functionName>Test.m and place in tests/ directoryverify qualifications by default — they let all tests run even if one failsAbsTol for every floating-point comparison — never rely on exact equalityaddTeardown for cleanup — it runs even if the test failsTestParameter for readable parameterized test namesrun_matlab_test_file MCP tool for automatic result captureCopyright 2026 The MathWorks, Inc.
Other measured skills in the registry, with their headline benchmark lift.