matlab/matlab-agentic-toolkit / skills-catalog/matlab-software-development/matlab-write-tests
matlab-write-tests
VerifiedGenerate and structure MATLAB unit tests using matlab.unittest and matlab.uitest features, including class-based tests, parameterized testing, fixtures, mocking, and app testing with gestures. Use when writing, generating, or adding tests, creating test classes, adding test methods, parameterizing tests, setting up fixtures, mocking dependencies, or testing App Designer apps. Do NOT use for running tests, collecting coverage, or CI/CD configuration.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 11.2 KB |
| manifest.yaml | config | 495 B |
| references/app-testing-guidance.md | markdown | 7.3 KB |
| references/baseline-tests-guidance.md | markdown | 6.0 KB |
| references/constraints-guidance.md | markdown | 2.2 KB |
| references/fixtures-guidance.md | markdown | 2.7 KB |
| references/mocking-guidance.md | markdown | 3.3 KB |
| references/parameterized-tests-guidance.md | markdown | 3.6 KB |
references/fixtures-guidance.md
View on GitHub# Fixtures
## Setup/Teardown Hierarchy
ClassSetup → (MethodSetup → Test → MethodTeardown) × N tests → ClassTeardown
**Prefer `addTeardown`** over `TestMethodTeardown`/`TestClassTeardown` blocks. Reserve those blocks for unconditional cleanup unrelated to a specific resource.
## addTeardown
Call immediately after creating an artifact or changing state. Runs in LIFO order even if setup fails partway through.
```matlab
function testWithFile(testCase)
filename = tempname;
fid = fopen(filename, 'w');
testCase.addTeardown(@() fclose(fid));
testCase.addTeardown(@() delete(filename));
% ... use file ...
end
```
## Built-in Fixtures
| Fixture | Constructor | Purpose |
|---|---|---|
| `WorkingFolderFixture` | `()` or `('WithSuffix', name)` | Temp working folder |
| `TemporaryFolderFixture` | `()` or `('WithSuffix', name)` | Temp folder (doesn't cd into it) |
| `PathFixture` | `(folderPath)` or `(folderPath, IncludingSubfolders=true)` | Add folder to MATLAB path |
| `CurrentFolderFixture` | `(folderPath)` | Change current folder |
| `ProjectFixture` | `(projectFolder)` | Open and close a MATLAB project |
| `EnvironmentVariableFixture` | `(varName, value)` | Set environment variable |
| `SuppressedWarningsFixture` | `(warningID)` | Suppress specific warning |
## Choosing Scope
Use the narrowest scope that works. Only promote to `SharedTestFixtures` when multiple test classes need the same expensive resource.
| Scope | Where |
|---|---|
| Across test classes (DB, server) | `SharedTestFixtures` class attribute |
| One class (path, large data) | `applyFixture` / `addTeardown` in `TestClassSetup` |
| Each method | `applyFixture` / `addTeardown` in `TestMethodSetup` |
| Single test (temp file, figure) | `applyFixture` / `addTeardown` in test method |
## Custom Fixtures
```matlab
classdef DatabaseFixture < matlab.unittest.fixtures.Fixture
properties
Connection
end
methods
function setup(fixture)
fixture.Connection = database('testdb', 'user', 'pass');
fixture.addTeardown(@() close(fixture.Connection));
end
end
end
```
If the fixture accepts configuration, override `isCompatible` so the framework knows when two instances can share state.
Usage as shared fixture:
```matlab
classdef (SharedTestFixtures = {DatabaseFixture}) ...
DatabaseTest < matlab.unittest.TestCase
methods (Test)
function testQuery(testCase)
fixture = testCase.getSharedTestFixtures();
conn = fixture{1}.Connection;
result = fetch(conn, 'SELECT * FROM users');
testCase.verifyNotEmpty(result);
end
end
end
```
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |