Self-cleaning test


#1

What if we need a test to clean up a newly created data after its execution (no matter it failed or succeeded)?
You can’t make test to clean itself, because it can fail at some point without cleanup. So we can prepare a function for After method

let cleanup;

Scenario('Create an item', async I => {
   // do some actions to create an item on page
  await  I.click('Create'); // at this point an item is created so we need to prepare cleanup
  cleanup = () => {
    I.say('Cleaning up');
    // perfrom actions to clean up created item
  }
  // continue test
})

After(() => {
  if (cleanup) {
    cleanup(); // execute cleanup
    cleanup = null; // disable cleanup for next tests
  }
});


#2

Could a solution using CodeceptJS be used for a whole test suite rather than after an individual test ie I run all my tests and then commence cleanup or is that wondering into different territory?


#3

I’m using the method _afterSuite() in my helper. The method is called as the last step in My_test.js (which is actually a suite)

E.g.:

async _afterSuite() {
        const rest = this.helpers["REST"];

        // Delete user account from Firebase
        rest.sendPostRequest(`https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key=${this.getFirebaseWebApiKey()}`, JSON.stringify({
            "idToken": await this.getFirebaseIdToken()
        }));
}

It works also for tests run as run-multiple.


#4

@davert your example seems to overwrite the global after hook. Any idea how to implement the self cleaning test idea without doing so?


What stops you from contributing?
#5

I work on this idea a lot and spend huge time. the best case is to delete data before all test and after all test with bootstrap and teardown. hat way you test start and ends with know state.