JUnit的测试与摇篮一个Android项目摇篮、测试、项目、JUnit

2023-09-05 07:07:40 作者:[ 我在床上等着你 ]

我想获得的测试(JUnit和robolectric)在Android项目的工作,但我完全卡住。我的主要问题是,所有的测试,我发现摇篮某种程度上拉动了Java插件,然后我得到这个错误:

I am trying to get tests ( junit and robolectric ) working in an Android project but am totally stuck. My main problem is that all testing I found with gradle somehow pull in the java plugin and then I get this error:

The 'java' plugin has been applied, but it is not compatible with the Android plugins.

唯一的出路我目前看到的是分裂成试验和应用项目 - 但我想避免这种情况。任何例子/提示将是非常美联社preciated!

The only way out I see at the moment is to split into test and app project - but I would like to avoid that. Any examples/hints would be highly appreciated!

在官方文档没有提及单元测试的 - 只有仪表 - 测试 - 但我想单元测试来快速得到结果。

In the official documentation there is no mention of unit-testing - only Instrumentation-Tests - but I want unit-tests to get results fast.

推荐答案

您不需要Java插件,因为Android将采取你所需要的大部分,从我到目前为止看到的照顾。

You don't need the Java plugin, since the Android will take care of what you need mostly, from what I've seen so far.

我设法通过这个人的博客上运行我的Robolectric和JUnit测试:http://tryge.com/2013/02/28/android-gradle-build/

I managed to get my Robolectric and junit tests running via this man's blog: http://tryge.com/2013/02/28/android-gradle-build/

我的build.gradle文件看起来像这样(在我的测试文件都在{} PROJECTDIR / test目录。

My build.gradle file looks like this (where my test files are in the {projectdir}/test directory.

...
// Unit tests

sourceSets {
        unitTest {
                java.srcDir file('test')
                resources.srcDir file('test/resources')
        }
}

dependencies {
        unitTestCompile files("$project.buildDir/classes/debug")
        unitTestCompile 'junit:junit:4.11'
        unitTestCompile 'org.robolectric:robolectric:2.1.1'
        unitTestCompile 'com.google.android:android:4.0.1.2'
}

configurations {
        unitTestCompile.extendsFrom runtime
        unitTestRuntime.extendsFrom unitTestCompile
}

task unitTest(type:Test, dependsOn: assemble) {
        description = "run unit tests"
        testClassesDir = project.sourceSets.unitTest.output.classesDir
        classpath = project.sourceSets.unitTest.runtimeClasspath
}

build.dependsOn unitTest