Skip to main content
edited body
Source Link
jeha
  • 10.8k
  • 5
  • 55
  • 72

I'd normally use @Ignore("comment on why it is ignored"). IMO the comment is very important for other developers to know why the test is disabled or for how long (maybe it's just temporarily).

EDIT:

By default there is only a info like Tests run: ... Skipped: 1 ... for ignored tests. How to print the value of Ignore annotation?

One solution is to make a custom RunListener:

public class PrintIgnoreRunListener extends RunListener { @Override public void testIgnored(Description description) throws Exception { super.testIgnored(description); Ignore ignore = description.getAnnotation(Ignore.class); String ignoreMessage = String.format( "@Ignore test method '%s()': '%s'", description.getMethodName(), ignore.value()); System.out.println(ignoreMessage); } } 

Unfortunately, for normal JUnit tests, to use a custom RunListener requires to have a custom Runner that registers the PrintIgnoreRunListener:

public class MyJUnit4Runner extends BlockJUnit4ClassRunner { public MyJUnit4Runner(Class<?> klassclazz) throws InitializationError { super(klassclazz); } @Override public void run(RunNotifier notifier) { notifier.addListener(new PrintIgnoreRunListener()); super.run(notifier); } } 

Last step is to annotate your test class:

@RunWith(MyJUnit4Runner.class) public class MyTestClass { // ... } 

If you are using maven and surefire plugin, you don't need a customer Runner, because you can configure surefire to use custom listeners:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.10</version> <configuration> <properties> <property> <name>listener</name> <value>com.acme.PrintIgnoreRunListener</value> </property> </properties> </configuration> </plugin> 

I'd normally use @Ignore("comment on why it is ignored"). IMO the comment is very important for other developers to know why the test is disabled or for how long (maybe it's just temporarily).

EDIT:

By default there is only a info like Tests run: ... Skipped: 1 ... for ignored tests. How to print the value of Ignore annotation?

One solution is to make a custom RunListener:

public class PrintIgnoreRunListener extends RunListener { @Override public void testIgnored(Description description) throws Exception { super.testIgnored(description); Ignore ignore = description.getAnnotation(Ignore.class); String ignoreMessage = String.format( "@Ignore test method '%s()': '%s'", description.getMethodName(), ignore.value()); System.out.println(ignoreMessage); } } 

Unfortunately, for normal JUnit tests, to use a custom RunListener requires to have a custom Runner that registers the PrintIgnoreRunListener:

public class MyJUnit4Runner extends BlockJUnit4ClassRunner { public MyJUnit4Runner(Class<?> klass) throws InitializationError { super(klass); } @Override public void run(RunNotifier notifier) { notifier.addListener(new PrintIgnoreRunListener()); super.run(notifier); } } 

Last step is to annotate your test class:

@RunWith(MyJUnit4Runner.class) public class MyTestClass { // ... } 

If you are using maven and surefire plugin, you don't need a customer Runner, because you can configure surefire to use custom listeners:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.10</version> <configuration> <properties> <property> <name>listener</name> <value>com.acme.PrintIgnoreRunListener</value> </property> </properties> </configuration> </plugin> 

I'd normally use @Ignore("comment on why it is ignored"). IMO the comment is very important for other developers to know why the test is disabled or for how long (maybe it's just temporarily).

EDIT:

By default there is only a info like Tests run: ... Skipped: 1 ... for ignored tests. How to print the value of Ignore annotation?

One solution is to make a custom RunListener:

public class PrintIgnoreRunListener extends RunListener { @Override public void testIgnored(Description description) throws Exception { super.testIgnored(description); Ignore ignore = description.getAnnotation(Ignore.class); String ignoreMessage = String.format( "@Ignore test method '%s()': '%s'", description.getMethodName(), ignore.value()); System.out.println(ignoreMessage); } } 

Unfortunately, for normal JUnit tests, to use a custom RunListener requires to have a custom Runner that registers the PrintIgnoreRunListener:

public class MyJUnit4Runner extends BlockJUnit4ClassRunner { public MyJUnit4Runner(Class<?> clazz) throws InitializationError { super(clazz); } @Override public void run(RunNotifier notifier) { notifier.addListener(new PrintIgnoreRunListener()); super.run(notifier); } } 

Last step is to annotate your test class:

@RunWith(MyJUnit4Runner.class) public class MyTestClass { // ... } 

If you are using maven and surefire plugin, you don't need a customer Runner, because you can configure surefire to use custom listeners:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.10</version> <configuration> <properties> <property> <name>listener</name> <value>com.acme.PrintIgnoreRunListener</value> </property> </properties> </configuration> </plugin> 
added 2068 characters in body
Source Link
jeha
  • 10.8k
  • 5
  • 55
  • 72

I'd normally use @Ignore("comment on why it is ignored"). IMO the comment is very important for other developers to know why the test is disabled or for how long (maybe it's just temporarily).

EDIT:

By default there is only a info like Tests run: ... Skipped: 1 ... for ignored tests. How to print the value of Ignore annotation?

One solution is to make a custom RunListener:

public class PrintIgnoreRunListener extends RunListener { @Override public void testIgnored(Description description) throws Exception { super.testIgnored(description); Ignore ignore = description.getAnnotation(Ignore.class); String ignoreMessage = String.format( "@Ignore test method '%s()': '%s'", description.getMethodName(), ignore.value()); System.out.println(ignoreMessage); } } 

Unfortunately, for normal JUnit tests, to use a custom RunListener requires to have a custom Runner that registers the PrintIgnoreRunListener:

public class MyJUnit4Runner extends BlockJUnit4ClassRunner { public MyJUnit4Runner(Class<?> klass) throws InitializationError { super(klass); } @Override public void run(RunNotifier notifier) { notifier.addListener(new PrintIgnoreRunListener()); super.run(notifier); } } 

Last step is to annotate your test class:

@RunWith(MyJUnit4Runner.class) public class MyTestClass { // ... } 

If you are using maven and surefire plugin, you don't need a customer Runner, because you can configure surefire to use custom listeners:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.10</version> <configuration> <properties> <property> <name>listener</name> <value>com.acme.PrintIgnoreRunListener</value> </property> </properties> </configuration> </plugin> 

I'd normally use @Ignore("comment on why it is ignored"). IMO the comment is very important for other developers to know why the test is disabled or for how long (maybe it's just temporarily).

I'd normally use @Ignore("comment on why it is ignored"). IMO the comment is very important for other developers to know why the test is disabled or for how long (maybe it's just temporarily).

EDIT:

By default there is only a info like Tests run: ... Skipped: 1 ... for ignored tests. How to print the value of Ignore annotation?

One solution is to make a custom RunListener:

public class PrintIgnoreRunListener extends RunListener { @Override public void testIgnored(Description description) throws Exception { super.testIgnored(description); Ignore ignore = description.getAnnotation(Ignore.class); String ignoreMessage = String.format( "@Ignore test method '%s()': '%s'", description.getMethodName(), ignore.value()); System.out.println(ignoreMessage); } } 

Unfortunately, for normal JUnit tests, to use a custom RunListener requires to have a custom Runner that registers the PrintIgnoreRunListener:

public class MyJUnit4Runner extends BlockJUnit4ClassRunner { public MyJUnit4Runner(Class<?> klass) throws InitializationError { super(klass); } @Override public void run(RunNotifier notifier) { notifier.addListener(new PrintIgnoreRunListener()); super.run(notifier); } } 

Last step is to annotate your test class:

@RunWith(MyJUnit4Runner.class) public class MyTestClass { // ... } 

If you are using maven and surefire plugin, you don't need a customer Runner, because you can configure surefire to use custom listeners:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.10</version> <configuration> <properties> <property> <name>listener</name> <value>com.acme.PrintIgnoreRunListener</value> </property> </properties> </configuration> </plugin> 
Source Link
jeha
  • 10.8k
  • 5
  • 55
  • 72

I'd normally use @Ignore("comment on why it is ignored"). IMO the comment is very important for other developers to know why the test is disabled or for how long (maybe it's just temporarily).