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 .
JUnit 5 – @Disabled Annotation You may disable or skip execution for a test method or a group of tests by applying the annotation at the Test level. Or all the tests could be skipped by applying @Disabled annotation at the class level instead of applying it to the test method level.
Annotation Type Ignore. Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with Test that are also annotated with @Ignore will not be executed as tests. Also, you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With