单元测试中用于检查代码覆盖率的反思

发布于 2024-09-03 00:36:19 字数 295 浏览 2 评论 0原文

这是场景。我有 VO(值对象)或 DTO 对象,它们只是数据的容器。当我将它们分开并保存到数据库(由于很多原因)无法优雅地映射到 VO 时,我想测试每个字段是否已在数据库中成功创建并成功读回重建 VO。

有没有办法可以测试我的测试是否涵盖了 VO 中的每个字段?我有一个关于使用反射来迭代 VO 字段作为解决方案的一部分的想法,但也许你们之前已经解决了这个问题?

当我在 VO 中添加字段时,我希望此测试失败,并且不记得在测试中添加对其的检查。

开发环境: 使用 JUnit、Hibernate/Spring 和 Eclipse

Here's the scenario. I have VO (Value Objects) or DTO objects that are just containers for data. When I take those and split them apart for saving into a DB that (for lots of reasons) doesn't map to the VO's elegantly, I want to test to see if each field is successfully being created in the database and successfully read back in to rebuild the VO.

Is there a way I can test that my tests cover every field in the VO? I had an idea about using reflection to iterate through the fields of the VO's as part of the solution, but maybe you guys have solved the problem before?

I want this test to fail when I add fields in the VO, and don't remember to add checks for it in my tests.

dev environment:
Using JUnit, Hibernate/Spring, and Eclipse

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

月竹挽风 2024-09-10 00:36:19

保持简单:为每个 VO/DTO 编写一个测试:

  1. 用测试数据填充 VO/DTO
  2. 保存它
  3. (可选:使用纯 JDBC 检查所有内容是否已在数据库级别正确保存)
  4. 加载
  5. 检查加载的 VO/DTO 与原始代码匹配

生产性代码将会发展,并且也需要维护测试。恕我直言,使测试尽可能简单,即使它们是重复的,也是最好的方法。 过度设计测试或测试框架本身以使测试通用(例如,通过反射读取字段并自动填充 VO/DTO)会导致几个问题:

  1. 编写测试所花费的时间较长,
  2. 错误可能会在测试本身中引入
  3. 测试的维护更加困难,因为它们是更复杂的
  4. 测试,更难发展,例如,通用代码可能不适用于与其他略有不同的新型 VO/DTO,稍后将介绍(它是只是一个例子)
  5. 测试不能轻易地用作生产代码如何工作的示例

测试和生产代码本质上非常不同。在生产代码中,您尝试避免重复并最大限度地重用。生产代码可能很复杂,因为它是经过测试的。另一方面,您应该尝试尽可能简单地进行测试,并且重复是可以的。如果重复的部分被破坏,测试无论如何都会失败。

当生产代码发生更改时,可能需要对多个测试进行轻微更改。问题是测试被视为无聊代码。但我认为他们就应该这样。

如果我问错了你的问题,请告诉我。

Keep it simple: write one test per VO/DTO:

  1. fill the VO/DTO with test data
  2. save it
  3. (optional: check everything has been correctly save at the database level, using pure JDBC)
  4. load it
  5. check that the loaded VO/DTO and the original one matches

Productive code will evolve and tests will need to be maintained as well. Making tests the simplest as possible, even if they are repetitive, is IMHO the best approach. Over-engineering the tests or testing framework itself to make tests generic (e.g. by reading fields with reflection and filling VO/DTO automatically) leads to several problems:

  1. time spent to write the test is higher
  2. bug might be introduced in the test themselves
  3. maintenance of the test is harder because they are more sophisticated
  4. tests are harder to evolve, e.g. the generic code will maybe not work for new kinds of VO/DTO that differ slightly from the other and will be introduced later (it's just an example)
  5. tests can not be used easily as example of how the productive code works

Test and productive code are very different in nature. In productive code, you try to avoid duplication and maximize reuse. Productive code can be complicated, because it is tested. On the other hand, you should try to have tests as simple as possible, and duplication is ok. If a duplicated portion is broken, the test will fail anyway.

When productive code change, this may require several tests to be trivially changed. With the problem that tests are seen as boring piece of code. But I think that's the way they should be.

If I however got your question wrong, just let me know.

回忆那么伤 2024-09-10 00:36:19

我建议使用 cobertura 来完成此任务。
运行测试后,您将获得完整的代码覆盖率报告,如果您使用 cobertura-check ant 任务,您可以添加覆盖率检查并使用属性 haltonfailure< 停止 ant 调用/代码>。

I would recommend cobertura for this task.
You will get a complete code coverage report after you run your tests and if you use the cobertura-check ant task you can add checks for the coverage and stop the ant call with the property haltonfailure.

秋意浓 2024-09-10 00:36:19

您可以将其作为 VO 验证的一部分。如果在使用 getter 时未设置字段,则可能会引发异常。

You could make it part of the validation of the VO. If the fields aren't set when you use a getter it can throw an exception.

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