3

Is it good practice to clean up your DB after running integration tests (which tests APIs that put data into that DB)? I want to have a cleanup method after my integration tests run which will access the DB through a data access layer but is this recommended?

2 Answers 2

12

You need to do it before every test, obviously, in order for the test not to be affected by other tests.

On the other hand, keep the data in the database after the test in order to make it easier to debug a test, especially the one which fails.

1

No, you shouldn't write tests that do cleanup afterwards, for multiple reasons:

  1. None of the major test frameworks provide a general solution for cleanup after tests. Junit and Cucumber have after-test hooks, but people generally don't use them. Do a Google search, and you'll find people generally do not recommend it.
  2. Don't do after-test cleanup unless you have to. In my experience, 99% of the time, a BeforeTest setup works for cleaning up data.
  3. If you do cleanup after tests, make sure that your tests still work in multi-threaded mode. Often times, cleanup after tests might cause other test to fail when tests are ran in multi-threaded mode.
  4. "Data testers" tend to have a "cleanup afterwards attitude" that is a side-effect of architectural decisions making the database not very testable without causing problems. Resetting the TEST database back to a known state, such as once per quarter, might resolve these concerns?
1
  • 1
    those aren't reasons Commented Nov 20, 2023 at 1:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.