使用Espresso进行测试的顺序

33

有没有一种方法可以在安卓中设置测试运行顺序?
我使用Espresso框架,需要测试许多活动和它们之间的转换。我想为这些活动编写不同的测试,但是我需要运行这些测试的特定顺序。


3
作为良好的测试实践,你的测试不应该有顺序。你可能需要退一步,分析一下你的情况。 - nerdy900
@spinster - 我同意单元测试应该可以以任何顺序运行,但是现在测试框架已经扩展到了集成/组件测试和功能测试,这通常需要排序。例如,在进行组件测试CRUD操作之前,您需要存在数据库表。也许我们可以争论那些框架,如Spring、Android Espresso等,应该提供这样的排序。然而,这将需要修改核心库,因此在Junit中具有套件级别的排序将是一个不错的功能。 - MG Developer
7个回答

45

浓缩咖啡设置测试的运行顺序。

Junit 4.11 引入了 @FixMethodOrder 注释。只需升级您的 junit 版本并使用 FixMethodOrder(MethodSorters.NAME_ASCENDING) 注释测试类即可,无需使用自定义解决方案。有关详细信息,请查看发行说明。

以下是示例:

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");
   }
}

如果您想按顺序(使用类名)运行测试,该怎么办? - zzz

12

您可以像这样将注释添加为测试运行器装置:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

班级名称上方


12

9
听起来对我来说太疯狂了......如果每个测试方法都需要自己的设置,那么测试套件可能需要很长时间......特别是如果设置功能已经被先前运行的测试方法测试过了...采用这种方法,您将一遍又一遍地重新测试情景,只为了达到每个给定测试方法所需的状态......如果指定测试顺序是一个不好的想法......强制按字母顺序排列甚至更糟。 - Tim Boland
是的,这有点“疯狂”,我不建议这样做,但这是对所提出问题的回答。 - yogurtearl
我不同意,如果您正在测试将API令牌存储在内存中(使用singleton)的API调用,并且其他测试访问该令牌。您仍然认为顺序无关紧要吗? - pregmatch

6

你有三种方法:



方法1: 使用JUnit 4和5工作

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

方法2:Junit 4和5均可使用

使用@FixMethodOrder注解

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

方法三: Junit5的使用

使用@Order注解进行排序

@Test
@Order(2)
public void testFunction(){
}

4

您可以使用订单编号和测试名称来设置订单,以下是示例:

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());

    }


}

2

我需要先测试登录活动,如果成功,它将登录用户。然后,我应该测试其他活动。注销活动测试应在最后运行。因此,需要按照活动测试的顺序进行。


1
你可以将一些授权/取消授权的功能从测试中提取到特定的方法中。然后在LoginTest中,你只需调用授权方法,在LogoutTest中调用授权和取消授权方法。其他测试将扩展一些BaseTest,该类将在setUp方法中进行授权,并在需要时在tearDown方法中进行取消授权。 - mr. Nutscracker

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接