1

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?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
jxmorris12
  • 1,262
  • 4
  • 15
  • 27

1 Answers1

1

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 */
  });
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 2
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – doydoy Jul 31 '17 at 13:21