views:

47

answers:

3

After a junit test case ran, should delete test data related with this test case?

Will keeping the test data help the developers to debug the code?

Thanks Joseph

+2  A: 

Yes unit tests should begin and end with "clean" database, file system, etc. Each test should leave things as it found them.

Apart from anything else this helps with re-runnability - you can keep re-running your tests time after time.

Sometimes however when you're developing and debugging it can be useful to disable data-removal.

There is a real craft to achieving all of this, for example when working with Java, Spring, and databases, you can use Spring's transaction management to simply roll back all your changes with zero effort.

Brian
+4  A: 

As a good practice the test case must remove its test data after it is finished so that next test case can run with a known initial db state. The test cases should not depend upon the order of run. This also makes debugging a test case easy since it runs from a known initial state.

daedlus
+2  A: 

I recommend to start the test with cleaning up and inserting the preferred test data. And leave the database as is afterwards.

Advantages with this approach:

  • Easy to manually verify that your services works as expected.
  • The test data isn't corrupted by other services before start, since the job is done immediately before the test.

The DbUnit framework is actually built to clean and insert the test data into your test database in the setUp() method before each test method. With this approach, it is recommended with a separate database for each developer.

Espen