5

I have a test and I want that it should not be launched what is the good practice : set Ignore inside test? @Deprecated?

I would like to not launch it but with a message informing that I should make changes for future to launch it .

2 Answers 2

12

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> 
Sign up to request clarification or add additional context in comments.

3 Comments

so, I add @ignore("") before @Test, in this case test will not be launched but a comment will be shows, right ?
@lola: unfortunately the value is not printed out (don't know why - even if it should be an easy thing). There is only a Skipped: X counter in the summary.
@lola: I updated my answer on how to show the @Ignore annotation value
0

If you use test suite, you can edit all test cases in one place. eg:

@RunWith(Suite.class) @Suite.SuiteClasses({ WorkItemTOAssemblerTestOOC.class, WorkItemTypeTOAssemblerTestOOC.class, WorkRequestTOAssemblerTestOOC.class, WorkRequestTypeTOAssemblerTestOOC.class, WorkQueueTOAssemblerTestOOC.class }) public class WorkFlowAssemblerTestSuite { } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.