Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/matlab-software-development/matlab-write-tests

matlab-write-tests

Verified

Generate 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.

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.

----

finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
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: 198kUpdated Claude CodeCodexVerifiedInstall