在 Android Studio 中将集成测试与单元测试分开 [英] Separating integration tests from unit tests in Android Studio

查看:33
本文介绍了在 Android Studio 中将集成测试与单元测试分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试分离 Android Studio 0.9 中的集成测试.

I'm trying to separate out integration tests in Android Studio 0.9.

我在构建文件中添加了以下内容:

I have added the following to the build file:

sourceSets {
    integrationTest {
        java.srcDir file('src/integrationTest/java')
    }
}

task integrationTest(type: Test) {
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
}

我遇到了几个问题:

  1. 任务将运行,但它没有可用的其余项目文件,因此我收到有关缺少类的错误.我发现了一些特定于 Java 的解决方案,例如:

  1. The task will run but it doesn't have the rest of the project files available so I get errors about missing classes. There are some Java specific solutions I've found such as:

但我一直无法弄清楚如何让它与 Android Studio 一起工作.mainmain.output 的各种组合和玩依赖似乎不起作用,我得到如下错误:

But I haven't been able to figure out how to get this to work with Android Studio. Various combinations of main and main.output and playing around with dependencies don't seem to work, I get errors like:

Error:(33, 0) Could not find property 'main' on SourceSet container..

这是有道理的,因为 android 插件定义了自己的源集,但这些也不起作用.

Which makes sense as the android plugin defines its own source sets, but these don't work either.

IDE 无法将该目录识别为测试源目录.出于测试目的,我将源集名称更改为 androidTest 并正确获取绿色文件夹图标,并且测试与已在 androidTest 中定义的现有单元测试一起运行.

The IDE doesn't recognise the directory as a test source directory. For testing purposes I changed the source set name to androidTest and it correctly gets the green folder icon and the tests are run along with the existing unit tests that are already defined in androidTest.

推荐答案

@sm4 的回答确实适用于 Java 模块(使用 apply plugin: 'java'),但不幸的是不适用于 Android 应用程序(apply plugin: 'com.android.application') 或 Android 库模块(apply plugin: com.android.library).

@sm4's answer works indeed for a Java module (with apply plugin: 'java'), but unfortunately not for Android application (apply plugin: 'com.android.application') nor Android library modules (apply plugin: com.android.library).

但我找到了一种解决方法:
为您的集成测试创建文件夹:

But I have found a workaround:
Create the folders for your integration tests:

src/integrationTest/java
src/integrationTest/res

为您的新文件夹添加源集:

Add the sourceSets for your new folders:

sourceSets {
    integrationTest {
        java {
            srcDir file('src/integrationTest/java')
        }
        res {
            srcDir file('src/integrationTest/res')
        }
    }
}

在纯 Java 模块中,java 文件夹现在将变为绿色,而 res 文件夹图标将更改.在 Android 应用程序/库模块中,它没有.

In a pure Java module the java folder would now turn green and the res folder icon would change. In an Android application/library module it does not.

现在创建一个与 sourceSet 中配置的文件夹同名的产品风味,它可以工作!

Now create a product flavor identically named as the folder configured in the sourceSet, and it works!

productFlavors {
    integrationTest {
    }
}

然后在上面放一颗樱桃:

And to put a cherry on top:

configurations {
    integrationTestCompile.extendsFrom testCompile
}

这篇关于在 Android Studio 中将集成测试与单元测试分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆