35

I'm using the Dart test package: https://pub.dartlang.org/packages/test

Often, I want to run some function before or after each tests in my test file. Does the test package provide something for this?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Kasper
  • 12,594
  • 12
  • 41
  • 63

1 Answers1

67

add a setUp(() { add your code here}) before your test() function. There is also a tearDown() which is run after each test.

If you add the setUp function at top level in main it is run for every test, if you put it inside a group it is run for every test in that group. You can have setUp/tearDown on more than one level at the same time.
tearDown is executed in any case (like finally) no matter if the test fails or succeeds.

Recently setUpAll() and tearDownAll() was added to do some set up and tear down once before and after all tests.

Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567