开发者

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 cod开发者_如何学Pythone?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜