安卓测试二(Espresso)

    技术2022-07-14  78

    Espresso

    官网 ui测试,在androidTest下使用,需要连接手机 在写的时候,比如你写一个onView,在用Alt+Enter导入库的时候,应该注意导入静态的库,要不就直接Espresso.onView(),初学者可能会有点迷茫,我怎么找不到这个方法啊,明明都有加依赖了。 Espresso其实网上的文章还是挺多的,也不复杂,要注意的就是在找控件的时候,防止因为你的控件在一个页面中多次使用到,从而withId(R.id.xx)失败,我一般找控件,因为项目界面复杂,控件引用的地方多,所以我会使用AS的自动寻找控件功能 在AS里 Run -> Record Espresso Test,(记得连接手机)这个时候会启动你的app,会记录每一次点击的控件,自动通过onView或者onData寻找,这个单纯用来找控件就是了, 基本的测试大概是这样 首先我们要启动一个Activity @Rule,这个注释就是明确你要启动的Activity,运行测试用例的时候就启动这个Activity,但是我们有时候启动Activity需要传递参数,这个时候,嘿嘿,就要这样

    @JvmField @Rule var rule: ActivityTestRule<LearnActivity> = object : ActivityTestRule<LearnActivity>(LearnActivity::class.java) { override fun getActivityIntent(): Intent { // Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); val intent = Intent() intent.putExtra("showType", EnumLearnType.TYPE_NORMAL) intent.putExtra("classify_id", 9141) intent.putExtra("cur_position", 0) intent.putExtra("jkbd_ids", "19128,29023,29037") //模拟三道题,正确答案都是B intent.putExtra("normal_type", 1000) return intent } }

    代码中加了个@JvmField是因为Kotlin的原因,Java用户请忽略 //测试用例,Kotlin,Rule

    API链接

    依赖,新建项目默认会有添加,以下是比较全面的

    def espresso_version = "3.2.0" //Test *************************************************************************** testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' testImplementation 'org.mockito:mockito-core:2.25.1' androidTestImplementation "org.mockito:mockito-android:2.23.0" // Optional -- Hamcrest library androidTestImplementation 'org.hamcrest:hamcrest-library:1.3' //Core library androidTestImplementation 'androidx.test:core:1.2.0' //AndroidJunitRunner and Junit Rules //收集分析数据,每次调用它都会上传待测应用包名的一个 hash 值(可设置禁止) androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test:rules:1.2.0' //Assertions androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:truth:1.2.0' androidTestImplementation 'com.google.truth:truth:1.0' //Espresso dependencies //包含核心和基本视图匹配器,以及操作和断言 androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_version" //包含DatePicker、RecyclerView和Drawer操作,以及辅助功能检查和CountingIdlingResource的外部贡献 androidTestImplementation "androidx.test.espresso:espresso-contrib:$espresso_version" //包含用于Intent意图测试的API资源 androidTestImplementation "androidx.test.espresso:espresso-intents:$espresso_version" //包含用于Accessibility测试的API资源 androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espresso_version" //包含用于WebView支持的测试API资源 androidTestImplementation "androidx.test.espresso:espresso-web:$espresso_version" androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espresso_version" //包含用于跨进程、多进程测试的API资源 androidTestImplementation "androidx.test.espresso:espresso-remote:$espresso_version" //Espresso与后台作业同步机制 androidTestImplementation "androidx.test.espresso:espresso-idling-resource:$espresso_version" implementation "androidx.test.espresso:espresso-idling-resource:$espresso_version" //UiAutomator androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    Processed: 0.010, SQL: 9