[英]@BeforeClass and @AfterClass called before and after each test
I have a very simple test class for running espresso tests on Android that looks like this: 我有一个非常简单的测试类,用于在Android上运行浓缩咖啡测试,如下所示:
import android.util.Log;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.IOException;
@RunWith(JUnit4.class)
public class Wtf {
private static class TestResources extends ExternalResource {
protected void before() {
println("before() TestResources");
}
protected void after() {
println("after() TestResources");
}
}
@ClassRule
public static final TestResources res = new TestResources();
@BeforeClass
public static void setUpClass() {
println("@BeforeClass setUpClass()");
}
@AfterClass
public static void tearDownClass() throws IOException {
println("@AfterClass tearDownClass()");
}
private static void println(String string) {
Log.d("wow", Wtf.class.getSimpleName() + ": " + string);
}
@Before
public void setUp() {
this.println("@Before setUp()");
}
@After
public void tearDown() throws IOException {
this.println("@After tearDown()");
}
@Test
public void test_1() {
this.println("@Test test1()");
}
@Test
public void test_2() {
this.println("@Test test2()");
}
}
And the output looks like this: 输出看起来像这样:
D/wow: Wtf: before() TestResources
D/wow: Wtf: @BeforeClass setUpClass()
D/wow: Wtf: @Before setUp()
D/wow: Wtf: @Test test1()
D/wow: Wtf: @After tearDown()
D/wow: Wtf: @AfterClass tearDownClass()
D/wow: Wtf: after() TestResources
D/wow: Wtf: before() TestResources
D/wow: Wtf: @BeforeClass setUpClass()
D/wow: Wtf: @Before setUp()
D/wow: Wtf: @Test test2()
D/wow: Wtf: @After tearDown()
D/wow: Wtf: @AfterClass tearDownClass()
D/wow: Wtf: after() TestResources
But I want something to be called after the entire class runs. 但是我希望在整个课程运行后再调用一些东西。 Am I doing something wrong? 难道我做错了什么? Why are the @BeforeClass and @AfterClass methods being called before and after each test? 为什么在每次测试之前和之后都要调用@BeforeClass和@AfterClass方法?
It turns out this is due to the Android Test Orchestrator : 原来这是由于Android Test Orchestrator造成的 :
Each test runs in its own Instrumentation instance. 每个测试都在其自己的Instrumentation实例中运行。 Therefore, if your tests share app state, most of that shared state is removed from your device's CPU or memory after each test. 因此,如果您的测试共享应用程序状态,则每次测试后,大部分共享状态将从设备的CPU或内存中删除。
I don't know of a way around it, but at least now I know why it's happening. 我不知道如何解决它,但是至少现在我知道为什么会这样。
I just tried to run your code in eclipse and @AfterClass and @BeforeClass is running only once as per the Junit documentation (screenshot attached). 我只是尝试在eclipse中运行您的代码,根据Junit文档(随附的屏幕截图),@AfterClass和@BeforeClass仅运行一次。
Wtf: before() TestResources
Wtf: @BeforeClass setUpClass()
Wtf: @Before setUp()
Wtf: @Test test1()
Wtf: @After tearDown()
Wtf: @Before setUp()
Wtf: @Test test2()
Wtf: @After tearDown()
Wtf: @AfterClass tearDownClass()
Wtf: after() TestResources
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.