Java 是否有一个工具可以查找哪些代码行被特定的 JUnit 测试所测试?
是否有一个 Java 工具,给定一组 JUnit 测试和一个要测试的类,它会告诉您测试测试了该类的哪些行? IE。需要在场才能成功运行测试。我的意思不是“代码覆盖率”,它只告诉您一行是否被执行,而是比这更强大的东西:测试通过是否需要该行?
我经常注释掉一行代码并运行测试来查看测试是否真的在测试该行代码。我认为这可以通过半智能工具自动完成(例如,IDE 之类的工具可以计算出可以从方法中删除哪些内容,同时保持其可编译性)。
Is there a tool for Java which, given a set of JUnit tests, and a class to test, will tell you which lines of the class are tested by the tests? ie. required to be present for the tests to run successfully. I don't mean "code coverage", which only tells you whether a line is executed, but something stronger than that: Is the line required for the test to pass?
I often comment out a line of code and run a test to see if the test really is testing that line of code. I reckon this could be done automatically by a semi-smart tool (eg. something like an IDE that can work out what can be removed from a method whilst keeping it compilable).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个名为 Jester 的开源突变测试工具,它可以更改源代码行,然后运行测试,并报告您的测试是否通过。听起来比代码覆盖工具更接近您正在寻找的东西。
关于纯 TDD 项目中是否需要这些工具的讨论,Jester 项目网页上有一个链接,指向关于在 TDD 会话期间编写的代码中使用 Jester 的好处的帖子(Uncle Bob 臭名昭著的保龄球 TDD 示例)。
There's an open source mutation-testing tool called Jester that changes the lines of your source code, then runs your tests, and reports whether your tests passed anyway. Sounds closer to what you're looking for than code coverage tools.
WRT the discussion about whether these tools are needed in a pure TDD project, there is a link on the Jester project webpage to a posting about the benefits of using Jester on code written during a TDD session (Uncle Bob's infamous bowling TDD example).
您正在寻找的内容可能被称为突变测试。虽然突变测试本身不会告诉您需要通过哪些代码行。突变测试的作用是修改您的源代码,寻找它可以对您的代码进行的更改,但您的测试仍然通过。例如,更改
为
并查看测试是否仍然通过。这将突出您测试中的弱点。
另一个用于突变测试的 Java 库是 jumble。
What you are looking for might be referred to as mutation testing. While mutation testing won't tell you which lines of code are required to pass, per se. What mutation testing does is modify your source code looking for changes it can make to your code but your test still passes. E.g. changing
to
and seeing if the test still passes. This will highlight weaknesses in your test.
Another java library for mutation testing is jumble.
我的大多数项目都使用 emma 。我将它包含在我的 ant 构建文件中,它为报告生成 html 文件
我读过但尚未尝试过的另外两个覆盖项目是 三叶草 或 cobertura
I use emma for most of my projects. i included it in my ant build file and it generates html files for the reports
two other coverage projects i read about but haven't tried yet are clover or cobertura
我喜欢 cobertura,因为生成的报告是恕我直言最漂亮的。而且它有自己的蚂蚁目标!
与 emma 相比,它不仅具有行覆盖,而且还具有分支覆盖,这常常会产生误导。
I love cobertura, because the generated reports are IMHO the most beautiful. And it has its own ant target!
In comparison to emma, it has also branch coverage, not only line coverage, which is misleading very often.