1.
在Module中创建androidTest
文件夹
module-name/src/androidTest/java/
build.gradle中加入
dependencies {
androidTestCompile 'com.android.support:support-annotations:24.0.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
// Optional -- UI testing with UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
如果用gradle运行instrumentation test,则加入以下配置
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
2. 创建Instrumented class
@RunWith(AndroidJUnit4.class)
@SmallTest
public class SampleAndroidUnitTest {
@Before
public void setup() {
}
@Test
public void test1() {
}
}
如何获取instrumentation
InstrumentationRegistry.getInstrumentation()
3.创建test suite
package name 以.suite结尾
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorInstrumentationTest.class,
CalculatorAddParameterizedTest.class})
public class UnitTestSuite {}
4.运行
./gradlew connectedAndroidTest
html 结果:
path_to_your_project/module_name/build/outputs/reports/androidTests/connected/
或者手动运行:
- rebuild 主程序和test程序.
- 在真机上安装主程序和test程序
- 命令行运行
adb shell am instrument -w com.android.foo.test/android.support.test.runner.AndroidJUnitRunner
网友评论