我的测试类中的所有测试在执行每个测试之前都执行一个“之前”方法(用 JUnit 的 @Before
注释)。
我需要一个特定的测试来不执行这个 before 方法。
有办法吗?
最佳答案
您可以使用 TestRule 来做到这一点.你用一些描述的注释标记你想跳过的测试,然后,在 TestRule 的 apply 方法中,你可以测试那个注释并做你想做的,比如:
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (description.getAnnotation(DontRunBefore.class) == null) {
// run the before method here
}
base.evaluate();
}
};
}
关于java - 从 JUnit 中的 'before' 方法中排除个别测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13017097/