Android 测试-Robolectric,mockito,esspresso

代码参考: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));
    }
}

 

 

原文地址:https://www.cnblogs.com/zhizhiyin/p/11389177.html