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/constraints-guidance.md

View on GitHub
# Constraints and Assertions

Advanced verification techniques, tolerances, and custom constraints. For basic assertion usage (`verify*`/`assert*`, `verifyError`, `AbsTol`/`RelTol` syntax), see SKILL.md.

## Floating-Point Comparisons

`AbsTol` and `RelTol` are only supported on `*Equal` methods.

### Choosing Tolerances

| Scenario | Recommended |
|----------|-------------|
| Small values near zero | `AbsTol` only |
| Large values | `RelTol` only |
| Mixed magnitudes | Both |
| Single precision | `AbsTol=1e-6`, `RelTol=1e-5` |
| Double precision | `AbsTol=1e-14`, `RelTol=1e-12` |

## Diagnostic Messages

Add diagnostics that describe what the assertion is checking — they should be meaningful regardless of whether the test passes or fails:

```matlab
testCase.verifyEqual(result, [1 2 3 4 5], ...
    "Output must be in ascending order.")
```

## Constraint Objects

Prefer informal APIs (`verifyEqual`, `verifyGreaterThan`, `verifySubstring`, etc.) over `verifyThat` with constraint objects. Only use `verifyThat` when no informal API exists.

### Informal API Mapping

Most constraints map directly to informal equivalents. Non-obvious mappings:

| Constraint class | Informal equivalent |
|---|---|
| `HasElementCount` | `verifyNumElements` |
| `ContainsSubstring` | `verifySubstring` |
| `MatchesRegexp` | `verifyMatches` |
| `Throws` | `verifyError` |
| `IssuesWarnings` | `verifyWarning` |
| `IssuesNoWarnings` | `verifyWarningFree` |

### When to Use `verifyThat`

No informal equivalent exists for these:

- `StartsWithSubstring`, `EndsWithSubstring`
- `IsFinite`, `IsReal`, `IsSameSetAs`
- `EveryElementOf(...)`, `AnyElementOf(...)`
- Boolean combinations (`&`, `|`, `~`)

```matlab
import matlab.unittest.constraints.*

testCase.verifyThat(value, IsEmpty | IsEqualTo(0));
testCase.verifyThat(x, IsGreaterThan(0) & IsLessThan(10));
testCase.verifyThat(array, EveryElementOf(IsGreaterThan(0)));
```

## Custom Constraints

Extend `matlab.unittest.constraints.Constraint` for domain-specific checks. Implement `satisfiedBy(~, actual)` (returns logical) and `getDiagnosticFor(constraint, actual)` (returns `StringDiagnostic`). Use via `testCase.verifyThat(value, MyConstraint)`.

----

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