Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit @Ignore usefulness?

In JUnit you can use @Ignore before methods to tell the test runner to automatically skip those tests. From what I can gather, this is really only a convenient way to document/mark off incomplete/no longer functional tests that you want to get back to later.

Am I correct in saying then, that at runtime there is no difference between an @Ignore test, a method with no annotation, and a commented out method? (Assuming these tests are all self contained.) Is there some way to obtain a list of the ignored test cases in JUnit on Netbeans? If not, how much usefulness does the @Ignore tag really have, since it might be more useful to fail the test so it's not overlooked?

like image 826
donnyton Avatar asked Jul 15 '11 12:07

donnyton


People also ask

What does @ignore do in JUnit?

A test method annotated with @Ignore will not be executed. If a test class is annotated with @Ignore, then none of its test methods will be executed.

What is an advantage of using JUnit?

JUnit is elegantly simple. It is less complex and takes less time. JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.

How do I use annotation JUnit ignore?

JUnit 4 – @Ignore Annotation The JUnit 4 @Ignore annotation could be applied for a test method, to skip its execution. In this case, you need to use @Ignore with the @Test annotation for a test method you wish to skip. The annotation could also be applied to the test class, to skip all the test cases under a class.


2 Answers

Am I correct in saying then, that at runtime there is no difference between an @Ignore test, a method with no annotation, and a commented out method?

An @Ignored method can be found via reflection. A method with no annotation can't (or, to be precise, it can't be identified with certainty as an ignored test method), and a commented out method does not even get into the bytecode.

Albeit I don't think there would be much practical value in finding @Ignored methods runtime, it may be useful to generate statistics / reports.

how much usefulness does the @Ignore tag really have

One thing I can think of is searchability. You can easily identify all @Ignore annotations in the source code, while unannotated or commented out tests are not so simple to find.

it might be more useful to fail the test so it's not overlooked?

If you want (and can) fix it right away, it is fine to have it fail. There are cases when you can't, but you still want to method to be around, precisely so that it does not get forgotten. Then @Ignore makes sense.

like image 129
Péter Török Avatar answered Sep 24 '22 00:09

Péter Török


  • A method with no annotation most likely is a utility method, likely used by other methods in the test.

  • A commented-out method is one that is completely hidden.

  • But you might use @Ignore to indicate that you are temporarily disabling the test. If you have a large and complicated piece of code that is in transition, you might want to temporarily disable some tests while that code is failing, especially if the mechanism running that code is run as part of some larger regression suite. The @Ignore attribute would be a reminder to you (or your QA team) that there is still more to do.

    Remember that @Ignore'd tests are still live code, and can be seen and even executed via reflection, which commented-out code cannot be. And it also means when you refactor other code, they are also updated, and syntax breakage in an API will show there. So even tests with @Ignore set can actively contribute to quality of code.

  • JUnit 4 TestRunners will show @Ignore'd tests as "skipped" so you can see in the end how many tests passed/failed/skipped.

Although your copy of Netbeans may not take full advantage of @Ignore, I can assure you that it is not the only consumer of JUnit tests.

like image 25
lavinio Avatar answered Sep 23 '22 00:09

lavinio