Dart Add Unit Test

作者 flutter0ef3972f93e2無授權條款3K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫2 天前更新

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.

AI 產生的概覽

指導使用 package:test 撰寫與組織 Dart 和 Flutter 單元測試,涵蓋結構、模擬與執行。

功能
此技能提供使用 package:test 為 Dart 和 Flutter 的函式、方法與類別建立單元測試的說明。內容涵蓋對應 lib 目錄的測試檔案配置、命名慣例、分組、斷言、非同步測試、setup 與 teardown,以及使用 package:mockito 進行模擬。它也說明如何用 dart test 或 flutter test 執行測試,並附有逐步實作流程與程式碼範例。
適用情境
適用於在 Dart 或 Flutter 程式碼中新增邏輯或修正錯誤時,希望取得無迴歸的測試涵蓋率。也適合需要測試組織、斷言、模擬或執行測試套件指引的專案。
執行需求
需要 Dart 或 Flutter 專案並包含 package:test 函式庫(Flutter 使用 package:flutter_test);模擬需要 package:mockito 和 build_runner。測試執行使用 dart test 或 flutter test 命令列。不附帶指令碼,僅為說明文件。

Testing Dart and Flutter Applications

Contents

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);    });  });}

來源與署名

來源:flutter/agent-plugins位於skills/dart-add-unit-test提交0ef3972

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架