我们有多个测试,其中一些有时会因为环境问题而失败。所以测试被@忽略了。不幸的是,有时它在环境问题消失后很长一段时间都被忽视了。有没有好的方法可以让@Ignore在一段时间内或在某个日期之前有效?
我想我可以用一个日期检查来围绕罪魁祸首方法,如果它在那个日期之前就退出,但是我想知道你们是否可以建议一个更优雅的解决方案。
谢谢
发布于 2016-08-24 11:21:37
请允许我发布我完成的工作。使用来自https://github.com/junit-team/junit4/wiki/rules和http://blog.jiffle.net/post/41125006846/extending-junit-functionality-with-additional的信息,我已经实现了一个带有自定义注释的自定义规则。
注释:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention (value = RetentionPolicy.RUNTIME)
@Target( value = { ElementType.METHOD} )
public @interface IgnoreUntilAfter
{
public String value() default "";
}CustomRule:
import org.joda.time.LocalDateTime;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class IgnoreForTime
implements TestRule
{
private IgnoreUntilAfter ignoreUntilAfter;
public String getIgnoreLtdValue()
{
return ignoreUntilAfter.value();
}
@Override
public Statement apply(final Statement base, final Description description)
{
return new Statement()
{
@Override
public void evaluate() throws Throwable
{
ignoreUntilAfter = description.getAnnotation(IgnoreUntilAfter.class);
if (ignoreUntilAfter != null)
{
LocalDateTime currentTime = new LocalDateTime(System.currentTimeMillis());
if (!currentTime.isAfter(checkAndSetTimeLimit(getIgnoreLtdValue())))
{
return;
}
}
base.evaluate();
}
};
}
private LocalDateTime checkAndSetTimeLimit(String timeLimit)
{
if (timeLimit.isEmpty())
{
throw new RuntimeException("Please specify the date you want this Ignore to be active until. Ex: 2016-08-24T14:40:19.208 or 2016-08-24");
}
return LocalDateTime.parse(timeLimit);
}
}TestFile:
import org.junit.Rule;
import org.junit.Test;
public class TestIgnoreForTime
{
@Rule
public IgnoreForTime timer = new IgnoreForTime();
@Test
@IgnoreUntilAfter("2016-08-24T14:40:19.208")
public void doSomething()
{
System.out.println("doSomething");
}
@Test
public void doSomething2()
{
System.out.println("doSomething2");
}
@Test
@IgnoreUntilAfter
public void doSomething3()
{
System.out.println("doSomething3");
}
}希望这也能帮助到其他人。
发布于 2016-08-23 15:49:21
我猜没有“好”的通用答案;我认为你可能会通过技术和非技术解决方案的混合实现最大;归结为一组规则,如:
最后,这一切都归结为我的第一个建议:避免“偶尔”失败的测试。
测试的目的是公开代码库中的回归;它们的存在是为了向您提供value。不增加价值的东西应该尽快改进或删除。把它们原封不动地留在身边,就像你宁愿把石头放在鞋里走一样,因为你太忙了,没有时间停下来把石头从鞋里拿出来。
https://stackoverflow.com/questions/39089984
复制相似问题