美文网首页
如何进行Intrumentation Test

如何进行Intrumentation Test

作者: yangweigbh | 来源:发表于2017-10-17 01:01 被阅读17次

    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/

    或者手动运行:

    1. rebuild 主程序和test程序.
    2. 在真机上安装主程序和test程序
    3. 命令行运行adb shell am instrument -w com.android.foo.test/android.support.test.runner.AndroidJUnitRunner

    相关文章

      网友评论

          本文标题:如何进行Intrumentation Test

          本文链接:https://www.haomeiwen.com/subject/htkruxtx.html