相关文章推荐
神勇威武的杨桃  ·  IDEA 单元测试Junit ...·  1 周前    · 
俊逸的匕首  ·  编译器错误 CS1540 - C# ...·  3 周前    · 
乐观的皮带  ·  pytorch推理 ...·  1 月前    · 
聪明伶俐的芒果  ·  docker - Alpine ...·  1 年前    · 
越狱的鸵鸟  ·  Laravel读取Excel_start ...·  1 年前    · 
谦虚好学的脸盆  ·  cin/cout and other ...·  1 年前    · 

androidTest 和 Test 目录

每次创建一个 module ,我们都会发现在目录下有 androidTest 以及 Test 两个文件夹, androidTest Test 都是对程序的内部进行测试,而非其是否符合预期的功能要求,这些test代码均不会打包到最终的apk之中

  • androidTest:需要设备环境才能进行的测试
  • Test:不依赖设备环境,可以在PC上单独进行测试
  • JUnit

    JUnit是一套基于注解的单元测试框架,在Test目录下的测试大都基于此框架实现,此类测试不依赖于设备环境,可以运行在本地JVM中的单元测试

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    
  • @Test:注明该方法为测试方法,该方法只能public void,即kotlin中只使用fun+函数定义方法即可
  • @Before:在每个测试方法执行前调用一次
  • @After:每个测试方法执行后调用一次
  • @BeforeClass:在所有方法执行前调用一次,只会执行一次,该注解的方法必须是public static void,即kotlin中添加@JvmStatic注解并使用fun+函数定义方法
  • @AfterClass:条件对应@BeforeClass,所有方法执行后调用一次
  • @Ignore:忽略该测试方法,当不想使用该方法时可添加该注解
  • @RunWith:表明类的测试运行器 注:在JUnit5版本中,可通过使用@TestInstance(Lifecycle.PER_CLASS)注解声明每个方法都是用一个测试类实例,即不需要每个方法都使用一次@JvmStatic
  • void assertEquals(boolean expected, boolean actual):检查两个变量或者等式是否平衡
    void assertTrue(boolean expected, boolean actual):检查条件为真
    void assertFalse(boolean condition):检查条件为假
    void assertNotNull(Object object):检查对象不为空
    JUnit中,通过使用注解和断言,可以编写相应的逻辑去进行测试,当需要测试某一个方法时,只需要在该方法上右键=>run"xxx()"即可执行,而需要测试一个类里的所有方法时,则对类进行相同的操作即可

    Espresso

    核心 API 小巧、可预测且易于学习,但仍可进行自定义。Espresso 测试会清楚地说明预期、交互和断言,不受样板内容、自定义基础架构或杂乱的实现细节干扰。 Espresso 测试运行速度极快!当它在静止状态下对应用界面进行操纵和断言时,无需等待、同步、休眠和轮询。
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'
    
  • 设置插桩测试运行程序
    android.defaultConfig中添加以下
  • testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"    
    
  • onView示例
    通过Espresso中的onView方法,可以访问目标应用中的组件并与之交互
  • onView(withId(R.id.my_button))
    

    onView通过查找id为R.id.my_button的按钮,如果匹配成功,则返回一个引用,可供用户基于视图执行断言等操作

    有两种方法,第一种是通过Android studio创建测试配置并运行,具体方法可以在官方文档查看 第二种则是通过执行Gradle命令运行测试
        ./gradlew connectedAndroidTest    
    

    AndroidJUnitRunner

    在android开发中,方法中经常使用android系统api,比如Context,Parcelable,SharedPreferences等等。而在本地JVM中(JUnit4)无法调用这些接口,因此,我们就需要使用AndroidJUnitRunner来完成这些方法的测试,使用时我们需要在androidTest目录下创建测试类,并添加注解@RunWith(AndroidJUnit4.class)

        @RunWith(AndroidJUnit4::class)
        @LargeTest
        class ChangeTextBehaviorTest {
            val stringToBeTyped = "Espresso"
            @get:Rule
            val activityRule = ActivityTestRule(MainActivity::class.java)
            @Test fun changeText_sameActivity() {
                // 输入text并点击按钮
                onView(withId(R.id.editTextUserInput))
                    .perform(typeText(stringToBeTyped), closeSoftKeyboard())
                onView(withId(R.id.changeTextBt)).perform(click())
                // 查看text是否改变
                onView(withId(R.id.textToBeChanged))
                    .check(matches(withText(stringToBeTyped)))
    
  • 同JUnit,右键run对应函数或类即可(需要配置设备环境)
  • Android单元测试
    Android 单元测试(一) 之JUnit基础

    分类:
    Android
    标签: