Issue
As an example, I have an app with a MainActivity that has a button and a NextActivity that has a RecyclerView populated with the integers in a vertical list. I could write the following separate Espresso tests:
Test 1:
public class MainScreenTest {
@Rule
public IntentsTestRule<MainActivity> mainActivityRule =
new IntentsTestRule<>(MainActivity.class);
@Test
public void shouldOpenNextActivityOnClick() {
onView(withId(R.id.btn)).check(matches(withText("foo")));
onView(withId(R.id.btn))
.perform(click());
intended(hasComponent("com.example.androidplayground.NextActivity"));
}
}
Test 2:
public class NextScreenTest {
@Rule
public ActivityTestRule<NextActivity> nextActivityRule =
new ActivityTestRule<>(NextActivity.class);
@Test
public void shouldScrollToItem() throws Exception {
int position = 15;
onView(withId(R.id.rv))
.perform(RecyclerViewActions.scrollToPosition(position));
onView(withText(position)).check(matches(isDisplayed()));
}
}
Alternatively, I could write one test that covers both:
public class UserJourneyTest {
@Rule
public ActivityTestRule<MainActivity> mainActivityRule =
new ActivityTestRule<MainActivity>(MainActivity.class);
@Test
public void userJourney() {
onView(withId(R.id.btn)).check(matches(withText("foo")));
onView(withId(R.id.btn))
.perform(click());
int position = 15;
onView(withId(R.id.rv))
.perform(RecyclerViewActions.scrollToPosition(position));
onView(withText(position)).check(matches(isDisplayed()));
}
}
Is one way better than the other? Will I gain a significant increase in performance by having one user journey instead of multiple separate tests?
Solution
My opinion is that if you're navigating from MainActivity to NextActivity by clicking a button, you wouldn't want to write a test which launches directly the NextActivity. For sure, espresso allows this, but if from MainActivity there are some data passed to NextActivity, you won't have them if your test launches NextActivity directly.
I'd say that first of all by writing a UI automation test you want to simulate a user's behaviour. So that I would go for the third option you've posted above, UserJourneyTest.class
In your case it's not a matter of performance, it's a matter of testing it right.
Answered By - mbob
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.