即使断言之一失败,仍在junit4中继续测试执行 [英] Continuing test execution in junit4 even when one of the asserts fails
问题描述
我有使用Jfunc构建的现有框架,该框架提供了即使在测试用例中的一个断言失败时也可以继续执行的功能. Jfunc使用junit 3.x框架.但是现在我们正在迁移到junit4,所以我不能再使用Jfunc,而已将其替换为junit 4.10 jar.
现在的问题是,因为我们已经在框架中广泛使用了jfunc,并且使用junit 4,我们希望使我们的代码能够继续执行,即使在测试用例中一个断言失败时也是如此.
有没有人对此有任何建议/想法,我知道在junit中测试需要更加原子化,即每个测试用例一个断言,但是由于某种原因我们不能在框架中做到这一点.
I have my existing framework built up using Jfunc which provides a facility to continue exection even when one of the asserts in the test case fails. Jfunc uses junit 3.x framework. But now we are migrating to junit4 so I can't use Jfunc anymore and have replaced it with junit 4.10 jar.
Now the problem is since we have extensively used jfunc in our framework, and with junit 4 we want to make our code continue the execution even when one of the asserts fails in a test case.
Does anyone has any suggestion/idea for this, i know in junit the tests needs to be more atomic i.e. one assert per test case but we can't do that in our framework for some reason.
推荐答案
To use it, first add the rule as a field in your test class:
public class MyTest {
@Rule
public ErrorCollector collector = new ErrorCollector();
//...tests...
}
然后用对collector.checkThat(...)
的调用替换您的断言.
Then replace your asserts with calls to collector.checkThat(...)
.
例如
@Test
public void myTest() {
collector.checkThat("a", equalTo("b"));
collector.checkThat(1, equalTo(2));
}
这篇关于即使断言之一失败,仍在junit4中继续测试执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!