flutterdartflutter-testflutterdriver

Access flutter tests result in tearDownAll


I need to access the tests result in tearDownAll callback. I want to check if any of the tests failed during the execution.

tearDownAll(() async {
  final success = // code here
});

I researched classes like Invoker, Declarer, Engine, LiveTestController and test_core.dart file. Seems like this is the code that does what I need.

var success = await runZoned(() => Invoker.guard(engine.run),
    zoneValues: {#test.declarer: _globalDeclarer});
if (success) return null;
print('');
unawaited(Future.error('Dummy exception to set exit code.'));

Unfortunately the engine is not accessible from outside. I've also not found API for checking the tests result in test_core library.

Is there a way to check if any of previous test has failed? I'd like use this inside a tearDownAll() or test() functions of a test group.


Solution

  • I found a possible solution to my question, that requires some extra stuff in the test class.

    Basically you have to check every single test result in tearDown and keep track of them, then in tearDownAll you can check if any of the test has failed.

    import 'package:test_api/src/backend/invoker.dart';
    import 'package:test_api/src/backend/state.dart' as test_api;
    
    final failedTests = [];
    
    tearDown(() {
      if (Invoker.current.liveTest.state.result == test_api.Result.error) {
        failedTests.add(Invoker.current.liveTest.individualName);
      }
    });
    
    tearDownAll(() {
      if (failedTests.isNotEmpty) {
        // do stuff
      }
    });