【问题标题】:Can I use both Junit 3 and Junit 4 in the same Android project?我可以在同一个 Android 项目中同时使用 Junit 3 和 Junit 4 吗?
【发布时间】:2015-01-15 17:45:24
【问题描述】:

我有一个基于 Junit 3 的现有 AndroidTestCase 单元测试的 Android Studio 项目。我也想探索引入 robolectric 测试,而不是替代,而是补充 AndroidTestCase 测试。我可以在同一个项目中同时使用基于 Junit 3 的 AndroidTestCase 测试和基于 Junit 4 的 robolectric 测试吗?我可以在我的竹 CI 系统上同时运行它们吗?如何配置项目以支持它?

【问题讨论】:

  • 我希望这是可能的 - 您可以过滤类或将它们放在不同的文件夹中,您可以在构建和运行 android 测试时删除 robolectric 测试类,您可以在运行 robolectric 时过滤掉 android 测试测试。但与此同时,我预计该项目的设置将难以实施、理解和支持。我会在不同的模块中移动 android 或 robolectric 测试,但我对在同一个项目中进行 android 和 robolectric 测试没有太多经验

标签: gradle android-studio robolectric android-testing bamboo


【解决方案1】:

我建议将 Robolectric 测试放在同一项目下的单独模块中。

要创建一个新模块,请转到 File -> New Module... -> Java Library。创建该模块后,导航到其build.gradle 文件并将其内容替换为以下行:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}

repositories {
    mavenLocal()
    maven { url "$System.env.ANDROID_HOME/extras/android/m2repository" } // Fix 'com.android.support:*' package not found issue
    mavenCentral()
}

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 'org.hamcrest:hamcrest-integration:1.1'
    testCompile 'org.hamcrest:hamcrest-core:1.1'
    testCompile 'org.hamcrest:hamcrest-library:1.1'

    testCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
    testCompile('org.robolectric:robolectric:2.4') {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }
    testCompile "org.mockito:mockito-core:1.9.5"
    testCompile 'org.assertj:assertj-core:1.6.1'
}

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

这使模块具有 Robolectric 测试的优点,应该作为一个很好的起点。

请注意,:app 应替换为您的应用程序模块的名称。

对于 Robolectric 测试,您还需要一个可能如下所示的 Robolectric 跑步者:

public class ApplicationTestRunner extends RobolectricTestRunner {

    //Maximun SDK Robolectric will compile (issues with SDK > 18)
    private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18;

    private static final String ANDROID_MANIFEST_PATH = "../app/src/main/AndroidManifest.xml";
    private static final String ANDROID_MANIFEST_RES_PATH = "../app/src/main/res";

    /**
     * Call this constructor to specify the location of resources and AndroidManifest.xml.
     *
     * @throws org.junit.runners.model.InitializationError
     */
    public ApplicationTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    @Override protected AndroidManifest getAppManifest(Config config) {
        return new AndroidManifest(Fs.fileFromPath(ANDROID_MANIFEST_PATH),
                Fs.fileFromPath(ANDROID_MANIFEST_RES_PATH)) {
            @Override
            public int getTargetSdkVersion() {
                return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC;
            }
        };
    }
}

然后只用这个运行器注释测试类,例如:

@RunWith(ApplicationTestRunner.class)
public class PriorityExecutorSchedulerTest {
    ....
}

可以使用 gradlew test 运行来自 Robolectric 模块的测试。

关于 Bamboo,AFAIK 有可能运行自定义脚本,所以我只需将 gradlew test 放入其中,看看效果如何。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 2019-11-03
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 2018-08-02
    • 2012-03-13
    相关资源
    最近更新 更多