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?