Testing an assertion

55 views
Skip to first unread message

Andreas Sumerauer

unread,
Nov 7, 2018, 7:09:22 AM11/7/18
to Flutter Dev
Is it possible to test an assertion with the flutter test framework?
In particular: Is there a way to make a test pass when an assertion fails (given this is the expected outcome)?
Thanks!

Jonas Sander

unread,
Nov 7, 2018, 3:50:26 PM11/7/18
to Flutter Dev
I'm writing it from memory, but it should go something like this:

bool assertion = false;
assert(() {assertion = true; return true;}())
return assertion;

Andreas Sumerauer

unread,
Nov 9, 2018, 4:03:33 AM11/9/18
to Flutter Dev

Thanks, but that is not what I meant I'm afraid.

It appears that one has to insert a try block within the test body and catch that.
Here is an example:

main()
test('south > north (must throw AssertionError)', () {
bool assertionError = false;
try {
final LatLngBounds bounds = LatLngBounds(
southwest: const LatLng(30.0, 20.0),
northeast: const LatLng(-10.0, 40.0),
);
}
on AssertionError catch (e){
assertionError = true;
}
expect(assertionError, true);
});
}

It would be nice to have a testAssert function for that specific case (or maybe even better a more general testException).

Jonas Sander

unread,
Nov 9, 2018, 8:38:37 AM11/9/18
to Andreas Sumerauer, Flutter Dev
You may want to look at this.
https://stackoverflow.com/q/49523786/8121937
You can also specify the Excepetion that should be thrown.


--
You received this message because you are subscribed to a topic in the Google Groups "Flutter Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/flutter-dev/fICU_VEOO7M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andreas Sumerauer

unread,
Nov 12, 2018, 8:40:04 AM11/12/18
to Flutter Dev
Thanks!
That is exaclty what I was looking for.
Reply all
Reply to author
Forward
0 new messages