我正在使用 Espresso 来构建 UI 测试。对于某些测试用例,我想调用特定的 after 步骤来重置状态,以防脚本失败。
有没有办法@After
为单个 JUnit 测试用例 ( @Test
)执行一个步骤?我能想到的唯一解决方案是制作一个单独的测试类。但我希望将测试用例分组在同一个测试类中。
听起来确实有点奇怪;)但是......
您可以将 try/finally 添加到您想要此行为后的单个测试中。例如:
@Test
public void testA() {
try {
// the body of testA
} finally {
// apply the 'after' behaviour for testA
}
}
或者,如果您真的想使用 JUnit,@After
那么您可以使用TestName 规则(自 JUnit 4.7 起),如下所示:
@Rule
public TestName testName = new TestName();
@After
public void conditionalAfter() {
if ("testB".equals(testName.getMethodName())) {
System.out.println("apply the 'after' behaviour for testB");
}
}
@Test
public void testA() {
}
@Test
public void testB() {
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句