Is there a way to run an Espresso test with several test methods, but only one installation method?

Today I have a simple test:

@RunWith(AndroidJUnit4.class) @LargeTest public class WhenNavigatingToUsersView { @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class); private MainActivity mainActivity; @Before public void setActivity() { mainActivity = mActivityRule.getActivity(); onView(allOf(withId(R.id.icon), hasSibling(withText(R.string.users)))).perform(click()); } @Test public void thenCorrectViewTitleShouldBeShown() { onView(withText("This is the Users Activity.")).check(matches(isDisplayed())); } @Test public void thenCorrectUserShouldBeShown() { onView(withText("Donald Duck (1331)")).check(matches(isDisplayed())); } } 

But for each test method, setActivity is setActivity , which, if you have 10-15 methods, will ultimately be time-consuming (if you also have many views).

@BeforeClass does not seem to work, as it should be static and thus force the ActivityTestRule also static.

So is there any other way to do this? Instead of having multiple statements in one testing method?

+7
android junit4 android-espresso
source share
4 answers

@Before annotation must precede methods containing presetting. Initializing the necessary objects, getting the current session or current activity, you get an idea.

It replaces the old setUp() method from ActivityInstrumentationTestCase2, just as @After replaces tearDown() . This means that it is designed to be run before each test in the class, and it should remain that way.

There should be no ViewInteraction , no DataInteraction , no Assertions and View actions in this method, since this is not its purpose.

In your case, just remove the onView() call from setActivity() and place it inside the actual test methods, if necessary in each test method, for example:

 @RunWith(AndroidJUnit4.class) @LargeTest public class WhenNavigatingToUsersView { @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class); private MainActivity mainActivity; @Before public void setActivity() { mainActivity = mActivityRule.getActivity(); // other required initializations / definitions } @Test public void thenCorrectViewTitleShouldBeShown() { onView(allOf(withId(R.id.icon), hasSibling(withText(R.string.users)))).perform(click()); onView(withText("This is the Users Activity.")).check(matches(isDisplayed())); } @Test public void thenCorrectUserShouldBeShown() { onView(allOf(withId(R.id.icon), hasSibling(withText(R.string.users)))).perform(click()); onView(withText("Donald Duck (1331)")).check(matches(isDisplayed())); } } 
+3
source share

Another option for you would be to separate these tests.

Clicking on the user icon will be in the HomeActivity test class, and the rest of the tests will be in the UserActivity test class.

The UserActivity test class will launch UserActivity with the appropriate intention (you can do this by passing a false Boolean to the Rule constructor and calling launchActivity(intent) manually).

This saves you from having to adjust activity every time. He will also get rid of constant dependence on core activities. If something goes wrong, your UserActivity tests will be intact and give results, while the problem will be caught by the test in MainActivity.

In fact, in doing so, your tests can become MediumSize, as the execution time will be significantly reduced.

0
source share

You can try the following:

 **** Setting **** public void testStory() throws Exception { } public void testStory2() throws Exception { } public void testStory3() throws Exception { } 

Try the test using this command:

 ./gradlew cC 
0
source share

You tried to do this as follows, or change it slightly to suit your needs:

 @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class); private MainActivity mainActivity = mActivityRule.getActivity(); @BeforeClass public static void setActivity() { onView(allOf(withId(R.id.icon), hasSibling(withText(R.string.users)))).perform(click()); } 

That way you mainActivity should not be static. In addition, the setActivity() method will be called only once.

0
source share

All Articles