Android 单元测试初体验

1 概述

What?

对最小可测试单元进行的测试,验证输入输出是否正确,属于白盒测试

Why?

从最小粒度上,尽早发现问题

通过测试思考更好的代码结构和设计

2 配置

2.1 配置依赖

主工程目录的 build.gradle

dependencies {
    ... 
    def final JUNIT_VERSION = '4.12'
    def final MOCKITO_VERSION = '1.10.19'
    def final HAMCREST_VERSION = '1.3'

    // Dependencies for local unit tests
    testCompile "junit:junit:$JUNIT_VERSION"
    testCompile "org.mockito:mockito-all:$MOCKITO_VERSION"
    testCompile "org.hamcrest:hamcrest-all:$HAMCREST_VERSION"

    // Dependencies for Android unit tests
    androidTestCompile "junit:junit:$JUNIT_VERSION"
    androidTestCompile "org.mockito:mockito-core:$MOCKITO_VERSION"
    androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    ...
}

2.2 配置 Runner

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

2.3 配置文件路径

如果使用项目默认路径,则测试代码在

- src
    - androidTest
    - test
    - main

如果是从 Eclipse 转化的项目,则需要自定义测试代码的路径,比如

 android {
    ...
    sourceSets {
        ...
        test.setRoot('tests/test)
        androidTest.setRoot('tests/androidTest')
        ... 
    }
    ...
}

这样测试代码就在

- tests
    - test
    - androidTest

3 验证

3.1 JUnitTest

如果纯 Java 方法,没有依赖 Android 的类库,则可以直接跑本地测试,在 JVM 上进行验证

/**
 * @author lidiqing
 * @since 2017/5/3.
 */
public class HelloWorldTest {

    @Test
    public void text() throws Exception {
        HelloWorld helloWorld = new HelloWorld();
        Assert.assertEquals("Hello World!", helloWorld.text());
    }
}

测试情况如下

《Android 单元测试初体验》 Java test

3.2 AndroidTest

如果用到了 Android 环境的一些类,需要进行 Android 测试,在设备上跑测试

比如这里使用了 android.webkit.URLUtil,如果直接跑本地测试,会报 RuntimeException 的异常,所以我们在 androidTest 包下创建测试

import android.support.test.runner.AndroidJUnit4;
import android.webkit.URLUtil;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * @author meitu
 * @since 2017/5/3.
 */
@RunWith(AndroidJUnit4.class)
public class SimpleTest {

    @Test
    public void testUrl() {
        String url = "http://lynch.me";
        Assert.assertTrue(URLUtil.isHttpUrl(url));
    }
}

配置一下启动

《Android 单元测试初体验》 Android Test

跑出来的结果

《Android 单元测试初体验》 Android Test Result

Q&A

  • androidTestCompile 是做什么的

    在测试的时候引入依赖,该依赖不会加入 debug 或者 release 中

  • Warning:Conflict with dependency ‘com.android.support:support-v4′. Resolved versions for app (25.0.1) and test app (23.1.0) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

    androidTestCompile 引用包依赖的 support 库和 compile 的 support 的库有冲突

  • testCompile 和 androidTestCompile 没有生效,各种 class 或 method 找不到

    从 Eclipse 移植过来项目,需要再配置一下 test 和 androidTest

android {
  sourceSets {
    test.setRoot('tests/test')
    androidTest.setRoot('test/androidTest')
  }
}
  • Test running failed: Instrumentation run failed due to ‘java.lang.NoSuchMethodError’
    Empty test suite

    导入的 support 库有冲突

资料

    原文作者:Oblee
    原文地址: https://www.jianshu.com/p/d73d4aa870b9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞