Test order with espresso

前端 未结 7 810
天涯浪人
天涯浪人 2021-01-01 10:20

Is there a way to set test running order in android?
I use Espresso framework and need to test a lot of activities and transitions between them. I want to write differen

相关标签:
7条回答
  • 2021-01-01 10:54

    You can add annotation as test runner fixture as shown here:

    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    

    just above class name

    0 讨论(0)
  • 2021-01-01 11:02

    You have 3 ways:



    way 1: JUnit 4 and 5 work

    @Test
    public void testFunctionMain() {
        test1(); 
        test2()
        test3(); 
    }
    

    way 2: JUnit 4 and 5 work

    use @FixMethodOrder

    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    @RunWith(AndroidJUnit4::class)
    class LoginActivityTest {
    }
    

    way 3: Junit5 work

    use @Order

    @Test
    @Order(2)
    public void testFunction(){
    }
    
    0 讨论(0)
  • 2021-01-01 11:03

    espresso set running order of tests

    From Junit 4.11 comes with @FixMethodOrder annotation. Instead of using custom solutions just upgrade your junit version and annotate test class with FixMethodOrder(MethodSorters.NAME_ASCENDING). Check the release notes for the details.

    Here is a sample:

    import org.junit.runners.MethodSorters;
    
    import org.junit.FixMethodOrder;
    import org.junit.Test;
    
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class SampleTest {
    
       @Test
       public void A_firstTest() {
          System.out.println("first");
       }
    
       @Test
       public void B_secondTest() {
          System.out.println("second");
       }
    }
    
    0 讨论(0)
  • 2021-01-01 11:04

    Add the annotation @FixMethodOrder(MethodSorters.NAME_ASCENDING) on top of the class name and name the methods in ascending order.

    Please see the below links. The answer is there to achieve your need.

    https://stackoverflow.com/a/41198659/4675067

    https://stackoverflow.com/a/34456810/4675067

    0 讨论(0)
  • 2021-01-01 11:07

    I need to test loginActivity test first, if it succeeds , it will login the user.And, then I should test other activities. LogoutActivity test should run at the end. So, sequence of activity test is required.

    0 讨论(0)
  • 2021-01-01 11:09

    Yes You can set order using the order no with the test_name, See the below example-

    public class MyEspressoTest
            extends ActivityInstrumentationTestCase2<UserLoginActivity> {
    
        private UserLoginActivity mActivity;
    
        public MyEspressoTest() {
            super(UserLoginActivity.class);
        }
    
        @Before
        public void setUp() throws Exception {
            super.setUp();
            injectInstrumentation(InstrumentationRegistry.getInstrumentation());
            mActivity = getActivity();
        }
    
        public void test1InvalidPropigerLogin() {
            // Type text and then press the button.
    
            //setContentView function to see the layout
    
            onView(withId(R.id.username))
                    .perform(typeText("hill.hacker@gmail.com"), closeSoftKeyboard());
            onView(withId(R.id.password))
                    .perform(typeText("hhhhh"), closeSoftKeyboard());
    
            onView(withId(R.id.user_login_button)).perform(click());
            // Check that the text was changed.
            onView(withId(R.id.login_status))
                    .check(matches(withText("Invalid username or password")));
    
            //System.out.println("Test pass with invalid user and password");
        }
    
        public void test2ValidPropigerLogin() {
            // Type text and then press the button.
    
            onView(withId(R.id.username))
                    .perform(typeText("hill.hacker@like.com"), closeSoftKeyboard());
            onView(withId(R.id.password))
                    .perform(typeText("gggggg"), closeSoftKeyboard());
    
            onView(withId(R.id.user_login_button)).perform(click());
    
            //System.out.println("Test pass with valid user and password");
        }
    
        public void test3ForgetPasswordButton() {
    
            onView(withId(R.id.forgot_pwd_button)).perform(click());
    
            //onView(isRoot()).perform(ViewActions.pressBack());
    
            onView(withId(R.id.email_edittext))
                    .perform(typeText("hill.hacker@propiger.in"), closeSoftKeyboard());
            onView(withId(R.id.reset_password_button)).perform(click());
            // Check that the text was changed.
            onView(withId(R.id.reset_result))
                    .check(matches(withText("Email not registered with propiger")));
        }
        public void test4ForgetPasswordButton2() {
    
            onView(withId(R.id.forgot_pwd_button)).perform(click());
    
            onView(withId(R.id.email_edittext))
                    .perform(typeText("Hill.Hacker@like.com"), closeSoftKeyboard());
            onView(withId(R.id.reset_password_button)).perform(click());
            // Check that the text was changed.
            onView(withId(R.id.reset_result))
                    .check(matches(withText("Reset password link sent successfully")));
        }
        public void test5RegisterButton() {
            onView(withId(R.id.register_button)).perform(click());
    
                  //onView(isRoot()).perform(ViewActions.pressBack());
    
            onView(withId(R.id.register_name_edittext))
                    .perform(typeText("Hill Hacker"), closeSoftKeyboard());
            onView(withId(R.id.register_email_edittext))
                    .perform(typeText("Hill.Hacker+888@gmail.com"), closeSoftKeyboard());
            onView(withId(R.id.register_mobileno_edittext))
                    .perform(typeText("9090909090"), closeSoftKeyboard());
            onView(withId(R.id.register_password_edittext))
                    .perform(typeText("password111"), closeSoftKeyboard());
            onView(withId(R.id.register_confirm_password_edittext))
                    .perform(typeText("password111"), closeSoftKeyboard());
            //onView(withId(R.id.register_country_spinner)).perform(click());
            //onView(isRoot()).perform(withId(R.id.register_country_spinner, Sampling.SECONDS_15));
            onData(allOf(is(instanceOf(String.class)), is("India")))
                    .perform(click());
    
           onView(withId(R.id.register_country_spinner)).check(matches(withText(containsString("India"))));
    
            onView(withId(R.id.register_button)).perform(click());
    
        }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题