我有
android项目和
android测试项目里面,位于文件夹测试下.这些项目的结构就像日食一样
-src
| – res
| – libs
| – 测试
| …..
我用毕业生所有我想要的是构建应用程序,运行单元测试并获取报告.但我不明白如何做到正确.我在根文件夹中创建了build.gradle文件
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.5.+' } } apply plugin: 'android' android { compileSdkVersion 18 buildToolsVersion "18.1.1" defaultConfig { minSdkVersion 8 targetSdkVersion 18 testPackageName "ua.cooperok.stringcalc.tests" testInstrumentationRunner "android.test.InstrumentationTestRunner" } sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } unitTest { java.srcDir file('tests/src') resources.srcDir file('tests/res') } } configurations { unitTestCompile.extendsFrom runtime unitTestRuntime.extendsFrom unitTestCompile } dependencies { unitTestCompile files("$project.buildDir/classes/release") } task unitTest(type:Test){ description = "Run unit tests..." testClassesDir = android.sourceSets.unitTest.output.classesDir classpath = android.sourceSets.unitTest.runtimeClasspath } build.dependsOn unitTest }
但是当我运行gradle构建我有错误
在源设置单元测试中找不到属性’output’.
另外,据了解,要获得报告,我需要应用插件“android报告”,但是当我这样做我有错误
无法添加名称为“android”的扩展名,因为已经注册了该名称的扩展名.
我应该这样做,还是我必须在我的测试应用程序项目中创建新的gradle.build文件?
UPDATE
我固定错误与输出,并使得gradle构建成功,但我不明白什么是goind.我故意尝试失败,但毕业生仍然是成功的.
我做了什么
我把任务unitTest从android部分移到root,之后我的测试项目开始编译,但是有一个错误’packackage android.test not found’
2.为了解决这个错误,我添加到android路径的路径,所以在这个gradle构建成功之后,但我真的不明白为什么总是成功,即使测试失败
而我仍然不明白如何从测试中得到报告
这是我的新build.gradle文件
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6+' } } apply plugin: 'android' android { compileSdkVersion 18 buildToolsVersion "18.1.1" defaultConfig { minSdkVersion 8 targetSdkVersion 18 testPackageName "ua.cooperok.stringcalc.tests" testInstrumentationRunner "android.test.InstrumentationTestRunner" } sourceSets { main { manifest.srcFile file('AndroidManifest.xml') java.srcDirs = ['src'] resources.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } } dependencies { compile fileTree(dir: 'libs',include: '*.jar') } } sourceSets { unitTest { java.srcDirs = ['tests/src'] resources.srcDirs = ['tests/src'] } } configurations { unitTestCompile.extendsFrom runtime unitTestRuntime.extendsFrom unitTestCompile } dependencies { unitTestCompile files("$project.buildDir/classes/release") unitTestCompile 'junit:junit:4.11' compile fileTree(dir: 'libs',include: '*.jar') } task unitTest(type:Test,dependsOn: assemble) { description = "run unit tests" testClassesDir = sourceSets.unitTest.output.classesDir classpath = files("$System.env.ANDROID_HOME/sources/android-18") //actualy I didn't get any report getReports().getJunitXml().setOutputPerTestCase(true) } build.dependsOn unitTest
解决方法
我找到解决方案我被几个话题搞砸了,觉得我需要单独测试单独的任务.其实这很容易.
我没有改变我的项目结构,只是build.gradle文件.要构建应用程序,我运行
gradle clean connectedCheck构建
connectedCheck从我的测试项目运行单元测试,如果某些测试失败 – gradle build也将失败
我没有改变我的项目结构,只是build.gradle文件.要构建应用程序,我运行
gradle clean connectedCheck构建
connectedCheck从我的测试项目运行单元测试,如果某些测试失败 – gradle build也将失败
这里的最终版本
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6+' } } apply plugin: 'android' android { compileSdkVersion 18 buildToolsVersion "18.1.1" defaultConfig { minSdkVersion 8 targetSdkVersion 18 testPackageName "ua.cooperok.stringcalc.tests" testInstrumentationRunner "android.test.InstrumentationTestRunner" } sourceSets { main { manifest.srcFile file('AndroidManifest.xml') java.srcDirs = ['src'] resources.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } instrumentTest { java.srcDirs = ['tests/src'] manifest.srcFile file('tests/AndroidManifest.xml') resources.srcDirs = ['tests/src'] res.srcDirs = ['tests/res'] assets.srcDirs = ['tests/assets'] } } dependencies { compile fileTree(dir: 'libs',include: '*.jar') } //Signing apk if(project.hasProperty("signingPropertiesPath")) { File propsFile = new File(System.getenv('HOME') + "/" + project.property("signingPropertiesPath")) if(propsFile.exists()) { Properties props = new Properties() props.load(new FileInputStream(propsFile)) //loading keystore properties signingConfigs { release { storeFile file(propsFile.getParent() + "/" + props['keystore']) storePassword props['keystore.password'] keyAlias props['keyAlias'] keyPassword props['keyPassword'] } } buildTypes { release { signingConfig signingConfigs.release } } } } }