Testing UI Espresso

ui  test是测试ui逻辑是否正确的流程,可以测试ui组件,数据,交互等多种内容,这里介绍Espresso测试方案。

那么下面说下测试步骤:

第一步:在build.gradle文件中的dependency选项中配置:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
然后在defaultConfig中增加配置,之后sync
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

贴一个示例build.gradle文件:

Example build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22"

    defaultConfig {
        applicationId "com.my.awesome.app"
        minSdkVersion 10
        targetSdkVersion 22.0.1
        versionCode 1
        versionName "1.0"

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

dependencies {
    // App's dependencies, including test
    compile 'com.android.support:support-annotations:22.2.0'

    // Testing-only dependencies
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}


第二步:开始编写测试代码,这里针对MainAcivity进行测试,MainActivity为项目代码,之前已经编写完成,测试代码如下:

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
public class ChangeTextTest {

    private String mStringToBetyped;

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

    @Before
    public void initValidString() {
        // Specify a valid string.
        mStringToBetyped = "Espresso";
    }
    @Test
    public void testChangeText_same() {
        // Type text and then press the button.

        // Check that the text was changed.
        onView(withId(R.id.textToBeChanged))
                .check(matches(withText("My Application")));
    }

    @Test
    public void changeText_sameActivity() {
        // Type text and then press the button.
        onView(withId(R.id.editTextUserInput))
                .perform(typeText(mStringToBetyped), closeSoftKeyboard());
        onView(withId(R.id.changeTextBt)).perform(click());

        // Check that the text was changed.
        onView(withId(R.id.editTextUserInput))
                .check(matches(withText(mStringToBetyped)));
    }
}

第三步:点击绿三角形左边的选项,点击edit configuration,然后配置需要测试的方法,然后在specific instrumentation runner (optional)后面选择某一项测试方案。这里选择
android.support.test.runner.AndroidJUnitRunner
第四步:开始测试,可以直接鼠标右键到你要测试的方法内部,选择run 该方法即可。


这个时候,底部run选项开始部署测试,一般会执行两步,第一步重新安装apk,如果代码未做更改,则跳过,只是停止应用,第二步,执行测试任务,见下日志。

Testing started at 10:15 ...

09/20 10:15:02: Launching testChangeText_sam...()
No apk changes detected since last installation, skipping installation of C:\Users\user\Desktop\MyApplication\app\build\outputs\apk\app-debug.apk
$ adb shell am force-stop com.example.sxy.myapplication
$ adb push C:\Users\user\Desktop\MyApplication\app\build\outputs\apk\app-debug-androidTest-unaligned.apk /data/local/tmp/com.example.sxy.myapplication.test
$ adb shell pm install -r "/data/local/tmp/com.example.sxy.myapplication.test"
    pkg: /data/local/tmp/com.example.sxy.myapplication.test
Success


Running tests

$ adb shell am instrument -w -r   -e debug false -e class com.example.sxy.myapplication.ChangeTextTest#testChangeText_same com.example.sxy.myapplication.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..Test running started
Tests ran to completion.


参考网址为:https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html


但在编写测试代码的时候却容易遇到一些问题,比如:

1:Test running failed: Unable to find instrumentation info for: ComponentInfo....

这个是因为虽然指定了测试,但指定的测试方案错误,需要在点击edit configuration ,然后点击需要测试的方法,然后在specific instrumentation runner (optional)后面选择某一项测试方案。这里选择 android.support.test.runner.AndroidJUnitRunner

2:junit.framework.AssertionFailedError: No tests found in....

这个是既未指定测试,也没有对配置文件进行配置,解决办法为:

①:在specific instrumentation  runner (optional)后面选择测试方案,参见上面的1

②:需要在build.gradle文件中增加配置,

  • Add to the same build.gradle file the following line in android.defaultConfig:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"


上述针对的是UI测试,需要context上下文,需要设备或者模拟器才能测试。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值