Android测试(五):Instrumented 单元测试

Android测试(五):Instrumented 单元测试

发布时间 2017年12月20日 虫师

原文:https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

Instrumented 单元测试是在真机和模拟器上运行的测试,它可以利用Android框架API和支持的API(如Android测试支持库)。如果你的测试需要访问工具信息(例如目标应用程序的Context),或者需要真正实现Android框架组件(如ParcelableSharedPreferences对象),则应该创建Instrumented 单元测试。

使用Instrumented单元测试还有助于减少编写和维护mock代码所需的工作量。 如果你愿意,仍然可以自由地使用一个mock框架模拟任何依赖关系。

设置测试环境



在你的Android Studio项目中,你必须将mock测试的源文件存储在module-name/src/androidTest/java/ 中。 创建新项目时该目录已经存在,并包含示例代码。

在开始之前,你应该下载Android测试支持库安装程序,该安装程序提供的API可让你快速构建和运行应用程序的检测代码。测试支持库包括用于功能性UI测试(Espresso和UI Automator)的JUnit 4测试运行器(AndroidJUnitRunner)和API。

还需要为项目配置Android测试依赖项,以使用测试运行程序和测试支持库提供的规则API。 为了简化测试开发,还应该包含Hamcrest库,它可以让你使用Hamcrest匹配器API创建更灵活的断言。

在你的App的顶级build.gradle文件中将这些库指定为依赖项:

dependencies {
    androidTestCompile ‘com.android.support:support-annotations:24.0.0‘
    androidTestCompile ‘com.android.support.test:runner:0.5‘
    androidTestCompile ‘com.android.support.test:rules:0.5‘
    // Optional -- Hamcrest library
    androidTestCompile ‘org.hamcrest:hamcrest-library:1.3‘
    // Optional -- UI testing with Espresso
    androidTestCompile ‘com.android.support.test.espresso:espresso-core:2.2.2‘
    // Optional -- UI testing with UI Automator
    androidTestCompile ‘com.android.support.test.uiautomator:uiautomator-v18:2.1.2‘
}

警告: 如果构建配置包含support-annotations库的编译依赖项和espresso-core库的androidTestCompile依赖项,则由于依赖冲突,构建可能会失败。 请按照下面步骤更新对espresso-core的依赖关系:

androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, {
    exclude group: ‘com.android.support‘, module: ‘support-annotations‘
})

要使用JUnit 4测试类,请确保将AndroidJUnitRunner指定为项目中的默认测试工具运行器,方法是在应用程序的模块级build.gradle文件中包含以下设置:

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

创建一个Instrumented的单元测试类



你的Instrumented单元测试类应该写成JUnit 4测试类。要了解有关创建JUnit 4测试类和使用JUnit 4断言和注释的更多信息,请参阅创建本地单元测试类。

要创建一个Instrumented的JUnit 4测试类,在测试类定义的开头添加@RunWith(AndroidJUnit4.class)注释。 还需要将Android测试支持库中提供的AndroidJUnitRunner类指定为默认测试运行器。测试入门中对此步骤进行了更详细的介绍。

以下示例显示如何编写一个Instrumented单元测试,以确保LogHistory类正确实现了Parcelable接口:

import android.os.Parcel;
import android.support.test.runner.AndroidJUnit4;
import android.util.Pair;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class LogHistoryAndroidUnitTest {

    public static final String TEST_STRING = "This is a string";
    public static final long TEST_LONG = 12345678L;
    private LogHistory mLogHistory;

    @Before
    public void createLogHistory() {
        mLogHistory = new LogHistory();
    }

    @Test
    public void logHistory_ParcelableWriteRead() {
        // Set up the Parcelable object to send and receive.
        mLogHistory.addEntry(TEST_STRING, TEST_LONG);

        // Write the data.
        Parcel parcel = Parcel.obtain();
        mLogHistory.writeToParcel(parcel, mLogHistory.describeContents());

        // After you‘re done with writing, you need to reset the parcel for reading.
        parcel.setDataPosition(0);

        // Read the data.
        LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel);
        List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData();

        // Verify that the received data is correct.
        assertThat(createdFromParcelData.size(), is(1));
        assertThat(createdFromParcelData.get(0).first, is(TEST_STRING));
        assertThat(createdFromParcelData.get(0).second, is(TEST_LONG));
    }
}

创建一个测试套件



要组织测试单元测试的执行,可以将一组测试集合在一个测试套件类中,并将这些测试一起运行。测试套件可以嵌套; 测试套件可以将其他测试套件分组,并将所有组件测试类一起运行。

测试套件包含在测试包中,类似于主应用程序包。按照惯例,测试套件包名通常以.suite后缀结尾(例如,com.example.android.testing.mysample.suite)。

以下示例显示了如何实现名为UnitTestSuite的测试套件,该测试套件将CalculatorInstrumentationTestCalculatorAddParameterizedTest测试类分组并运行在一起。

import com.example.android.testing.mysample.CalculatorAddParameterizedTest;
import com.example.android.testing.mysample.CalculatorInstrumentationTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

// Runs all unit tests.
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorInstrumentationTest.class,
        CalculatorAddParameterizedTest.class})
public class UnitTestSuite {}

运行Instrumented单元测试



要运行Instrumented测试,请遵循以下步骤:

1、通过单击工具栏中的“Sync Project”,确保您的项目与Gradle同步。。

2、以下列其中一种方式运行测试:

  • 要运行单个测试请打开Project窗口,然后单击“Run”。
  • 要测试类中的所有方法,请右键单击测试文件中的类或方法,然后单击“Run”。
  • 要在目录中运行所有测试,请右键单击该目录并选择“Run Tests”。

Gradle的Android插件编译位于默认目录(src/androidTest/java/)中的测试代码,构建测试APK和生产APK,在连接的真机或模拟器上安装两个APK,并运行测试。Android Studio然后在“Run”窗口中显示测试执行结果。

注意:在运行或调试测试工具时,Android Studio不会为即时运行注入所需的额外方法,并关闭该特性。

使用Firebase测试实验室运行测试



略….

(请查看原文)

额外的示例代码



要下载到示例应用程序,请参阅Android ActivityInstrumentation Sample

原文地址:https://www.cnblogs.com/jason89/p/9034057.html

时间: 2024-08-05 12:07:19

Android测试(五):Instrumented 单元测试的相关文章

Android测试:从零开始3—— Instrumented单元测试1

Instrumented单元测试是指运行在物理机器或者模拟机上的测试,这样可以使用Android framework 的API和supporting API.这会在你需要使用设备信息,如app的Context,你可以使用Instrumented单元测试.使用Instrumented单元测试还可以减少mock的代码(当然也可以选择写mock的代码). 一般测试代码放在app/src/androidTest/java/下面.开始测试之前需要在build.gradle配置中引入依赖库: depende

(4.5.4)Android测试TestCase单元(Unit test)测试和instrumentationCase单元测试

Android单元和instrumentation单元测试 Developing Android unit and instrumentation tests Android的单元测试是基于JUnit的.可分为: 1.本地单元测试 - 可以在JVM上运行测试(速度快,优先考虑). 2.Instrumented单元测试 - 需要Android系统 Android的Gradle插件支持在JVM上执行Andr??oid单元测试.它使用特殊版本的android.jar(也称为 Android mocka

Android测试(三):Android 单元测试

Android测试(三):Android 单元测试 发布时间 2017年12月20日 虫师 原文:https://developer.android.com/training/testing/unit-testing/index.html 单元测试是你的应用程序测试策略的基本测试. 通过针对您的代码创建和运行单元测试,你可以轻松验证各个单元的逻辑是否正确. 在每次构建之后运行单元测试可帮助你快速捕获并修复由代码更改引入到应用程序的软件回归. 单元测试通常以可重复的方式实现尽可能小的代码单元(可以

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

上一篇分析了android项目的测试分类,这一篇讲local单元测试. 参考android官方文档. 测试前需要配置测试环境,新建项目后,目录下会出现app/src/test/java/文件夹,这个文件夹是用于存放local单元测试代码的(就是与androidAPI无关的测试). 在build.gradle配置文件中增加测试依赖: dependencies { // Required -- JUnit 4 framework testCompile 'junit:junit:4.12' // O

Android测试(四):Local 单元测试

Android测试(四):Local 单元测试 发布时间 2017年12月20日 虫师 原文:https://developer.android.com/training/testing/unit-testing/local-unit-tests.html 如果你的单元测试没有依赖或者只有简单的Android依赖,则应该在本地开发机器上运行测试.这种测试方法非常高效,因为它可以帮助你避免每次运行测试时将目标应用程序和单元测试代码加载到真机或模拟器上的开销.因此,运行单元测试的执行时间大大减少了.

Android测试:从零开始1——简介

参考文档:https://developer.android.com/training/testing/start/index.html 测试分类 使用android studio进行测试,首先需要先了解android测试的分类,新建工程后,项目会默认包含两个测试目录: 1.本地单元测试(Local unit tests) 测试代码位于module-name/src/test/java/,这些测试直接运行在本地JVM上,不需要使用Android框架的API. 2.设备测试(Instrumente

Android测试APP工具(一)

最近面试APP开发人员的时候,遇到了技术总监问 APP测试的概念性问题,后面感觉基本的项目流程.项目逻辑.屏幕适配. 测试是完全没有问题的,但是对于APP的性能测试.压力测试等高端的测试,还是存在着美中不足之处呀,毕竟是搞专业的APP开发 人员.但是,自己追求APP开发的流程是永不止境的,所以下定决心去了解深挖一下APP的测试,饿补一下APP的高端测试流程及其常 用工具.最近开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括android测试框 架.C

android产品研发(十九)--&gt;android studio中的单元测试

转载请标明出处:一片枫叶的专栏 上一篇文章中我们讲解了webview中问题集锦,讲解了webview的性能优化.webview种入Cookie信息.activity退出的时候清除webview信息报错.如何通过java代码和js代码相互交互.webview如何下载文件以及腾讯的X5浏览服务等知识,这些都是我在使用webview中遇到的问题,难点,实践等,更多关于这些问题的说明,可以参考我的:android产品研发(十八)–>webview趟过的坑 本文我们将讲解如何在android studio

构建Instrumented单元测试

本文翻译自:构建Instrumented单元测试 水平有限自己感觉很多地方表达的并不到位,但找不到更好的表达方式,如果您觉着有更好的表达方式,帮助我改进! 构建Instrumented单元测试 Instrumented单元测试运行在你的物理设备或模拟器上,而不是你本地机器中的jvm.如果你需要获取instrumentation信息(比如目标App的Context)或者 你需要一个android framework 组件(比如Parcelable or SharedPreferences 对象)的