Dart – How run a function after or before each test?

Issue

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?

Solution

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.

Answered By – Günter Zöchbauer

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

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