Running all unit tests in Dart

Issue

I’m working on a Flutter project and trying to find the best way to structure my tests. Typically I structure tests to mirror the directory structure of the main project which is being tested.

lib
 |models
 |providers
   |userprovider.dart
test
 lib
   |models
   |providers
     |userproviderShould.dart  

However, I’m having trouble figuring out if this approach is less than optimal for the Dart code. Each file in the test project seems to need to have a main method, which feels odd. I’m also not clear on how to run the entire test suite. The Flutter test running (flutter test) doesn’t seem to understand directories. Running flutter test test/lib/providers doesn’t work while flutter test test/lib/providers/userproviderShould.dart does. If it doesn’t understand directories it certainly doesn’t understand having to recurse into directories.

Is there a way to solve this that doesn’t involve either having to build a fragile entry point that manually includes all the rest of the tests or writing a shell script to go run each file individually?

Solution

If you want flutter test or pub run test to execute a file without manually passing its path as parameter to the command, then the file must:

  • be inside the /test folder
  • its name must be suffixed by _test.dart

Anything that does not have this _test will not be executed by the command line.

Answered By – Rémi Rousselet

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *