gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:08:42 25 4
gpt4 key购买 nike

我正在实现一个测试自动化工具,我有一个扩展 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_one() 之前,另一次在 test_two() 之后。 tearDown() 也会发生同样的情况,它会在执行完这两个方法之后被调用。

我想在这里做的是只调用一次 setUp() 和 tearDown() 方法来执行所有 BaseTests 测试。所以方法调用的顺序是这样的:

1) 设置()

2) test_one()

3) 测试二()

4) 拆解()

有没有办法做这样的事情?

最佳答案

我使用以下方法解决了这个问题:

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

和:

@AfterClass
public static void tearDownAfterClass() 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分别保证在测试运行前后只运行一次

关于android - 为每个测试套件 InstrumentationTestCase Android 运行 setUp() 和 tearDown() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24893248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com