Flutter のテスト実行(flutter test)前後に処理を挟むことができる仕組みが提供されています。
test/ ディレクトリの下に flutter_test_config.dart を作りましょう。コードの中身は次の通りにします。
// test/flutter_test_config.dart
import 'dart:async';
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
await testMain();
}
testMain() の部分でテストが実行されています。コードを次のように変更してみましょう。
import 'dart:async';
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
print("Hello from testExecutable() in flutter_test_config.dart");
await testMain();
}
flutter test すると、テスト実行前に print() が実行されていることが確認できます。
このように、全体のテストを実行する前後で実行したい処理はここで記述しておくと良きです。
参考
- flutter_test library - Dart API