Issue
I have a very simple test class for running espresso tests on Android that looks like this:
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?
Solution
It turns out this is due to the Android Test Orchestrator:
Each test runs in its own Instrumentation instance. Therefore, if your tests share app state, most of that shared state is removed from your device's CPU or memory after each test.
I don't know of a way around it, but at least now I know why it's happening.
Answered By - tylerargo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.