Skip to main content
skill Claude Code CX Codex Cursor GitHub Copilot

dart-add-unit-test — Claude Code skill: what it does, how to install

Writes Dart and Flutter unit tests with package:test: files under test/, mockito mocks when dependencies get in the way, and a rerun loop until the suite is green.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add dart-lang/skills --skill dart-add-unit-test -a claude-code
Open on GitHub

What dart-add-unit-test does

The skill gives the agent a fixed routine for Dart and Flutter testing. Test files sit in test/ and mirror the lib/ tree, names end in _test.dart, and the cases are built on package:test: group() for shared context, test() for a case, expect() with matchers like equals() or throwsA(), setUp() and tearDown() for fixtures. Async checks use plain async/await since the runner waits on the returned Future.

It also ships a seven-step checklist that runs from creating the file to executing the suite, with a feedback loop in the middle: run, read the stack trace, fix the code or the assertion, run again. Two examples come along, one plain suite and one mockito setup using @GenerateNiceMocks. Mocking has a prerequisite — the mock file is produced by dart run build_runner build, and skipping that leaves the *.mocks.dart import broken.

Pick the runner that matches the project. dart test for pure Dart, flutter test for Flutter. Integration tests have to be pointed at their directory explicitly, as dart test integration_test or flutter test integration_test, because the default runner ignores that folder. Worth checking before you wonder why the report shows no tests.

What it helps with

  • cover a new function in lib/src/ with tests
  • pin a bug down with a regression case
  • keep test files mirroring the lib/ layout
  • swap an HTTP client for a mock built with package:mockito
  • trace a failing run back through the stack trace

Example prompts for the agent

  • Write unit tests for `lib/src/parser.dart` with `package:test` and put them in `test/src/parser_test.dart`
  • Add a test that reproduces this bug, then run `flutter test` until it passes
  • Mock `ApiClient` with `@GenerateNiceMocks` and assert that `fetchData()` parses the response

How to install dart-add-unit-test

1Skills CLI (Vercel)

npx skills add dart-lang/skills --skill dart-add-unit-test -a claude-code

2Manually: put the files in place

~/.claude/skills/dart-add-unit-test/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/dart-add-unit-test/

From the same set7

The whole set →

Similar

libfuzzer trailofbits A skill that walks the agent through libFuzzer: the harness, the -fsanitize=fuzzer build, corpus handling, campaign triage. For C/C++ built with Clang. Claude Code CX Codex Cursor +1 5,200installs burpsuite-project-parser trailofbits Explores and queries Burp Suite project files from the command line. It extracts proxy history, scanner findings, and HTTP traffic without opening the desktop app. Claude Code CX Codex Cursor +1 5,100installs wycheproof trailofbits Validates cryptographic implementations using Project Wycheproof test vectors. It helps your agent spot known vulnerabilities and edge cases across AES, RSA, and ECDSA. Claude Code CX Codex Cursor +1 5,100installs review-and-ship cursor This skill automates code review before shipping: checks for bugs, tests, and intent fit, then commits and opens a PR. Use it to verify your work is ready before merge. Claude Code CX Codex Cursor +1 2,500installs prek j178 Guides agents in configuring and running Git hooks with prek, a Rust alternative to pre-commit. Useful when you need speed, workspace support, or native TOML configuration. Claude Code CX Codex Cursor +1 728installs testing-setup Google LLC Analyzes your Android app's testing strategy and installs libraries, infrastructure, and frameworks for unit tests, UI tests, screenshot tests, and end-to-end tests. Claude Code CX Codex Cursor +1 2,500installs improve-codebase-architecture Matt Pocock This skill scans a codebase for architectural friction, then builds a visual HTML report with refactoring candidates. Use it before a deeper architecture review. Claude Code 1,163,035installs design-taste-frontend Leonxlnx This skill directs the design of public pages and work screens, then checks the result in a browser through screenshots. Use it when an interface feels generic or unfinished. Claude Code CX Codex Cursor +1 673,100installs

Questions about dart-add-unit-test

How to install dart-add-unit-test in Claude Code?

Run npx skills add dart-lang/skills --skill dart-add-unit-test -a claude-code. The skill file lands at ~/.claude/skills/dart-add-unit-test/SKILL.md.

What does dart-add-unit-test do?

It walks the agent through writing tests on package:test for functions, methods and classes: file placement, grouping, fixtures and a run-fix-rerun loop until the suite passes.

Do I need anything extra for mocks?

Yes, package:mockito plus build_runner, because the mock code is generated with dart run build_runner build. Without that step the *.mocks.dart import will not compile.

Source text

SKILL.md · BSD-3-Clause
---
name: dart-add-unit-test
description: Write and organize unit tests for functions, methods, and classes using `package:test`. Use when creating new logic or fixing bugs to ensure code remains correct and regression-free.
metadata:
  model: models/gemini-3.1-pro-preview
  last_modified: Mon, 03 Aug 2026 21:51:24 GMT
---
# Testing Dart and Flutter Applications

## Contents
- [Structuring Test Files](#structuring-test-files)
- [Writing Tests](#writing-tests)
- [Executing Tests](#executing-tests)
- [Test Implementation Workflow](#test-implementation-workflow)
- [Examples](#examples)

## Structuring Test Files
Organize test files to mirror the `lib` directory structure to maintain predictability.

* Place all test code within the `test` directory at the root of the package.
* Append `_test.dart` to the end of all test file names (e.g., `lib/src/utils.dart` should be tested in `test/src/utils_test.dart`).
* If writing integration tests, place them in an `integration_test` directory at the root of the package.

## Writing Tests
Utilize `package:test` as the standard testing library for Dart applications.

* Import `package:test/test.dart` (or `package:flutter_test/flutter_test.dart` for Flutter).
* Group related tests using the `group()` function to provide shared context.
* Define individual test cases using the `test()` function.
* Validate outcomes using the `expect()` function alongside matchers (e.g., `equals()`, `isTrue`, `throwsA()`).
* Write asynchronous tests using standard `async`/`await` syntax. The test runner automatically waits for the `Future` to complete.
* Manage test setup and teardown using `setUp()` and `tearDown()` callbacks.
* If testing code that relies on dependency injection, use `package:mockito` alongside `package:test` to generate mock objects, configure fixed scenarios, and verify interactions.

## Executing Tests
Select the appropriate test runner based on the project type and test location.

* If working on a pure Dart project, execute tests using the `dart test` command.
* If working on a Flutter project, execute tests using the `flutter test` command.
* If running integration tests, explicitly specify the directory path, as the default runner ignores it: `dart test integration_test` or `flutter test integration_test`.

## Test Implementation Workflow

Follow this sequential workflow when implementing new test suites. Copy the checklist to track your progress.

### Task Progress
- [ ] 1. Create the test file in the `test/` directory, ensuring the `_test.dart` suffix.
- [ ] 2. Import `package:test/test.dart` and the target library.
- [ ] 3. Define a `main()` function.
- [ ] 4. Initialize shared resources or mocks using `setUp()`.
- [ ] 5. Write `test()` cases grouped by functionality using `group()`.
- [ ] 6. Execute the test suite using the appropriate CLI command.
- [ ] 7. **Feedback Loop**: Run test -> Review stack trace for failures -> Fix implementation or assertions -> Re-run until passing.

## Examples

### Standard Unit Test Suite
Demonstrates grouping, setup, synchronous, and asynchronous testing.

```dart
import 'package:test/test.dart';
import 'package:my_package/calculator.dart';

void main() {
  group('Calculator', () {
    late Calculator calc;

    setUp(() {
      calc = Calculator();
    });

    test('adds two numbers correctly', () {
      expect(calc.add(2, 3), equals(5));
    });

    test('handles asynchronous operations', () async {
      final result = await calc.fetchRemoteValue();
      expect(result, isNotNull);
      expect(result, greaterThan(0));
    });
  });
}
```

### Mocking with Mockito
Demonstrates configuring a mock object for dependency injection testing.

```dart
import 'package:test/test.dart';
import 'package:mockito/mockito.dart';
import 'package:mockito/annotations.dart';
import 'package:my_package/api_client.dart';
import 'package:my_package/data_service.dart';

// Generate the mock using build_runner: dart run build_runner build
@GenerateNiceMocks([MockSpec<ApiClient>()])
import 'data_service_test.mocks.dart';

void main() {
  group('DataService', () {
    late MockApiClient mockApiClient;
    late DataService dataService;

    setUp(() {
      mockApiClient = MockApiClient();
      dataService = DataService(apiClient: mockApiClient);
    });

    test('returns parsed data on successful API call', () async {
      // Configure the mock
      when(mockApiClient.get('/data')).thenAnswer((_) async => '{"id": 1}');

      // Execute the system under test
      final result = await dataService.fetchData();

      // Verify outcomes and interactions
      expect(result.id, equals(1));
      verify(mockApiClient.get('/data')).called(1);
    });
  });
}
```

© dart-lang · BSD-3-Clause · dart-lang/skills

See also

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor