Search code examples
flutterdartunit-testingtestingautomated-tests

Execution order confusion in Flutter tests: Future<void> runs before test groups


I have a test folder in my flitter project which contains the following :

group ('beneficiary methods tests', () {

test('insert beneficiary test',()async{…}

test('update beneficiary test',()async{…}

test('get beneficiary test',()async{…}

});

await deleteDatabase('testingDB');

What I’m expecting is to run the code line by line , meaning that the deletion of the database will be after each test’s execution , however this is not happening the tests depend’s on the database and they’re failing with the following error

SafLiteFfiException(error,
Bad state: This database
has already been closed})

Moving the await deleteDatabase('testingDB'); above the group solved the error but I still want to understand the reason of this behavior


Solution

  • test registers a test callback. It does not execute the test immediately.

    If you have code that you want to be executed after all tests have run, then you must register that code as a tearDownAll callback. If you want that to happen after all tests in a group, register your tearDownAll callback within that group:

    group('...', () {
      test('...', () async {
        // ...
      });
    
      test('...', () async {
        // ...
      });
    
      tearDownAll(() async {
        await deleteDatabase('testingDB');
      });
    });