Android – espresso junit4 – log in once before running all tests

I want to write some automated tests for one of my applications. All functions need to be logged in

So, I have written tests, but for each test, it is logging in and testing functions. Anyway, does it help me log in only once and run all tests?

The simplest way is to write all the tests with only one test method. But I think it will be an ugly way to achieve this goal. For any cleaner solution, the test will only log in once and run a set of tests

Here is my test code:

@RunWith(AndroidJUnit4.class)
public class AllDisabledTest {
    public static final String USER_NAME = "all_disabled";
    public static final String DISPLAY_NAME = "All Disabled";
    public static final String PASSWORD = "1234";

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

    @Before
    public void loginToApp(){

        onView(withId(R.id.edit_email)).perform(replaceText(USER_NAME));
        onView(withId(R.id.edit_password)).perform(replaceText(PASSWORD));

        onView(withId(R.id.login_button)).perform(click());
    }

    @Test
    public void checkIfFoodItemAddedToCart(){
        onData(anything()).inAdapterView(withId(R.id.menu_item_grid)).atPosition(2).perform(click());

        onData(anything()).inAdapterView(withId(R.id.listview)).atPosition(0).onChildView(withId(R.id.item_name)).check(matches(withText("BLUEITEM")));
    }
}

Thank you first:)

resolvent:

You can use methods annotated with @ beforeclass and @ afterclass

@RunWith(AndroidJUnit4.class)
public class AllDisabledTest {
    public static final String USER_NAME = "all_disabled";
    public static final String DISPLAY_NAME = "All Disabled";
    public static final String PASSWORD = "1234";

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

    @BeforeClass
    public static void setUpBeforeClass() {
        // do login stuff here
    }

    @AfterClass
    public static void tearDownAfterClass() {
        // ...
    }

    // ...
}

Note: @ beforeclass and @ afterclass methods must be static

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>