Flutter:lib 配下のテストファイルを実行する

Flutter:lib 配下のテストファイルを実行する

July 22, 2022

通常の場合 Flutter のテストを実施するには次のコマンドを実行します。

flutter test

この場合は test/ ディレクトリ配下のテストファイルが実行されます。

The test runner considers any file that ends with _test.dart to be a test file. If you don’t pass any paths, it will run all the test files in your test/ directory, making it easy to test your entire application at once.

しかし、テストファイルを test/ 以外の別の場所に配置したいケースもあると思います。例えば私は、テストファイルは被テストファイルの隣(=同じディレクトリの中)に配置したい派です。

このように、テストファイルをデフォルトの test/ 以外に配置したときは次のように対象ディレクトリを指定します。これで lib/ 以下の _test.dart ファイルが実行されます。

flutter test lib

A single test file can be run just using dart test path/to/test.dart (as of Dart 2.10 - prior sdk versions must use pub run test instead of dart test).

Many tests can be run at a time using dart test path/to/dir.

参考 #