代码参考:https://github.com/googlesamples/android-testing

解释参考:

https://www.jianshu.com/p/5732b4afd12f

官网教程:

https://developer.android.google.cn/training/testing/unit-testing/local-unit-tests#setup

一、Android项目的测试基础

1.根据执行环境组织测试目录

Android Studio中的典型项目包含两个放置测试的目录。按如下方式组织测试:

  • androidTest目录应包含在实际或虚拟设备上运行的测试。此类测试包括集成测试,端到端测试以及仅JVM无法验证应用程序功能的其他测试。
  • test目录应包含在本地计算机上运行的测试,例如单元测试。

2.Consider whether to use test doubles考虑是否使用替身测试(test doubles 解释参考:https://www.jianshu.com/p/7a04f28b08a6)

创建测试时,您可以选择创建真实对象或测试替身,例如假对象或模拟对象。通常,在测试中使用真实对象比使用测试替身更好,尤其是当测试对象满足以下条件之一时:

  • 该对象是一个数据对象。
  • 除非它与依赖项的真实对象版本通信,否则该对象无法运行。一个很好的例子是事件回调处理程序。
  • 使用依赖项复制对象的通信很困难。一个很好的例子是SQL数据库处理程序,其中真实数据库比伪造的数据库能够提供更健壮的测试。

但是,如果您的测试尝试在真实对象上执行以下类型的操作,则最好创建伪造或甚至模拟对象 fake or mock:

  • 长操作,例如处理大文件。
  • 非密封操作Non-hermetic actions,例如连接到任意开放端口。
  • 难以创建的配置。

3.android中的测试类型:

  金字塔模型,分别是unitTest单元测试(70%) ,integration tests集成测试(20%),UI test(10%)

  官方推荐使用Robolectric和AndroidJUnitTest进行单元测试,参考官网http://robolectric.org/

  unit test:可以一次验证一个类的应用程序行为。

  integration test:validate either interactions between levels of the stack within a module, or interactions between related modules.用来验证一个模块中堆栈级别的交互,或者不同模块中的交互

Use your app's structure and the following examples of medium tests (in order of increasing scope) to define the best way to represent groups of units in your app:

  1. Interactions between a view and view model, such as testing a Fragment object, validating layout XML, or evaluating the data-binding logic of a ViewModel object.视图和视图模型之间的交互,例如测试 Fragment 对象,验证布局XML或评估对象的数据绑定逻辑 ViewModel
  2. Tests in your app's repository layer, which verify that your different data sources and data access objects (DAOs) interact as expected.在应用程序的存储库层中进行测试,以验证您的不同数据源和数据访问对象(DAO)是否按预期进行交互。
  3. Vertical slices of your app, testing interactions on a particular screen. Such a test verifies the interactions throughout the layers of your app's stack.应用程序的垂直切片,测试特定屏幕上的交互。此类测试会验证应用程序堆栈各层的交互。
  4. Multi-fragment tests that evaluate a specific area of your app. Unlike the other types of medium tests mentioned in this list, this type of test usually requires a real device because the interaction under test involves multiple UI elements.多片段测试,用于评估应用的特定区域。与此列表中提到的其他类型的介质测试不同,此类测试通常需要真实设备,因为测试中的交互涉及多个UI元素。

具体执行方式:

  1. Use methods from the Espresso Intents library. To simplify the information that you're passing into these tests, use fakes and stubbing.
  2. Combine your use of IntentSubject and Truth-based assertions to verify the captured intents.

  官方推荐使用espresso进行集成测试:https://developer.android.google.cn/training/testing/espresso/intents

  UI test:end-to-end tests that validate user journeys spanning multiple modules of your app.验证用户遍历应用中的不同模块。

  具体模型的介绍参考视频:https://www.youtube.com/watch?v=pK7W5npkhho&start=111

二、单元测试具体实践:

1、junit实践 (官方推荐使用Truth断言替代junit断言http://truth.dev/,我用不熟,暂时没管)

gradle添加依赖包,并且再AndroidManifest.xml中引入对应的lib,添加的包是根据junit的对应的用途,如果有些不会用到,可以不添加,具体对应关系参考:https://developer.android.google.cn/training/testing/set-up-project#junit-based-libs

 编辑depedency: 

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'

在项目src/com...../test 目录下新建junit的用例:

package com.patech.testApp;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class JunitTest {
@Test
public void testJunitTrue(){
assertTrue(1==1);
}
@Test
public void testJunitFalse(){
assertTrue(1==2);
}
}

测试结果应该是testJunitTrue pass,testJunitFalse failure。

这里用得依旧是junit的断言,可以更改为truth的断言,增加对应depenency,然后执行例子。

testImplementation "com.google.truth:truth:1.0"

  

public class TruthTest {
@Test
public void truthTestTrue(){
String string = "awesome";
assertThat(string).startsWith("awe");
assertWithMessage("Without me, it's just aweso").that(string).contains("me"); }
@Test
public void truthTestFalse(){
String string = "awesome";
assertThat(string).startsWith("awe.");
assertWithMessage("Without me, it's just aweso").that(string).contains("mex");
}
}

  

2、

  • 如果您对Android框架有依赖关系,特别是那些与框架创建复杂交互的框架,那么最好 使用Robolectric 包含框架依赖关系
  • 如果您的测试对Android框架的依赖性最小,或者测试仅依赖于您自己的对象,那么使用像Mockito这样的模拟框架来 包含模拟依赖项是很好的。

Robolectric的单元测试

添加gradle依赖:

androidImplementation "org.robolectric:robolectric:3.6.1"

  

添加其他配置

android {
// Robolectric
testOptions {
unitTests.includeAndroidResources = true
}
}

  

编写测试用例,保存再androidTest包中:

import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Test; import static com.google.common.truth.Truth.assertThat; public class UnitTestSampleJava {
private static final String FAKE_STRING = "HELLO_WORLD";
private Context context = ApplicationProvider.getApplicationContext(); @Test
public void readStringFromContext_LocalizedString() {
// Given a Context object retrieved from Robolectric...
ClassUnderTest myObjectUnderTest = new ClassUnderTest(context); // ...when the string is returned from the object under test...
String result = myObjectUnderTest.getHelloWorldString(); // ...then the result should be the expected one.
assertThat(result).isEqualTo(FAKE_STRING);
}
}

  

执行测试,需要连接手机或者模拟器。

3、Mockito

添加gradle依赖:

// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:1.10.19'

  

编写用例,保存再test包中

import android.content.Context;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class)
public class UnitTestSample { private static final String FAKE_STRING = "HELLO WORLD"; @Mock
Context mockContext; @Test
public void readStringFromContext_LocalizedString() {
// Given a mocked Context injected into the object under test...
when(mockContext.getString(R.string.hello_world))
.thenReturn(FAKE_STRING);
ClassUnderTest myObjectUnderTest = new ClassUnderTest(mockContext); // ...when the string is returned from the object under test...
String result = myObjectUnderTest.getHelloWorldString(); // ...then the result should be the expected one.
assertThat(result, is(FAKE_STRING));
}
}

 

 

Android 测试-Robolectric,mockito,esspresso的更多相关文章

  1. Android测试:Testing Apps on Android

    原文:https://developer.android.com/training/testing/index.html 测试你的App是开发过程中的重要组成部分.通过对应用程序持续的运行测试,你可以 ...

  2. Android测试:Fundamentals of Testing

    原文地址:https://developer.android.com/training/testing/fundamentals.html 用户在不同的级别上与你的应用产生交互.从按下按钮到将信息下载 ...

  3. Android测试(二):Android测试基础

    原文地址:https://developer.android.com/training/testing/fundamentals.html 用户在不同的级别上与你的应用产生交互.从按下按钮到将信息下载 ...

  4. Android测试(一):在Android中测试App

    原文:https://developer.android.com/training/testing/index.html 测试你的App是开发过程中的重要组成部分.通过对应用程序持续的运行测试,你可以 ...

  5. 1、Android测试入门

    编写和运行测试时Android APP开发周期中的重要的一环.好的测试可以让你非常容易的在开发过程中发现bug,提升你对自己代码的自信.使用Android Studio,你可以在物理设备或者虚拟机中运 ...

  6. Android测试:从零开始2——local单元测试

    上一篇分析了android项目的测试分类,这一篇讲local单元测试. 参考android官方文档. 测试前需要配置测试环境,新建项目后,目录下会出现app/src/test/java/文件夹,这个文 ...

  7. 2014 非常好用的开源 Android 测试工具

    http://www.php100.com/html/it/mobile/2014/1015/7495.html 当前有很大的趋势是转向移动应用平台,Android 是最广泛使用的移动操作系统,201 ...

  8. Android测试提升效率批处理脚本(三)

    前言: 前面放出过几次批处理,这次只放一个环境检查的被管理员给打回来了,不得不再找找几个有含金量的放出来,请看正文~~~ 目录 1.Android环境检查 2.Android内存监控 3.模拟蓝牙手柄 ...

  9. Android测试基础题(三)

    今天接着给大家带来的是Android测试基础题(三).    需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...

随机推荐

  1. {"aa":null} 如何能转化为 {"aa":{}}

    一个同事问的一个功能需求:{"aa":null} 如何能转化为 {"aa":{}}因为需求暂时不明确,暂时先完成这样的转换.使用的是FastJson1.2.7 ...

  2. python glob删除目录下的文件

    使用glob匹配目录下的文件 import os import glob path="./" for infile in glob.glob(os.path.join(path,& ...

  3. java面向函数编程简单应用

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.functio ...

  4. golang使用一个二叉树来实现一个插入排序

    思路不太好理解,请用断点 package main import "fmt" type tree struct { value int left, right *tree } fu ...

  5. 20 闭包、nonlocal

    闭包的概念 闭包就是能够读取其他函数内部变量的函数. 从模块级别调用函数内部的局部变量. 闭包 = 函数+环境变量(函数外部的变量) 闭包存在的条件 闭包必须返回一个函数 被返回的函数必须调用环境变量 ...

  6. WUSTOJ 1302: 区间k大数查询(Java)

    题目链接:

  7. Springboot对JPA的支持及使用

    目的: 1.springboot之jpa支持 2.Springboot+bootstrap界面版之增删改查及图片上传 springboot之jpa支持 导入相关pom依赖 <dependency ...

  8. AtCoder Grand Contest 034

    A:如果C在D左侧,显然先让B到达终点再让A走即可,否则先判断一下A是否可以在某处超过B.也就是先判断一下起点与终点之间是否有连续的障碍,若有则无解:然后若C在D左侧输出Yes,否则判断B和D之间是否 ...

  9. 怎样检测浏览器是否安装了某个插件, 比如flash

    首先, 我们可以获取浏览器安装的所有在插件: navigator.plugins 它会返回一个类似数组的对象, 包含所有已安装插件的具体信息. navigator.plugins; 然后我们可以通过正 ...

  10. C# SpinLock用法。

    class Program { static void Main(string[] args) { ; ]; Stopwatch sp = new Stopwatch(); sp.Start(); / ...