如何在 Android Studio 中构建项目测试目录? [英] How do I structure project test directory in Android Studio?
问题描述
对此还没有约定,但是既然
There is no convention for this yet, but how do I structure the test directory for Android Studio, now that what's stated on the Android testing fundamentals page differs?
Particularly, any pointers on how to get jUnit tests up and running on Android Studio would be helpful, thanks.
Also, using the android
tool does not really help now, since things are a bit different with Android Studio.
Update:
I tried setting up the test folder and running it, but all I'm getting is the following:
Running tests
Test running startedTest running failed: Unable to find instrumentation info for:ComponentInfo{<project-package-name>/android.test.InstrumentationTestRunner}
Empty test suite.
I've also tried adding a standard AndroidManifest.xml file for tests in there.
UPDATE
Starting from Build Tools 19.1.0 and build plugin 0.11.0 build.gradle files needs to have testPackageName renamed to testApplicationId ( also packageName should be renamed to androidId)
As of build plugin 0.9.0 instrumentTest folder is renamed to androidTest. That's all we need for testing.
Here is example of 0.11.+ DSL
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
minSdkVersion 16
targetSdkVersion 19
versionCode 1
versionName "1.0"
androidId "org.homelab.lab"
testApplicationId "org.homelab.lab.test"
testInstrumentationRunner "org.homelab.lab.test.Runner"
}
...
}
GOTCHAS : if your build file consists definitions of testPackageName and testInstrumentationRunner, remove them
For version 0.5.0 - 0.8.+
Android Studio uses Gradle plugin version 0.5.+ which follows Gradle SourceDir principles.
How to make it work:
1.update SDK
2.install or update Gradle to 1.6 (reported problems with 1.7) or stick with gradle wrapper
3.don't use Android Studio for running instrumentation task, use gradle command
gradle connectedCheck
4.don't use same package for test and main apk
5.check results using browser
<project>/build/reports/instrumentTests/index.html
Gotchas:
If test package and main package are the same it may create empty TestSuite. Result is misleading as Gradle reports no problems but reports show that no Class has been tested.
EDIT:
Below is the part of build.gradle which configures instrument tests required before 0.9.0:
android {
compileSdkVersion 14
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 17
testPackageName "org.homelab.lab.test"
testInstrumentationRunner "org.homelab.lab.test.Runner"
}
...
}
example project https://github.com/swavkulinski/android-studio-instrumentation-test
这篇关于如何在 Android Studio 中构建项目测试目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!