Android Studio Robolectric测试环境配置

Robolectric是一个高效但不完美的Android单元测试框架。其通过一系列对底层Android元素的替换来实现对原有元素调用的模拟,从而实现脱离模拟器的测 试。非常值得一提的是,在测试服务器请求时,Robolectric的数据模拟和延时发送模拟,给多线程状态下的测试提供了很好的解决方法

目前最新版本为3.0,可以支持API21,你可以在github上面找到他的实例和开源代码。
Roblectric的github地址
此处所实例的配置是之前2.4版本Roblectric的配置,目前使用最新的AS和3.0版的Robolectric更简单。

我的配置环境步骤
  • 安装Android Studio Unit Test插件。
    这个插件可以自动帮你关联测试所在目录以及维护插件测关系依赖,免去你很多需要配置的地方。
    可以通过插件中心(settings-plubgins)在线安装(Browse repositories…),也可以先下载插件的zip包,离线安装(Install plugin from disk..)。(插件zip包下载位置:https://github.com/evant/android-studio-unit-test-plugin/blob/master/AndroidStudioUnitTestPlugin.zip?raw=true

  • 添加测试module
    下图是我的项目目录,测试类单独在一个module里面。这样我们项目里面是不需要任何改动的,配置都是在测试module里面配置。
    目录结构
    下面是一些比较关键的文件:
    ../robolectric-test/build.gradle

evaluationDependsOn(":app")
apply plugin: 'java'

dependencies {
    def androidModule = project(':app')

    testCompile project(path: ':app', configuration: 'debugCompile')

    def debugVariant = androidModule.android.applicationVariants.find({it.name == 'debug'})
    testCompile debugVariant.javaCompile.classpath
    testCompile debugVariant.javaCompile.outputs.files
    testCompile files(androidModule.plugins.findPlugin("com.android.application").getBootClasspath())

    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:2.4'
}

tasks.withType(Test) {
    scanForTestClasses = false
    include "**/*Should.class"
    include "**/*Test.class"
    include "**/*Tests.class"
}

我这里使用了AndroidAnnotations所以需要自定义TestRunner类,让测试方法能够找到资源文件和清单文件位置。../robolectric-test/src/test/packagename/MyRobolectricTestRunner.java

package example.mislead.com.apptest;
import org.junit.runners.model.InitializationError;
import org.robolectric.AndroidManifest;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.res.Fs;

public class MyRobolectricTestRunner extends RobolectricTestRunner {
    // 2.4版本只是支持到API18,最新的3.0可以支持API21了
    private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18;
    protected static final String projectName = "appTest";
    public MyRobolectricTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    @Override
    protected AndroidManifest getAppManifest(Config config) {
        //set the project path as the root path
        int nameLength = projectName.length();
        String rootPath = System.getProperty("user.dir", "./");
        int index  = rootPath.indexOf(projectName);         //get the index of projectRootPath of user.dir

        if (index == -1) {
            throw new RuntimeException("project name not found in user.dir");
        }

        rootPath = rootPath.substring(0, index + nameLength);
        String manifestProperty = rootPath + "/app/src/main/AndroidManifest.xml";
        String resProperty = rootPath + "/app/src/main/res";
        String assetsProperty = rootPath + "/app/src/main/assets";
        return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
                Fs.fileFromPath(assetsProperty)) {
            @Override
            public int getTargetSdkVersion() {
                return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC;
            }
        };
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值