Android 如何为特定的JUnit测试用例执行@After?
我用浓缩咖啡来做UI测试。对于一些测试用例,我想在脚本失败时调用特定的after步骤来重置状态 对于一个JUnit测试用例(Android 如何为特定的JUnit测试用例执行@After?,android,unit-testing,junit,automated-tests,android-espresso,Android,Unit Testing,Junit,Automated Tests,Android Espresso,我用浓缩咖啡来做UI测试。对于一些测试用例,我想在脚本失败时调用特定的after步骤来重置状态 对于一个JUnit测试用例(@test),有没有一种方法可以执行@After步骤? 我能想到的唯一解决方案是创建一个单独的测试类。但是我希望将测试用例分组到同一个测试类中。听起来确实有点奇怪;)但是 您可以在单个测试中添加try/finally,以便在行为之后执行此操作。例如: @Test public void testA() { try { // the body of
@test
),有没有一种方法可以执行@After
步骤?
我能想到的唯一解决方案是创建一个单独的测试类。但是我希望将测试用例分组到同一个测试类中。听起来确实有点奇怪;)但是
- 您可以在单个测试中添加try/finally,以便在行为之后执行此操作。例如:
@Test public void testA() { try { // the body of testA } finally { // apply the 'after' behaviour for testA } }
- 或者,如果您真的想在之后使用JUnit的
,那么您可以使用(自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() { }