我有一个基于Junit 3的现有AndroidTestCase单元测试的Android Studio项目。我想探索引入robolectric测试,而不是替换,但补充AndroidTestCase测试。我可以在同一个项目中同时使用基于Junit 3的AndroidTestCase测试和基于Junit 4的robolectric测试吗?我可以在我的Bamboo CI系统上运行它们吗?如何配置项目以支持它?
答案 0 :(得分:1)
我建议将Robolectric测试放在同一项目下的单独模块中。
要创建新模块,请转到文件 - >新模块...... - > Java库。创建该模块后,导航到其build.gradle
文件,并使用以下行替换其内容:
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"
}
这使用Robolectric测试装置为模块充电,应该是一个很好的起点。
请注意,: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;
}
};
}
}
然后只使用此运行器注释测试类,例如:
@RunWith(ApplicationTestRunner.class)
public class PriorityExecutorSchedulerTest {
....
}
Robolectric模块的测试可以使用gradlew test
运行。
关于Bamboo,AFAIK可以运行自定义脚本,因此我只需将gradlew test
放入其中,看看它是怎么回事。