jeudi 21 mai 2015

Decoupled tests, go back activity

I'm pretty new to android and I'm trying to create some instrumented tests over an existing app.

There are two related activities:

Login --> Dashboard

Once you login correctly there is an intent that starts the Dashboard activity.

I have 2 instrumented tests:

  • test_nologin_with_bad_credentials: shows an error
  • test_login_with_good_credentials: initialize some db info an starts an intent that moves you into a DashboardActivity.

If I run them independently everything goes fine, but if I run them together and the 'good credentials' test comes first the 'bad credentials' starts with the app on another activity and thus the test fails.

Relevant code:

public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
  ...
    protected void setUp() throws Exception {
        super.setUp();

        cleanDB();
        setActivityInitialTouchMode(true);

        loginActivity = getActivity();
        userView =(TextView) loginActivity.findViewById(R.id.user);
        passView =(TextView) loginActivity.findViewById(R.id.password);
        signInButton =(Button) loginActivity.findViewById(R.id.email_sign_in_button);
        res = getInstrumentation().getTargetContext().getResources();
    }

   public void test_form_initial_empty() {
        assertEquals("", userView.getText().toString());
        assertEquals("", passView.getText().toString());
    }

    public void test_nologin_with_bad_credentials(){
        //GIVEN
        setText(userView, "bad");
        setText(passView, "bad");

        //WHEN
        TouchUtils.clickView(this, signInButton);

        //THEN
        String error=userView.getError().toString();
        assertEquals(res.getString(R.string.login_error_bad_credentials), error);
    }

    public void test_login_with_good_credentials(){
        //GIVEN
        Instrumentation.ActivityMonitor receiverActivityMonitor =getInstrumentation().addMonitor(DashboardActivity.class.getName(),null, false);
        setText(userView, "user");
        setText(passView, "user");

        //WHEN
        TouchUtils.clickView(this, signInButton);

        //THEN
        assertNotNull(Session.getUser());

        //THEN
        DashboardActivity dashboardActivity = (DashboardActivity) receiverActivityMonitor.waitForActivityWithTimeout(TIMEOUT_IN_MS);
        assertNotNull("DashboardActivity is started", dashboardActivity);

        //CLEANUP
        cleanDB();
        dashboardActivity.finish();
    }
}

How can I 'tearDown' each test so next one starts from scratch?

Aucun commentaire:

Enregistrer un commentaire