开发者

Is there a tool for Java which finds which lines of code are tested by specific JUnit tests?

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


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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜