Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.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
为每个测试套件InstrumentationTestCase Android运行setUp()和tearDown()方法_Android_Testing_Automated Tests_Android Instrumentation - Fatal编程技术网

为每个测试套件InstrumentationTestCase Android运行setUp()和tearDown()方法

为每个测试套件InstrumentationTestCase Android运行setUp()和tearDown()方法,android,testing,automated-tests,android-instrumentation,Android,Testing,Automated Tests,Android Instrumentation,我正在实现一个测试自动化工具,我有一个扩展InstrumentationTestCase的类。例如: public class BaseTests extends InstrumentationTestCase { @Override protected void setUp() throws Exception { super.setUp(); Log.d(TAG, "setUp()"); } @Override pr

我正在实现一个测试自动化工具,我有一个扩展
InstrumentationTestCase
的类。例如:

public class BaseTests extends InstrumentationTestCase {

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        Log.d(TAG, "setUp()");
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        Log.d(TAG, "tearDown()");
    }

    public void test_one() {
        Log.d(TAG, "test_one()");
    }

    public void test_two() {
        Log.d(TAG, "test_two()");
    }
}
当我运行
BaseTests
的测试时,setUp()方法被调用了2次。一次在执行
test\u One()
之前,另一次在执行
test\u two()
之后。tearDown()也会发生同样的情况,在执行两个方法中的每一个后都会调用它

这里我想做的是只调用setUp()和tearDown()方法一次,以执行所有
BaseTests
测试。因此,方法调用的顺序如下:

1) 设置()

2) 测试一

3) 测试二

4) 拆卸


有没有办法做到这一点?

我使用以下方法解决此问题:

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
以及:

而不是setUp()和tearDown()。 因此,在您的情况下,它将是:

import org.junit.AfterClass;
import org.junit.BeforeClass;
public class BaseTests extends InstrumentationTestCase {

@BeforeClass
protected static void setUp() throws Exception { 
    //do your setUp
    Log.d(TAG, "setUp()");
}

@AfterClass
protected static void tearDown() throws Exception {
    //do your tearDown
    Log.d(TAG, "tearDown()");
}

public void test_one() {
    Log.d(TAG, "test_one()");
}

public void test_two() {
    Log.d(TAG, "test_two()");
}
}

注释@BeforeClass和@AfterClass确保它在测试运行之前和之后分别只运行一次

我最终遵循了@BeforeClass和@AfterClass的思想

然而,我不能使用注释本身。相反,我在基类上实现了它们(通过使用计数器),并且我的测试套件继承自这个基类

以下是我自己创建的链接:


我希望这能帮助别人

我不希望改变这种行为,但您可以使用一个布尔值来指示何时调用了第一对setUp/tearDown。调用这些回调函数是为了让您在每次测试开始之前设置环境,并在测试结束后清理环境以获取答案。但是添加这样的注释使我的构建中断了。我还需要进口什么吗?是的,我忘了进口。您应该导入:import org.junit.AfterClass;导入org.junit.BeforeClass;我现在可以建造了,谢谢你提醒我进口。。但它仍在交替打印每个测试的设置和拆卸。。我不知道是否会发生这种情况,因为我正在扩展InstrumentationTestCase。我想android.test.InstrumentationTestCase没有这些注释。像JUnitIt这样的东西也取决于您如何运行这些测试
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class BaseTests extends InstrumentationTestCase {

@BeforeClass
protected static void setUp() throws Exception { 
    //do your setUp
    Log.d(TAG, "setUp()");
}

@AfterClass
protected static void tearDown() throws Exception {
    //do your tearDown
    Log.d(TAG, "tearDown()");
}

public void test_one() {
    Log.d(TAG, "test_one()");
}

public void test_two() {
    Log.d(TAG, "test_two()");
}
}