Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为特定的JUnit测试用例执行@After?_Android_Unit Testing_Junit_Automated Tests_Android Espresso - Fatal编程技术网

Android 如何为特定的JUnit测试用例执行@After?

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

我用浓缩咖啡来做UI测试。对于一些测试用例,我想在脚本失败时调用特定的after步骤来重置状态

对于一个JUnit测试用例(
@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() {
    
    }
    

因为提到TestName规则,所以投票结果很好,因为我使用的是contiperf