lola
lola

Reputation: 5789

Ignore JUnit test

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 .

Upvotes: 5

Views: 2881

Answers (2)

jeha
jeha

Reputation: 10700

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>

Upvotes: 12

passover
passover

Reputation: 1

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 {

}

Upvotes: 0

Related Questions