Running the same flutter test after each other. 1 succeeds and 1 fails

Viewed 1703

Im starting with creating Flutter tests and running into a problem with 2 tests that are exactly the same, but only 1 succeeds.

Gist: https://gist.github.com/Mooibroek/bd6c02e5b56e4b03409ee1839a2e0b23

The second one fails with this error:

The following TestFailure object was thrown running a test:
  Expected: exactly one matching node in the widget tree
  Actual: ?:<zero widgets with widget matching predicate (Closure: (dynamic) => bool) (ignoring
  offstage widgets)>
   Which: means none were found but one was expected

When the exception was thrown, this was the stack:
#4      main.<anonymous closure> (file:///Users/nicepants/Projects/flutter/generic_flutter/test/register_test.dart:38:5)
<asynchronous suspension>
#5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:108:25)

Line 38 is the line where we assert if the button is found. I added most of the related code in the GIST, let me know if you need more.

Is it in any way possible that there is some hidden state left from the previous test? Or is this not suppose to work.

1 Answers

I'm using a similar setup where I just duplicated my testWidgets(). There shouldn't be ay issues with referencing the same Widget on the test since the second testWidgets() initializes a new MyApp on tester.pumpWidget(). Though I'm using integration_test and flutter driver to launch my tests.

The tests should run the app on the emulator or physical device if you're using one. Check if the screen that appears on the second testWidgets() is correct. Tap the expected Widget to be found on screen in the emulator. This should show logs on how the tapped Widget can be found in the test. The logs should look similarly to this

Test logs

Related