dart-add-unit-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
testdirectory at the root of the package. - Append
_test.dartto the end of all test file names (e.g.,lib/src/utils.dartshould be tested intest/src/utils_test.dart). - If writing integration tests, place them in an
integration_testdirectory at the root of the package.
Writing Tests
Utilize package:test as the standard testing library for Dart applications.
- Import
package:test/test.dart(orpackage:flutter_test/flutter_test.dartfor Flutter).
More from dart-lang/skills
dart-fix-runtime-errors
Uses get_runtime_errors and lsp to fetch an active stack trace, locate the failing line, apply a fix, and verify resolution via hot_reload.
2.1Kdart-run-static-analysis
Execute `dart analyze` to identify warnings and errors, and use `dart fix --apply` to automatically resolve mechanical lint issues. Use during development to ensure code quality and before committing changes.
2.1Kdart-use-pattern-matching
Use switch expressions and pattern matching where appropriate
2.0Kdart-resolve-package-conflicts
Workflow for fixing package version conflicts. Use this when `pub get` fails due to incompatible package versions.
2.0Kdart-collect-coverage
Collect coverage using the coverage packge and create an LCOV report
2.0Kdart-generate-test-mocks
Define and generate mock objects for external dependencies using `package:mockito` and `build_runner`. Use when unit testing classes that depend on complex external services like APIs or databases.
2.0K