我正在使用 Espresso 来构建 UI 测试。对于某些测试用例,我想调用特定的后续步骤来重置状态,以防脚本失败。
有没有办法为单个 JUnit 测试用例 (@Test
) 执行 @After
步骤?
我能想到的唯一解决方案是制作一个单独的测试类。但我希望将测试用例分组在同一个测试类中。
最佳答案
听起来确实有点奇怪;)但是......
您可以将 try/finally 添加到您想要此之后行为的单个测试中。例如:
@Test public void testA() { try { // the body of testA } finally { // apply the 'after' behaviour for testA } }
或者,如果您确实想使用 JUnit 的
@After
那么您可以使用 TestName Rule (自 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() { }
关于android - 如何对特定的 JUnit 测试用例执行 @After?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47174618/