2013-06-20 3 views
24

Android 프로젝트에서 작동하는 테스트 (junit 및 robolectric)를 얻으려고하고 있지만 완전히 멈추었습니다. 내 주요 문제는 내가 Gradle을 발견 모든 테스트는 어떻게 든 자바 플러그인을 끌어와 나는이 오류 얻을 것이다 : 나는 순간에 볼안드로이드 프로젝트를위한 gradle을 사용한 junit 테스트

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

유일한 탈출구는 테스트 및 응용 프로그램 프로젝트로 분할하는 것입니다 -하지만 나는 그것을 피하고 싶습니다. 모든 예제/힌트를 높이 평가 될 것입니다!

the official documentation에는 단위 테스트 - 계측 테스트 만 언급되어 있지 않지만 단위 테스트를 통해 결과를 빠르게 얻길 바랍니다.

+0

하는 것으로 AS 1.1과 안드로이드 Gradle을 플러그인 1.1.0. http://tools.android.com/tech-docs/unit-testing-support –

답변

27

Java 플러그인이 필요하지 않습니다. Android에서 내가 지금까지 본 것에서 주로 필요한 것을 처리 할 것이기 때문입니다. . http://tryge.com/2013/02/28/android-gradle-build/ 내 테스트 파일은 {PROJECTDIR}에 내 build.gradle 파일은 다음과 같습니다

(/ test 디렉토리

:

나는 나의 Robolectric와의 JUnit 테스트는이 사람의 블로그를 통해 실행 얻을 관리
... 
// 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 
+0

감사합니다! 당신을 위해 Android-Studio와 함께 작동합니까? IDE에서 테스트를 실행하고 싶지만 아직 작동하지 않습니다. – ligi

+0

Android Studio를 아직 사용하지 않습니다. 명령 행에서 'gradle build'를 실행하기 만하면됩니다. AS에는 Gradle 통합이 있고 '빌드'를 실행하기 때문에 'unitTest'작업도 실행되므로 테스트하지는 않았지만 실행될 것으로 생각했을 것입니다. – newfivefour

+0

이 작업을 수행 할 수없는 것, 내 단위 테스트 robolectric 또는 junit 찾을 수없는 classpath 오류 가져옵니다. 작업과 함께 명령 줄에서 Gradle 1.6을 실행 중입니다 ./gradlew unitTest – gleenn

1

이 가이드는 도움이 될 수 있습니다 - http://www.slideshare.net/tobiaspreuss/how-to-setup-unit-testing-in-android-studio

최신 Gradle을 시험이 디렉토리

androidTest 아래에 있어야한다 당신의 gradle.build에서 또한

:

dependencies { 
    androidTestCompile 'junit:junit:4.+' 
} 

에서 사람들을 추가 defaultConfig는 {

testPackageName "test.java.foo" 
testInstrumentationRunner "android.test.InstrumentationTestRunner" 

}

+0

아니요. 단위 테스트에 의존성이 없거나 Android에만 단순한 종속성이있는 경우 로컬 개발 시스템에서 테스트를 실행해야합니다. http://developer.android.com/intl/ru/training/testing/unit-testing/local-unit-tests.html – babay

7

안드로이드 스튜디오와 새로운 안드로이드 Gradle을 플러그인은 이제 공식적인 단위 테스트 지원을 제공하고 있습니다 .

dependencies { 
    testCompile 'junit:junit:4.12' 
    testCompile "org.mockito:mockito-core:1.9.5" 
} 

자세한 내용 여기 : Unit testing support - Android Tools Project Site

이 안드로이드 스튜디오 1.1 이상과 안드로이드 Gradle을 1.1.0+

종속성 지금 testCompile로 선언 할 수 있습니다 플러그인 버전에서 지원됩니다.

0

이는 나를 위해 일한 무엇을 보낸 사람 공식적인 단위 테스트 지원이

androidTestCompile 'net.bytebuddy:byte-buddy-android:0.7.8' 
관련 문제