junit测试用例跑完后,是否应该删除与该测试用例相关的测试数据?

发布于 2024-09-05 14:51:44 字数 71 浏览 2 评论 0原文

一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡的优雅 2024-09-12 14:51:44

作为一个好的实践,测试用例必须在完成后删除其测试数据,以便下一个测试用例可以使用已知的初始数据库状态运行。
测试用例不应取决于运行顺序。
这也使得调试测试用例变得容易,因为它从已知的初始状态运行。

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.

诗笺 2024-09-12 14:51:44

是的,单元测试应该以“干净”的数据库、文件系统等开始和结束。每个测试都应该保留它发现的东西。

除此之外,这有助于重新运行——您可以一次又一次地重新运行测试。

然而,有时在开发和调试时禁用数据删除可能会很有用。

实现所有这一切需要一种真正的技巧,例如,在使用 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.

夏尔 2024-09-12 14:51:44

我建议从清理和插入首选测试数据开始测试。然后保留数据库原样。

此方法的优点:

  • 轻松手动验证您的服务是否按预期运行。
  • 测试数据在启动之前不会被其他服务损坏,因为该作业是在测试之前立即完成的。

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:

  • 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文