Is test order deterministic when running Flutter tests?

688 views Asked by At

I have an integration test file with 3 test cases. I want to be sure that the third test case is always executed last (otherwise other test cases will fail) – how can I accomplish this?

Are the test cases always executed in the order in which they're defined?

I know that test cases depending on one another is a bad pattern, but let's forget it for the matter of this discussion (I need to define 3 test cases in a single .dart file because flutter test integration_test takes quite a long time to run a single Dart test file).

Here's the code:

// integration_test/login_test.dart

import 'package:flutter_test/flutter_test.dart';

void main() {
  group('login screen', () {
    testWidgets(
      'shows proper error message when email has incorrect format',
      (tester) async {
        // test code
      },
    );

    testWidgets(
      'shows error message when password is incorrect',
      (tester) async {
        // test code
      },
    );

    testWidgets(
      'logs in with correct credentials',
      (tester) async {
        // test code
      },
    );
  });
}

If the third test case ("logs in with correct credentials") gets executed as the first or second, then the other test case(s) will fail because the app will already be in the logged-in state.

Is there a way to enforce the order in which tests are executed?

1

There are 1 answers

0
Bartek Pacia On BEST ANSWER

The flutter test (and dart test) have the --test-randomize-ordering-seed argument. By default, it is not set, which means that test run in the order in which they're defined.

The below is copied from flutter test --help:

--test-randomize-ordering-seed    

The seed to randomize the execution order of test cases 
within test files. Must be a 32bit unsigned integer or 
the string "random", which indicates that a seed should
be selected randomly. By default, tests run in the order
they are declared.