Skip to main content

New site for Dart news and articles

For the latest Dart news, visit our new blog at .

New setUp/tearDown in Unit Test library

Posted by Seth Ladd

Graham Wheeler, an engineer on the Dart project, announced today:

Our unit tests now support setUp/tearDown functions. To use these you need to put the tests within a group(). Inside the group body, in addition to calling test() you can call setUp() and/or tearDown() with function arguments. The setUp function will be called before each test and tearDown after each test.

Usually you would set these up at the start of the group:

group('foo', () {
  setUp(() {...});
  tearDown(() {...});
  test(description, () {...});

but you can interlace them differently; each test() will use the most recently set values setUp/tearDown.

Whenever a new group is started these are reset. This applies for nested groups too. Nested groups do not inherit these functions or augment them; they get their own. This seemed the most flexible approach as chaining can always be done explicitly.

Another new (unrelated) change is that you can now pass a --filter=<string> argument to Only tests whose descriptions match the supplied string will be executed. The string gets wrapped in a RegExp so you can use flexible matching. This can be combined with other restrictions, so for example I could use:

tools/ --compiler=none --runtime=vm --filter=contains lib/unittest

to run all the tests in lib/unittest/test*.dart that have descriptions containing the word contains.

As always, you can join the Dart mailing list to send along your feedback. Thanks for trying Dart!