4

I want to write some automated test for one of my application. All of the functionality requires login.

So, i have written test, but for each test, it is doing login and testing the functionality. Is there anyway which will help me to login only once and then run all test?

Easiest way would be to write all test in only one test method. But i think it would be ugly way to achieve that. Any cleaner solution so, test will login only once and then run set of test.

Following 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 in advance :).

1 Answer 1

4

You can use methods with @BeforeClass and @AfterClass annotations.

@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.