Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 280
Joseph Avatar asked Jun 10 '10 07:06

Joseph


2 Answers

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.

like image 146
Inv3r53 Avatar answered Oct 07 '22 21:10

Inv3r53


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.

like image 24
Brian Avatar answered Oct 07 '22 22:10

Brian