JUnit的测试与摇篮一个Android项目 [英] junit testing with gradle for an android project

查看:233
本文介绍了JUnit的测试与摇篮一个Android项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的测试(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测试:<一href="http://tryge.com/2013/02/28/android-gradle-build/">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

这篇关于JUnit的测试与摇篮一个Android项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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