Android Espresso(一)——环境建立

2024-01-15 16:32
文章标签 android 环境 建立 espresso

本文主要是介绍Android Espresso(一)——环境建立,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • Android Testing使用总结(一)
    • 标注
    • Android Support Test
      • 引入
      • 基本使用
    • AndroidX Test
      • 测试库
      • 基本使用示例
      • 示例

Android Testing使用总结(一)

写了很多年的代码,Test Case代码一直一个被忽略且容易去逃避的问题,总觉得在不停迭代运行过程中,可以插错到很多情况。但事实,一旦进入到QA阶段,很多问题(bug)就是冒出来了。

因此引起重视,开始在完成task的过程中尽可能的编写Test代码。

标注

这里的几个标注先说明下:

  • @FixMethodOrder决定测试用例方法的执行顺序,其中的参数值定义在MethodSorters有三个:NAME_ASCENDING, JVM, DEFAULT.
    • NAME_ASCENDING 表示通过字典序排列方法,执行测试方法。
    • JVM 表示由JVM进行排序并执行,不能保证想要的执行顺序。
    • DEFAULT 表示不可预知的顺序执行。
  • @LargeTest 大型测试用例,执行时间 >1000ms,还需要说明的是与之对应的还有 SmallTest, MediumTest
    • LargeTest 标注一个大型测试,一般多为功能UI测试,执行时间 >1000ms
    • MediumTest标注一个中型测试,执行时间 <1000ms,一般用以测试单个组件功能。
    • SmallTest 标注一个小型测试,这个就是最常说的单元测试,执行时间 < 200ms
  • @Before 在 @Test 测试方法执行之前,执行一些初始化动作。
  • @After 与 @Before 相对,它标注的方法在所有 @Test 方法执行结束之后执行。
  • @Test 标注用以说明当前方法是一个测试方法。
  • @Rule 标注一个字段(Field)或者方法(Method),且字段或方法并须是publicstatic。具体描述可以查看 TestRule (JUnit API)

Android Support Test

开始,Android test 库是 support library.

引入

app/build.grdale 内添加基本测试依赖库。

android {defaultConfig {// ...testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}testOptions {unitTests {includeAndroidResources=true}}}dependencies {// ...testImplementation 'junit:junit:4.13.2'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'androidTestImplementation 'com.android.support.test:rules:1.0.2'androidTestImplementation 'com.android.support.test:runner:1.0.2'androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'androidTestImplementation 'org.hamcrest:hamcrest-integration:1.3'
}

基本使用

@RunWith(AndroidJUnit4.class)
@LargeTest
public class TM295PrintTextTest {@Rulepublic ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);private UiDevice mDevice;@Beforepublic void setup() {mDevice = UiDevice.getInstance(getInstrumentation());}@Testpublic  void testPrint() {onView(withText(R.string.menu_reports)).perform(click());mDevice.wait(Until.hasObject(By.textContains("printing")), 30000);}
}

AndroidX Test

AndroidX Test无疑是一开始最该去了解并且运用的。不多说,看使用总结。

可以阅读官方文档的测试基知识来获取知识;在 Android 平台上测试应用

可以去github阅读samples代码:android/testing-samples

测试库

UI Automator

作用:

  • 用于检查布局层次结构的查看器。
  • 用于检索状态信息并在目标设备上执行操作的 API。
  • 支持跨应用界面测试的 API。

gradle引用:

androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

官方文档:

UI Automator | Android 开发者 | Android Developers


Espresso

作用:

Espresso 来编写简洁、美观且可靠的 Android 界面测试。

gradle引用:

androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

其他软件包:

软件包

官方文档:

Espresso | Android 开发者 | Android Developers


上述两个测试库最为基本且重要,其他还有一般引入的有:

    testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test:rules:1.3.0'androidTestImplementation 'androidx.test:runner:1.3.0'androidTestImplementation 'androidx.test:core:1.3.0'androidTestImplementation 'org.hamcrest:hamcrest-integration:1.3' // UI匹配器

基本使用示例

  1. gradle依赖引入(app目录下build.gradle)

    dependencies {// ...testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'androidTestImplementation 'androidx.test.espresso:espresso-intents:3.3.0'androidTestImplementation 'androidx.test:rules:1.3.0'androidTestImplementation 'androidx.test:runner:1.3.0'androidTestImplementation 'androidx.test:core:1.3.0'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.ext:truth:1.3.0'androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'androidTestImplementation 'org.hamcrest:hamcrest-integration:1.3'// ...
    }
    
  2. 工程根目录下build.gradle

    android {// ...defaultConfig {testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}testOptions {unitTests {includeAndroidResources=true}}useLibrary 'android.test.runner'useLibrary 'android.test.base'useLibrary 'android.test.mock'// ...
    }
    

这样Gradle的基本配置结束了。

这里的示例是依赖于Android API,因此测试代码放置于 project/app/src/androidTest目录之下。

线上测试代码:


@RunWith(AndroidJUnit4.class)
@FixMethodOrder(value= MethodSorters.NAME_ASCENDING) // 执行顺序,@Test的方法按照方法名字典序执行
@LargeTest // 大型测试标注
public class TM295TestUnit  {@Rulepublic ActivityScenarioRule<PeripheralActivity> activityRule = new ActivityScenarioRule<>(PeripheralActivity.class);private UiDevice mDevice; // UI Automator API/*** 在此初始化一些公共字段,或者通用常量*/@Beforepublic void setup() {System.out.println("Test Start ~~~");mDevice = UiDevice.getInstance(getInstrumentation()); activityRule.getScenario().moveToState(State.RESUMED);}/*** 表明一个test case方法*/@Testpublic void testPrinter_1() {onView(withId(R.id.button_init_printer)).perform(click()); // EspressomDevice.wait(Until.hasObject(By.text("Open successfully")), 1000);try {System.out.println("test1 sleep starts");Thread.sleep(1000);System.out.println("test1 sleep ends");} catch (InterruptedException e) {e.printStackTrace();}}@Afterpublic void tearDown() {System.out.println("Test Complete");activityRule.getScenario().moveToState(State.DESTROYED);}
}

示例

这里需要注意的是@Before @After 两个标注,他们标注的方法在@Test标注的方法执行时均被调用,执行顺序是 @Before @Test @After,即每个测试用例方法执行时, @Before @After 方法也都会被执行。

@RunWith(AndroidJUnit4.class)
@MediumTest
public class MainActivityTest {@Rulepublic ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class);@Beforepublic void setup() {Log.v("MainActivityTest", "setup method");}@Testpublic void test1() {Log.i("MainActivityTest", "test method test1");}@Testpublic void test2() {Log.i("MainActivityTest", "test method test2");onView(withId(R.id.btn_show_dialog)).perform(click());}public void test3() {Log.i("MainActivityTest", "test method test3");}@Afterpublic void tearDown() {Log.v("MainActivityTest", "tearDown method");}
}

上述代码运行结果:

2021-03-15 09:53:42.938 328-351/com.westernunion.android.demo I/TestRunner: started: test1(com.westernunion.android.MainActivityTest)
2021-03-15 09:53:44.384 328-351/com.westernunion.android.demo V/MainActivityTest: setup method
2021-03-15 09:53:44.385 328-351/com.westernunion.android.demo I/MainActivityTest: test method test1
2021-03-15 09:53:44.385 328-351/com.westernunion.android.demo V/MainActivityTest: tearDown method
2021-03-15 09:53:46.047 328-351/com.westernunion.android.demo I/TestRunner: finished: test1(com.westernunion.android.MainActivityTest)
2021-03-15 09:53:46.126 328-351/com.westernunion.android.demo I/TestRunner: started: test2(com.westernunion.android.MainActivityTest)
2021-03-15 09:53:46.740 328-351/com.westernunion.android.demo V/MainActivityTest: setup method
2021-03-15 09:53:46.740 328-351/com.westernunion.android.demo I/MainActivityTest: test method test2
2021-03-15 09:53:48.685 328-351/com.westernunion.android.demo V/MainActivityTest: tearDown method
2021-03-15 09:53:49.788 328-351/com.westernunion.android.demo I/TestRunner: finished: test2(com.westernunion.android.MainActivityTest)

从控制台打印输出可以看到,每个“test method xx”信息前后,都有打印setup methodtearDown method信息。

这就印证代码前的说法。

这篇关于Android Espresso(一)——环境建立的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/609515

相关文章

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SQLite3 在嵌入式C环境中存储音频/视频文件的最优方案

《SQLite3在嵌入式C环境中存储音频/视频文件的最优方案》本文探讨了SQLite3在嵌入式C环境中存储音视频文件的优化方案,推荐采用文件路径存储结合元数据管理,兼顾效率与资源限制,小文件可使用B... 目录SQLite3 在嵌入式C环境中存储音频/视频文件的专业方案一、存储策略选择1. 直接存储 vs

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级