junit测试用例跑完后,是否应该删除与该测试用例相关的测试数据?
一个junit测试用例跑完后,是否应该删除与该测试用例相关的测试数据?
保留测试数据是否有助于开发人员调试代码?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为一个好的实践,测试用例必须在完成后删除其测试数据,以便下一个测试用例可以使用已知的初始数据库状态运行。
测试用例不应取决于运行顺序。
这也使得调试测试用例变得容易,因为它从已知的初始状态运行。
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.
是的,单元测试应该以“干净”的数据库、文件系统等开始和结束。每个测试都应该保留它发现的东西。
除此之外,这有助于重新运行——您可以一次又一次地重新运行测试。
然而,有时在开发和调试时禁用数据删除可能会很有用。
实现所有这一切需要一种真正的技巧,例如,在使用 Java、Spring 和数据库时,您可以使用 Spring 的事务管理来轻松地以零的努力回滚所有更改。
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.
我建议从清理和插入首选测试数据开始测试。然后保留数据库原样。
此方法的优点:
DbUnit 框架实际上是为了在 setUp() 方法中清理测试数据并将其插入到测试数据库中而构建的在每个测试方法之前。通过这种方法,建议为每个开发人员使用单独的数据库。
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:
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.