2015-01-15 2 views
2

나는 Junit 3을 기반으로 한 기존 AndroidTestCase 유닛 테스트가 포함 된 Android Studio 프로젝트를 가지고 있습니다. AndroidTestCase 테스트를 대체하지 않고 보완하기 위해 robolectric 테스트를 소개합니다. 동일한 프로젝트에서 Junit 3 기반 AndroidTestCase 테스트와 Junit 4 기반 Robolectric 테스트를 모두 사용할 수 있습니까? 대나무 CI 시스템에서 둘 다 실행할 수 있습니까? 이를 지원하도록 프로젝트를 어떻게 구성합니까?동일한 Android 프로젝트에서 Junit 3과 Junit 4를 모두 사용할 수 있습니까?

+1

나는 클래스를 필터링하거나 다른 폴더에 넣을 수 있으며, android 테스트를 빌드하고 실행할 때 robolectric 테스트 클래스를 제거 할 수 있습니다. robolectric 테스트를 실행할 때 안드로이드 테스트를 필터링 할 수 있습니다. 그러나 동시에이 프로젝트의 설치가 구현, 이해 및 지원하기가 어려울 것으로 기대합니다. 나는 다른 모듈에서 안드로이드 나 로보트 테스트를 옮길 것이지만 같은 프로젝트에서 안드로이드와 로보 테스트를하는 데 많은 경험이 없다. –

답변

1

같은 프로젝트에서 Robolectric 테스트를 별도의 모듈에 넣는 것이 좋습니다.

새 모듈을 만들려면 파일 -> 새 모듈 -> Java 라이브러리로 이동하십시오. 해당 모듈을 만들 때, 그 build.gradle 파일로 이동 한 다음과 같은 라인의 내용을 대체 :이 Robolectric 시험 선 (善)과 모듈을 청구

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" 
} 

과 좋은 출발점 역할을한다.

: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; 
      } 
     }; 
    } 
} 

그럼 그냥 예를 들어,이 주자와 테스트 클래스에 주석 : Robolectric에서

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

테스트 모듈은 gradlew test으로 실행할 수 있습니다.

Bamboo와 관련하여 AFAIK에는 사용자 지정 스크립트를 실행할 가능성이 있으므로 그냥 gradlew test을 입력하고 어떻게 진행되는지 확인하십시오.

관련 문제