My unit tests require a setup that needs to run asynchronously. That is, I need to wait for the setup to finish before the tests are run, but the setup deals with Futures.
Asked
Active
Viewed 3,135 times
2 Answers
15
With Dart M3, the setUp
function can optionally return a Future
. If setUp returns a Future, the unittest framework will wait for the Future to complete before running the individual test methods.
Here is an example:
group(('database') {
var db = createDb();
setUp(() {
return openDatabase()
.then((db) => populateForTests(db));
});
test('read', () {
Future future = db.read('foo');
future.then((value) {
expect(value, 'bar');
});
expect(future, completes);
});
});
Learn more about setUp.

Günter Zöchbauer
- 623,577
- 216
- 2,003
- 1,567

Seth Ladd
- 112,095
- 66
- 196
- 279
6
While the accepted answer by Seth is correct, the following example may be easier to understand and reuse. It returns a Future
and performs the setup in the Future's async worker function:
setUp(() {
return Future(() async {
await someFuture();
callSomeFunction();
await anotherFuture();
});
});
The test cases will be called after the last call to anotherFuture()
returns.

Ber
- 40,356
- 16
- 72
- 88