If you are using JUnit 4, then you can use @Ignore annotation to disable a test case; what this means is that any method which is annotated by @Ignore, will not be executed by JUnit. Why would you want to disable a test? Well, It may be because that particular test is for functionality that is not yet implemented fully, and because of that your build is failing. Like that there could be some other scenarios where you would like to temporarily disable a test or a group of tests. From JUnit 4 onward test methods are annotated with @Test, so if they are also annotated with @Ignore will not be executed as tests.
Apart from disabling a single method, you can also disable all methods of a class by using @Ignore annotation at the class level. For example, If you annotate a class containing test methods with @Ignore and none of the containing tests will be executed.
Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and the number of tests that failed.
Also worth noting is that @Ignore annotation takes an optional default parameter if you want to record why a test is being ignored, very useful if you are not the only person who is working on that piece of code. If there is a reason to disable a test case, later that can be enabled if the reason is already sorted.
1) Ignoring a single test method in JUnit, just annotate method with @Ignore annotation.
2) Ignoring a single test method but with proper reason, annotate method with @Ignore annotation but also give String argument as reason.
3) Ignoring all methods of a test class in JUnit, just use @Ignore annotation at class level.
Now, let's see @Ignore annotation in action by writing a simple JUnit test class with three test methods. We will first run this program without disabling any method and then subsequently disable them one at a time. You can verify whether @Ignore is working properly or not by looking at the output.
First Run
You can see that all three test methods are executed. Now let's run them again but this time we will disable test2() by putting @Ignore annotation on top of it.
Second Run after Ignoring one test
You can see that only the first and third test methods ran but test2() didn't run because it was ignored. Now let's try using @Ignore annotation at the class level, this should disable whole test class i.e. no test method will execute.
Apart from disabling a single method, you can also disable all methods of a class by using @Ignore annotation at the class level. For example, If you annotate a class containing test methods with @Ignore and none of the containing tests will be executed.
Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and the number of tests that failed.
Also worth noting is that @Ignore annotation takes an optional default parameter if you want to record why a test is being ignored, very useful if you are not the only person who is working on that piece of code. If there is a reason to disable a test case, later that can be enabled if the reason is already sorted.
JUnit @Ignore Example
As I said in the first paragraph, you can use @Ignore to disable one or many tests in JUnit. You can use @Ignore with a method to disable just that method or you can use it at the class level to disable all methods of that class. Here are a few examples to demonstrate how you can use @Ignore annotation to ignore JUnit test cases.1) Ignoring a single test method in JUnit, just annotate method with @Ignore annotation.
@Ignore @Test public void testSomething() { System.out.println("Sorry this test will not run, its ignored"); }
2) Ignoring a single test method but with proper reason, annotate method with @Ignore annotation but also give String argument as reason.
@Ignore("not ready yet") @Test public void testOnething() { System.out.println("This method is ignored because its not ready yet"); }
3) Ignoring all methods of a test class in JUnit, just use @Ignore annotation at class level.
@Ignore public class Ignored { @Test public void test1() { System.out.println("Ignored test case 1"); } @Test public void test2() { System.out.println("Ignored test case 2"); } }
Now, let's see @Ignore annotation in action by writing a simple JUnit test class with three test methods. We will first run this program without disabling any method and then subsequently disable them one at a time. You can verify whether @Ignore is working properly or not by looking at the output.
First Run
import static org.junit.Assert.*; import org.junit.Ignore; import org.junit.Test; public class ABCTest { @Test public void test1() { System.out.println("First JUnit test from ABCTest class executed"); } @Test public void test2() { System.out.println("Second JUnit test from ABCTest class executed"); } @Test public void test3() { System.out.println("Third JUnit test from ABCTest class executed"); } } Output : First JUnit test from ABCTest class executed Third JUnit test from ABCTest class executed Second JUnit test from ABCTest class executed
You can see that all three test methods are executed. Now let's run them again but this time we will disable test2() by putting @Ignore annotation on top of it.
Second Run after Ignoring one test
import static org.junit.Assert.*; import org.junit.Ignore; import org.junit.Test; public class ABCTest { @Test public void test1() { System.out.println("First JUnit test from ABCTest class executed"); } @Ignore @Test public void test2() { System.out.println("Second JUnit test from ABCTest class executed"); } @Test public void test3() { System.out.println("Third JUnit test from ABCTest class executed"); } } Output : First JUnit test from ABCTest class executed Third JUnit test from ABCTest class executed
You can see that only the first and third test methods ran but test2() didn't run because it was ignored. Now let's try using @Ignore annotation at the class level, this should disable whole test class i.e. no test method will execute.
The third run - Ignoring whole test case file
import static org.junit.Assert.*; import org.junit.Ignore; import org.junit.Test; @Ignore public class JUnitTest { @Test public void test1() { System.out.println("First JUnit test from ABCTest class executed"); } @Ignore @Test public void test2() { System.out.println("Second JUnit test from ABCTest class executed"); } @Test public void test3() { System.out.println("Third JUnit test from ABCTest class executed"); } }
No test case ran. Despite not using @Ignore annotation with test1() and test3() those were not ran because whole class was ignored due to use of @Ignore annotation on top of class level.
That's all about how to disable JUnit tests temporarily in Java. You can disable a single test or a whole test class by using @Ignore annotation at method or class level. It's good to know that you have this option, but I always believed that using @Ignore to deactivate tests is a bad idea. Except, maybe as one way to put tests that fail intermittently into quarantine to attend to them later
This bears the danger that the test suite decays as more and more tests keep getting ignored and forgotten. Therefore you should have a policy in place to ensure that tests aren't quarantined for too long
If you love Unit testing and are hungry for more JUnit tutorials, tips and tricks, checkout the following amazing articles :
If you love Unit testing and are hungry for more JUnit tutorials, tips and tricks, checkout the following amazing articles :
- Difference between Stub and Mock in Unit testing? (answer)
- My List of JUnit and Unit testing Best Practices (list)
- Simple JUnit example to test linked list in Java (example)
- How to test Exception in JUnit 4? (answer)
- JUnit 4 tip - Constructor is called before test method (tip)
- Top 5 Books to Learn JUnit (books)
- How Assertion works in Java? (answer)
1 comment :
Do you have any video of that? I'd like to find out some additional information.
Post a Comment