Java 是否有一个工具可以查找哪些代码行被特定的 JUnit 测试所测试?

发布于 2024-09-18 09:21:19 字数 226 浏览 5 评论 0原文

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

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

发布评论

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

评论(4

颜漓半夏 2024-09-25 09:21:19

有一个名为 Jester 的开源突变测试工具,它可以更改源代码行,然后运行测试,并报告您的测试是否通过。听起来比代码覆盖工具更接近您正在寻找的东西。

Jester 是一个测试器,用于测试 java JUnit 测试(Pester 用于 Python PyUnit 测试)。它会修改您的源代码,运行测试并报告测试是否通过(尽管代码发生了更改)。这可能表明缺少测试或冗余代码。

关于纯 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.

Jester is a test tester for testing your java JUnit tests (Pester is for Python PyUnit tests). It modifies your source code, runs the tests and reports if the tests pass despite the changes to the code. This can indicate missing tests or redundant code.

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

你爱我像她 2024-09-25 09:21:19

您正在寻找的内容可能被称为突变测试。虽然突变测试本身不会告诉您需要通过哪些代码行。突变测试的作用是修改您的源代码,寻找它可以对您的代码进行的更改,但您的测试仍然通过。例如,更改

if (a < b)

if (a >= b)

并查看测试是否仍然通过。这将突出您测试中的弱点。

另一个用于突变测试的 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

if (a < b)

to

if (a >= b)

and seeing if the test still passes. This will highlight weaknesses in your test.

Another java library for mutation testing is jumble.

洋洋洒洒 2024-09-25 09:21:19

我的大多数项目都使用 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

把人绕傻吧 2024-09-25 09:21:19

我喜欢 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.

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