简体   繁体   中英

Android - Espresso : Activity getting recreated for each test

I am created a small application to check testing using espresso. While running multiple tests in a test class, I am observed that the activity is getting paused and recreated for each test. I don't think that's a normal behaviour.

Here is my code for test class :

@RunWith(AndroidJUnit4.class)
@LargeTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class MyTestClass {

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

@Before
public void beforeTest(){
    // Called before each test
}

@After
public void afterTest(){
    // Called after each test
}

@Test
public void a_clickButton() {
    Log.d("robotarch", ">>>>>>> a_clickButton");
}

@Test
public void b_enterAndClick(){
    Log.d("robotarch", ">>>>>>> b_enterAndClick");
}


}

Beside this, I have also added logs to all my activity lifecycle methods.

Here is the order of output I am getting :

oncreate
onstart
onresume
onPostresume
>>>>>>> b_enterAndClick
onpause
onresume
onPostresume
onpause
oncreate
onstart
onresume
onPostresume
>>>>>>> a_clickButton
onStop
ondestroy
onpause
onStop
ondestroy

I checked the same application in robotium. It was not showing any such behavior. Can somebody explain me the reason behind such behavior ? Am I doing something wrong ?

Yes. The javadoc of ActivityTestRule is pretty clear about that:

This rule provides functional testing of a single activity. The activity under test will be launched before each test annotated with Test and before methods annotated with Before. It will be terminated after the test is completed and methods annotated with After are finished. During the duration of the test you will be able to manipulate your Activity directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM