Android TestingAndroid单元测试Android Studio

Android中使用Espresso进行UI测试

2016-01-03  本文已影响1302人  阳春面

在使用Android Studio创建项目时,Android Studio一般都会自动创建测试相关的包名和类,可见测试在Android Studio开发中具有很重要的地位了,但我却从来没有使用过。
今天抽空看了一下Android中现在使用的测试框架Testing Support Library
,
其中的Espresso的API是非常友好,且易于使用的。

为了练习使用Espresso进行UI测试,我实现了一个简单的登录界面作为被测试功能。

实现登录功能

创建一个LoginActivity, 输入用户名和密码,然后点击登录按钮跳转
MainActivity,在MainActivity中显示“登录成功”或“登录失败”。

LoginActivity

@Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.btnLogin:
                Intent intent = new Intent(this,MainActivity.class);
                intent.putExtra("name",edtName.getText().toString().trim());
                intent.putExtra("pwd",edtPwd.getText().toString().trim());
                startActivity(intent);
                break;
        }

    }

MainActivity

    public static final String NAME = "android";
    public static final String PWD = "123456";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tvLoginResult = (TextView) findViewById(R.id.tvLoginResult);

        String name = getIntent().getStringExtra("name");
        String pwd = getIntent().getStringExtra("pwd");

        if (NAME.equals(name) && PWD.equals(pwd)) {
            tvLoginResult.setText("登录成功");
        } else {
            tvLoginResult.setText("登录失败");
        }
    }

配置Espresso

打开APP Module中得build.gradle文件,在dependencies中加入:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'

在android.defaultConfig中加入:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

添加测试类

在测试的package中创建LoginTest测试类, 我分别使用了正确和错误的密码进行测试,检查结果是否与预期的一致。
我的结果在另一个Activity中,Espresso可以直接跨Activity检查,不需要知道跳转关系。

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginTest    {

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


    @Test
    public void loginWithWrongPassword(){
        onView(withId(R.id.edtName)).perform(typeText("android"), closeSoftKeyboard());
        onView(withId(R.id.edtPwd)).perform(typeText("wrong"),closeSoftKeyboard());
        onView(withId(R.id.btnLogin)).perform(click());

        onView(withId(R.id.tvLoginResult)).check(matches(withText("登录失败")));

    }


    @Test
    public void loginWithRightPassword(){
        onView(withId(R.id.edtName)).perform(typeText("android"), closeSoftKeyboard());
        onView(withId(R.id.edtPwd)).perform(typeText("123456"),closeSoftKeyboard());
        onView(withId(R.id.btnLogin)).perform(click());

        onView(withId(R.id.tvLoginResult)).check(matches(withText("登录成功")));

    }

目前只是初步使用Espresso,以后还需要在实际项目中逐渐加入Espresso测试,在实战中把它用起来。

参考

Android Testing Support Library Espresso的官方网站

本文作者: 阳春面
作者博客:https://www.aswifter.com/2016/01/03/android-use-Espresso-ui-testing/

上一篇 下一篇

猜你喜欢

热点阅读