jeh*_*eha 12
我通常会用@Ignore("comment on why it is ignored")
.IMO评论对于其他开发人员来说非常重要,因为他们知道测试被禁用的原因或持续时间(也许只是暂时的).
编辑:
默认情况下,只有Tests run: ... Skipped: 1 ...
忽略测试的信息.如何打印Ignore
注释的值?
一个解决方案是制作一个自定义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);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,对于正常的JUnit测试,使用自定义RunListener
需要有一个自定义Runner
注册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);
}
}
Run Code Online (Sandbox Code Playgroud)
最后一步是注释您的测试类:
@RunWith(MyJUnit4Runner.class)
public class MyTestClass {
// ...
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是maven和surefire插件,则不需要客户Runner
,因为您可以配置surefire以使用自定义侦听器:
<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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2385 次 |
最近记录: |