Two different setUp methods for the same tests

Issue

I currently have two groups of tests that are identical in every way except the setUp() method call. I want to simplify the code so that the set of tests is only defined once but each group runs its own setUp() method and then the identical set of tests.

Currently my code looks something like this:

group('test things while a flag is turned off', () {
  setUp(() {
    global_flags.flag = false;
  }
  /* run lots of tests */
})

group('test things while a flag is turned on' () {
  setUp(() {
    global_flags.flag = true;
  }
  /* run the exact same tests */
}

How can I consolidate this code?

Solution

You can register the same tests multiple times and add parameters to customize execution:

main() {
  myTests(true);
  myTests(false);
}

myTests(bool global_flags) {
  group('test things while a flag is turned ${global_flags ? 'on' : 'off'}' () {
    /* run the exact same tests */
  });
}

Answered By – Günter Zöchbauer

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

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