开发者

How to look for JUnit and TestNG tests with no assertions?

I've been able to figure out how to go about looking for tests that aren't run. Does anyone know how I can go about figuring out if a test that was run does开发者_运维技巧n't have an assertion?


I think you have two choices: at runtime, or using static analysis.

At runtime

You will be able to hook into your tests at runtime using JUnit's RunListener (TestNG has a similar abstraction), but how could you tell when assertions are made? Not straightforward, but this would be possible using a Java Agent. You can use a java agent and a library which does bytecode manipulation to alter the behaviour of "assertion" methods as they are loaded into the JVM. Your altered methods would have to call your RunListeners then make their assertions as ususal. You need to be able to list every single method which counts as an "assertion" (junit? testng? hamcrest?, etc?). Phew! I have done all this in the past, it was pretty fiddly but it does work.

Static analysis

In your case, static analysis would seem to be the better choice. You can scan the classpath for all @Test annotations (or TestNG equivalent) and check to see which of them call a method whose name starts "assert". I've used javaparser to do things similar to this and it works pretty well.

As mentioned in the other answer, be aware that there is always the implicit assertion that no exceptions are thrown. We do have some valid and useful tests in our suites which have no explicit assertions in them because of this.


Simpler approach could have been been JUnit and TestNG supporting assert counter which can print not just the number of tests but also asserts used in the test methods.

I have written a wrapper for Assert class which overrides all the methods and has a counter..

You could also try to use IntelliJ's Structured Search

Regards, Nagendra


I'd say your best bet would be a regex search or some kind of static analysis. Whichever, it has to be able to identify test methods and determine whether they contain an assertion, where an assertion is some assert* call, a mock verification, and/or an "expected exception" clause at the very least. All of those constitute some kind of "assertion".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜