1、配置测试环境
在app:gradle下配置
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.testshellcmd"
minSdkVersion 14
targetSdkVersion 19
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:19.1.0'
// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support:support-annotations:19.1.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
}
2、在你要测试的类上,点击类名,右键,Go To --> Test
选择自动生成的方法以及要被测方法之后,选择测试类的存放路径
3、补充测试类
4、配置manifest文件,添加
<!-- 单元测试配置 -->
<uses-library android:name="android.test.runner" />
5、运行测试类,Run -- Edit Configurations... ,点击+号,选择Android Tests,在右侧完善信息
最后AS的工具栏中选择该测试类,并点击绿色三角形运行即可