简体   繁体   English

让gradle执行JUnit Test(Android应用程序,Android Studio)

[英]getting gradle to execute JUnit Test (Android app, Android Studio)

I am currently working on an android app and lately switched from Eclipse to Android Studio (wasn't my idea;)). 我目前正在开发一个Android应用程序,最近从Eclipse切换到Android Studio(不是我的想法;))。 However I want to configure a jenkins server to run JUnit Tests and other tests on a regularly basis. 但是我想配置一个jenkins服务器来定期运行JUnit测试和其他测试。 To achieve this, I try to configure a gradle buidlfile. 为此,我尝试配置gradle buidlfile。 Here is my setup: 这是我的设置:

Directory structure: 目录结构:

-app

    -src
        -main
        -test

The build.gradle file: (located in "src/build.gradle) build.gradle文件:(位于“src / build.gradle”中)

    apply plugin: 'android'

    sourceSets {
        unitTest {
            java.srcDir file('src/test/java/[appName]/app')
        }
    }

    android {
        compileSdkVersion 19
        buildToolsVersion '19.0.3'

        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                runProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
        sourceSets {
            instrumentTest.setRoot('src/test')
        }
    }

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

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:support-v13:+'
        compile 'com.android.support:appcompat-v7:+'
        compile 'com.android.support:support-v4:+'

        unitTest 'junit:junit:4.10'
    }

Testclass MainActivityTest.java Testclass MainActivityTest.java

    package [appName].app;

    import android.test.InstrumentationTestCase;
    import junit.framework.Assert;

    public class MainActivityTest extends InstrumentationTestCase{

        public void testMain(){
            Assert.assertEquals(1,2);
        }
    }

and the most important piece, the error message: 而最重要的一块,错误信息:

    Error:(41, 0) Build script error, unsupported Gradle DSL method found: 'unitTest()'!

It is my first time working with gradle and Android Studio and I have really no clue what I am doing wrong. 这是我第一次使用gradle和Android Studio,我真的不知道我做错了什么。 On the internet I only find people with similar problems, but no one with a solution which worked for me. 在互联网上我只找到有类似问题的人,但没有一个人有一个适合我的解决方案。 I'd be really glad if you could point me in the right direction! 如果你能指出我正确的方向,我会很高兴的!

Regards 问候

The error message tell that there is no such property unitTest. 错误消息告诉我们没有这样的属性unitTest。 Test dependency are declared with instrumentTest (old) or androidTest (New). 使用instrumentTest(旧)或androidTest(新)声明测试依赖性。

Android sdk comes already with a junit 3 dependency. Android sdk已经有了junit 3依赖。 So just remove that line 所以只需删除该行

Unit testing doesn't appear to work out of the box in Android Studio. 在Android Studio中,单元测试似乎不是开箱即用的。 I have a working Gradle configuration that runs unit tests using Robolectric after some minor configurations. 我有一个工作的Gradle配置,在一些小配置后使用Robolectric运行单元测试。

See: Gradlectric 见: Gradlectric

Also to specify a different test folder you need to instead use androidTest : 另外要指定一个不同的测试文件夹,你需要使用androidTest

sourceSets {
        androidTest {
            setRoot('src/test')
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM