AgentSkillsCN

managing-tests

运行、调试并修复 Flutter 测试,包括单元测试、Widget 测试以及 Patrol E2E 测试。适用于测试失败、代码变更后需要更新测试,或在创建新测试时使用。不适用于常规代码调试或功能实现。

SKILL.md
--- frontmatter
name: managing-tests
description: "Runs, debugs, and fixes Flutter tests including unit tests, widget tests, and Patrol E2E tests. Use when tests fail, need updating after code changes, or when creating new tests. Not for general code debugging or feature implementation."
argument-hint: "[test-file-path-or-pattern]"
disable-model-invocation: false

Managing Tests Skill

Purpose

This skill handles the complete lifecycle of test management:

  1. Run tests - Execute unit, widget, or Patrol E2E tests
  2. Analyze failures - Identify why tests fail (API changes, mocks outdated, logic errors)
  3. Fix tests - Update tests to match current implementation
  4. Refine test structure - Improve test organization and maintainability

Test Categories

Unit/Widget Tests (test/ directory)

  • Standard Flutter tests using flutter_test
  • Mock with mocktail (mockito-style API)
  • Run with: flutter test test/path/to/test.dart

Patrol E2E Tests (patrol_test/ directory)

  • Device/emulator automation tests
  • Critical: test_bundle.dart is auto-generated by patrol_cli
  • Run with: patrol test --target patrol_test/tests/file.dart

Common Issues & Solutions

1. Test API Mismatches

Symptom: "X positional arguments expected by Y, but Z found" Cause: Function signature changed, tests not updated Fix: Update test mocks to match new signatures

2. Mocktail argThat Not Found

Symptom: "The function 'argThat' isn't defined" Cause: Missing mockito-style matcher import Fix: Mocktail uses different matcher syntax - check current mock patterns in codebase

3. Patrol Import Errors

Symptom: "Target of URI doesn't exist: 'package:patrol/patrol_tester.dart'" Cause: test_bundle.dart uses runtime-only imports Fix: Already handled - patrol_test/** is excluded from analysis in analysis_options.yaml

4. Type Inference Warnings

Symptom: "AsyncValue<dynamic> should have explicit type arguments" Fix: Add explicit generic types to AsyncValue and AsyncData

Workflow

  1. Identify test type from file path
  2. Run failing test to see exact error
  3. Analyze error - is it mock setup, API change, or logic issue?
  4. Fix incrementally - update one test at a time
  5. Verify fix - run test again to confirm
  6. Apply dart fix - run dart fix --apply for auto-fixes
  7. Final check - dart analyze and flutter test

Key Commands

bash
# Run specific test
flutter test test/features/scanner_test.dart

# Run all tests
flutter test

# Run with verbose output
flutter test -v

# Run Patrol test
patrol test --target patrol_test/tests/full_cycle_test.dart

# Fix auto-fixable issues
dart fix --apply

# Check types
dart analyze

Code Patterns

Mock Setup with Mocktail

dart
class MockCatalogDao extends Mock implements CatalogDao {}

// In setUp:
when(() => mockDao.getProduct(any())).thenAnswer((_) async => mockResult);

Riverpod Provider Tests

dart
final container = ProviderContainer(
  overrides: [
    catalogDaoProvider.overrideWithValue(mockCatalogDao),
  ],
);

AsyncValue Type Safety

dart
// Instead of: AsyncValue<dynamic>
// Use: AsyncValue<ScannerState>

Files to Check

  • test/** - Unit and widget tests
  • patrol_test/tests/** - E2E test files
  • patrol_test/robots/** - Robot pattern page objects
  • pubspec.yaml - Test dependencies
  • analysis_options.yaml - Already excludes patrol_test/** from analysis

Output Format

After fixing tests, report:

  1. Files modified: List of test files updated
  2. Issues fixed: What specific problems were resolved
  3. Verification: Results of dart analyze and flutter test
  4. Remaining issues: Any tests still failing and why