2014-05-09 134 views
8

我有一個簡單的HelloWorld Android項目(內置於Eclipse IDE中),我可以在此項目的cmd提示符下成功執行「gradle build」。編寫Gradle腳本爲Eclipse運行單元測試用例Android測試項目

另外我也爲它編寫了一個簡單的JUnit Android測試項目,並且它在Eclipse中運行良好。

現在我想運行這個測試項目或單元測試用例(如果我的理解是錯誤的!)使用Gradle腳本。 我該怎麼做?

以下是我正在使用的build.gradle文件。我想知道如何編寫腳本代碼來自動執行測試用例。

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:0.10.+' 
    } 
} 
apply plugin: 'android' 

dependencies { 
    compile fileTree(dir: 'libs', include: '*.jar') 
} 

android { 
    compileSdkVersion 19 
    buildToolsVersion "19.0.3" 

    sourceSets { 
     main { 
      manifest.srcFile 'AndroidManifest.xml' 
      java.srcDirs = ['src'] 
      resources.srcDirs = ['src'] 
      aidl.srcDirs = ['src'] 
      renderscript.srcDirs = ['src'] 
      res.srcDirs = ['res'] 
      assets.srcDirs = ['assets'] 
     } 
     test { 
      java.srcDirs = ['src/test/java'] 
      resources.srcDirs = ['src/test/resources']   
     } 

     // Move the tests to tests/java, tests/res, etc... 
     instrumentTest.setRoot('tests') 
     androidTest.setRoot('tests') 

     // Move the build types to build-types/<type> 
     // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... 
     // This moves them out of them default location under src/<type>/... which would 
     // conflict with src/ being used by the main source set. 
     // Adding new build types or product flavors should be accompanied 
     // by a similar customization. 
     debug.setRoot('build-types/debug') 
     release.setRoot('build-types/release') 
    } 
} 
+0

'instrumentTest'重命名爲'androidTest',請參閱https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.enide的歷史記錄.editors.gradle /文檔/安卓/編譯。gradle –

回答

0

這裏是我使用:

android { 
    // ... 
    // your stuff 
    // ... 

    // So Android Studio can nicely edit the test files 
    sourceSets { 
     androidTest { 
      java.srcDir file('src/test/java') 
     } 
    } 
} 

// <Task to run tests> 
sourceSets { 
    unitTest { 
     java.srcDir file('src/test/java') 
    } 
} 

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 
} 

check.dependsOn unitTest 
// </Task to run tests> 

dependencies { 
    // ... 
    // compile stuff 
    // .... 

    unitTestCompile files("$project.buildDir/classes/release") 
    unitTestCompile 'junit:junit:4.11' 
} 

運行測試:

gradlew unitTest 
+0

問題是有關Eclipse ADT(帶有經典源文件夾)以及如何運行Android Test項目(在Eclipse中創建) –

+0

儘管我投了棄權票,但仍然給予賞金作爲最佳答案(不能爲我自己判斷) –

0

該解決方案還沒有實現和測試。正在進行中,歡迎評論。

仔細閱讀所有user guide之後,顯然沒有Eclipse Eclipse項目的直接支持。原因在於新建系統採用Gradle(和Maven)風格在模塊/項目中進行單元測試。

但因爲Eclipse的Android測試項目仍是Android項目,有方式來嘗試

  1. 添加build.gradle見下文到Eclipse的Android測試項目。
    它應該有compile project(':app')dependencies

    /*  Android 
    * http://www.nodeclipse.org/projects/gradle 
    * Nodeclipse/Enide build.gradle template for classic Android project 
    * https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.enide.editors.gradle/docs/android/build.gradle 
    * Gradle Plugin User Guide: 
    * http://tools.android.com/tech-docs/new-build-system/user-guide 
    * Usage 
    * 1. put in project root 
    * 2. check version numbers 
    * 3. use from command line or with Gradle for Android http://marketplace.eclipse.org/content/gradle 
    * Support for this template 
    * https://github.com/nodeclipse/nodeclipse-1/issues/ 
    * History 
    * 2014-03-13 android plugin updated to 0.9, see http://tools.android.com/tech-docs/new-build-system/migrating_to_09 
    * 2014-04-01 check for gradle version 
    * 2014-04-10 wrapper and runAndroidApplication tasks 
    * 2014-04-25 rename to run, add << 
    * 2014-05-23 defaut plugin version 0.10.x 
    * 2014-06-06 show "boilerplate part" 
    * @author Paul Verest 
    */ 
    println GradleVersion.current().prettyPrint() 
    assert gradle.gradleVersion >= "1.10" // android plugin 0.10 requires Gradle 1.10, 1.11, 1.12 
    // after `gradle wrapper` it is possible to use './gradlew build' with Gradle version specified 
    task wrapper(type: Wrapper) { 
        gradleVersion = '1.12' 
    } 
    
    //{ "boilerplate part" 
    buildscript { 
        repositories { 
         mavenCentral() 
         //jcenter() 
        } 
        dependencies { 
         classpath 'com.android.tools.build:gradle:0.10.+' 
        } 
    } 
    apply plugin: 'android' 
    
    dependencies { 
        compile fileTree(dir: 'libs', include: '*.jar') 
        //androidTestCompile 'com.jayway.android.robotium:robotium-solo:4.3.1' 
    
        // for multi-module project build (needs `settings.gradle`): 
        // reference needed modules or App under test (for Eclipse Android Test project) 
        compile project(':app') 
    } 
    
    android { 
        compileSdkVersion 19 
        buildToolsVersion "19.0.3" 
    
        sourceSets { 
         main { 
          manifest.srcFile 'AndroidManifest.xml' 
          java.srcDirs = ['src'] 
          resources.srcDirs = ['src'] 
          aidl.srcDirs = ['src'] 
          renderscript.srcDirs = ['src'] 
          res.srcDirs = ['res'] 
          assets.srcDirs = ['assets'] 
         } 
    
         // Move the tests to tests/java, tests/res, etc... 
         androidTest.setRoot('tests') 
    
         // Move the build types to build-types/<type> 
         // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... 
         // This moves them out of them default location under src/<type>/... which would 
         // conflict with src/ being used by the main source set. 
         // Adding new build types or product flavors should be accompanied 
         // by a similar customization. 
         debug.setRoot('build-types/debug') 
         release.setRoot('build-types/release') 
        } 
    } 
    //} "boilerplate part" 
    
  2. 由於APP及其測試現在做多模塊項目添加settings.gradle。最簡單的方法把它降低1級(文件夾中的兩個項目都是)

    include ':app' 
    include ':apptest' 
    

    其他方法是放在兄弟文件夾,例如,所謂parent

    includeFlat 'app' 
    includeFlat 'apptest' 
    

    在後一種情況下,你將需要運行該文件夾中建立(或每次用-cCLI option

更新關於與gradle這個純JUnit測試做研究指定settings.gradle位置(位對於這個問題而言,這似乎就像Android Tooling團隊忽略了這個問題,打破了其他開發者正在試圖用新的Android gradle插件做的事情,甚至在Android Studio中也不是如此,即使是在How can I create tests in Android Studio?之間。其框架已準備好進行純JUnit測試的butors https://github.com/robolectric/gradle-android-test-plugin

相關問題