Espresso + Junit4-运行所有测试之前登录一次 [英] Espresso + Junit4 - login once before running all test

查看:95
本文介绍了Espresso + Junit4-运行所有测试之前登录一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的一个应用程序编写一些自动化测试.所有功能都需要登录.

I want to write some automated test for one of my application. All of the functionality requires login.

因此,我已经编写了测试,但是对于每个测试,它都在登录并测试功能.反正有什么方法可以帮助我仅登录一次然后运行所有测试?

So, i have written test, but for each test, it is doing login and testing the functionality. Is there anyway which will help me to login only once and then run all test?

最简单的方法是只用一种测试方法编写所有测试.但是我认为实现这一目标将是一种丑陋的方式.任何更清洁的解决方案都是如此,测试只会登录一次,然后运行测试集.

Easiest way would be to write all test in only one test method. But i think it would be ugly way to achieve that. Any cleaner solution so, test will login only once and then run set of test.

以下是我的测试代码:

@RunWith(AndroidJUnit4.class)
public class AllDisabledTest {
    public static final String USER_NAME = "all_disabled";
    public static final String DISPLAY_NAME = "All Disabled";
    public static final String PASSWORD = "1234";

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
            LoginActivity.class);

    @Before
    public void loginToApp(){

        onView(withId(R.id.edit_email)).perform(replaceText(USER_NAME));
        onView(withId(R.id.edit_password)).perform(replaceText(PASSWORD));

        onView(withId(R.id.login_button)).perform(click());
    }

    @Test
    public void checkIfFoodItemAddedToCart(){
        onData(anything()).inAdapterView(withId(R.id.menu_item_grid)).atPosition(2).perform(click());

        onData(anything()).inAdapterView(withId(R.id.listview)).atPosition(0).onChildView(withId(R.id.item_name)).check(matches(withText("BLUEITEM")));
    }
}

预先感谢您:).

推荐答案

您可以使用带有@BeforeClass和@AfterClass批注的方法.

You can use methods with @BeforeClass and @AfterClass annotations.

@RunWith(AndroidJUnit4.class)
public class AllDisabledTest {
    public static final String USER_NAME = "all_disabled";
    public static final String DISPLAY_NAME = "All Disabled";
    public static final String PASSWORD = "1234";

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
            LoginActivity.class);
    }

    @BeforeClass
    public static void setUpBeforeClass() {
        // do login stuff here
    }

    @AfterClass
    public static void tearDownAfterClass() {
        // ...
    }

    // ...
}

注意:@BeforeClass和@AfterClass方法必须是静态的.

Note: @BeforeClass and @AfterClass methods must be static.

这篇关于Espresso + Junit4-运行所有测试之前登录一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆