美文网首页
15_Android测试

15_Android测试

作者: 刘加城 | 来源:发表于2023-01-25 16:13 被阅读0次

    一直以来,对Android App的测试部分是有所忽视的。对它的了解得并不深入,也不全面,每次都是浅尝辄止,精力主要集中在功能实现上。毕竟,有专门的测试人员来完成这一工作。但话又说回来,他们大多数是基于黑盒进行手动测试的,主要验证需求是否实现和一些边界是否异常等。对于白盒测试,更多则需要开发者来完成。本篇文章就来稍微弥补这一空缺。此外,保证一些重要、核心的方法在长久的迭代、升级过程中的正确性,不被意外修改或影响,是非常重要的。这一点可以通过特定的白盒测试用例来实现。

(1)分类

    根据不同的粒度,测试可以分为单元测试、集成测试和大型测试。

  1. 单元测试:对App小部分的测试,如一个方法或一个类;
  2. 集成测试:也叫中等测试(Medium tests),由两个或多个单元测试构成;
  3. 大型测试:Big tests,也叫端对端测试(End-to-End test),对App多个部分进行的测试,如一个用户操作流程、一个显示过程等;

(2)androidTest和test

    使用Android Studio(后文简称AS)创建新Project时,会有两个目录:

  1. androidTest:也叫插桩测试(Instrumented tests),运行在真机或模拟器上的测试;它通常是UI测试,启动一个app,测试相关的交互;
  2. test:单元测试,也叫本地测试(local tests),一般在本地PC上完成;因此,还称它为host-side tests。

    在build.gradle中,两个相关的依赖项:

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'

    几点说明:

  1. testImplementation:表示使用的依赖项仅为了单元测试;
  2. androidTestImplementation:表示使用的依赖项仅为了插桩测试(Instrumented tests);

(3)AndroidX Test API

    如果编写的单元测试依赖于Android框架,那么可以使用与设备无关的统一API,如androidx.test API。
    如果测试依赖于资源,那么需要在build.gradle中配置:

android {
        // ...
        testOptions {
            unitTests {
                includeAndroidResources = true
            }
        }
    }

    一个基本AndroidX Test的示例:

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.xxx.xxx", appContext.packageName)
    }
}

    作为对比,一个不使用AndroidX的基本示例:

import org.junit.Test;
import static org.junit.Assert.*;

class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }
}

(4)Robolectric简介

    上面的AndroidX API 是和Android真机或模拟器关联的,而Robolectric是一种PC上模拟Android运行环境的工具。真机或模拟器有时候会很慢,比如冷启动一个大App首页就要很长时间。Robolectric就是针对这一点做的改进,以加快测试速度。
    如果想使用Robolectric,需要在build.gradle添加如下配置:

android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}

dependencies {
  testImplementation 'junit:junit:4.13.2'
  testImplementation 'org.robolectric:robolectric:4.9'
}

    具体使用示例:

@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {

  @Test
  public void clickingButton_shouldChangeMessage() {
    try (ActivityController<MyActvitiy> controller = Robolectric.buildActivity(MyActvitiy.class)) {
      controller.setup(); // Moves Activity to RESUMED state
      MyActvitiy activity = controller.get();

      activity.findViewById(R.id.button).performClick();
      assertEquals(((TextView) activity.findViewById(R.id.text)).getText(), "Robolectric Rocks!");
    }
  }
}

    官方地址:http://robolectric.org/

(5)Espresso简介

     Espresso是AndroidX Test API中的一部分,提供准确的、可信赖的UI测试。基于它,可以做一些自动化测试。
     配置如下:

dependencies {
        ...
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    }

     基本使用如下:

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.matcher.ViewMatchers.withId

@Test
fun greeterSaysHello() {
    onView(withId(R.id.name_field)).perform(typeText("Steve"))
    onView(withId(R.id.greet_button)).perform(click())
}

     Espresso的具体内容非常多,感兴趣的朋友可以去找官方文档学习。

(6)UI Automator简介

    UI Automator是一个可以跨app的UI测试框架。通过使用它的一些API,可以与屏幕上可见的View进行交互。它并不局限于焦点Activity,可以操作桌面launcher上任何一项。通过类名、文字或内容描述等来找到屏幕上的元素。
    build.gradle中添加依赖项:

dependencies {
        ...
        androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    }

    使用示例:

import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.UiObject
import androidx.test.uiautomator.UiCollection

//示例1
val device = UiDevice.getInstance(getInstrumentation())
device.pressHome()

// Bring up the default launcher by searching for a UI component
// that matches the content description for the launcher button.
val allAppsButton: UiObject = device.findObject(UiSelector().description("Apps"))

// Perform a click on the button to load the launcher.
allAppsButton.clickAndWaitForNewWindow()

//示例2
val videos = UiCollection(UiSelector().className("android.widget.FrameLayout"))
// Retrieve the number of videos in this collection:
val count = videos.getChildCount(
  UiSelector().className("android.widget.LinearLayout")
)

// Find a specific video and simulate a user-click on it
val video: UiObject = videos.getChildByText(
  UiSelector().className("android.widget.LinearLayout"),
  "Cute Baby Laughing"
)
video.click()

    对于带滚动功能的控件如ListView,操作如下:

 val settingsItem = UiScrollable(UiSelector().className("android.widget.ListView"))
 val about: UiObject = settingsItem.getChildByText(UiSelector().className("android.widget.LinearLayout"),"About tablet")
 about.click()

    快速定位某个View有一个快捷的方式,尤其对不熟悉的复杂页面,即使用uiautomatorviewer命令,它可以展示UI对应层次、类名、id等。根据这些信息,就可以像上面代码示例中那样获取对应的UiObject,然后对它进行一些操作。
    在命令行运行:uiautomatorviewer(<android-sdk>/tools/目录下),就可以打开一个可视界面。点击左上角按钮(Screen Shot)对手机进行截图,截图内容会展示在左边的面板,移动鼠标到不同的UI元素上,右边的面板就会展示出对应的信息,有页面的层次结构、类名、resource-ID等。值得注意的是:该命令在Java 8运行良好,在Java 11、12、19都有问题(亲测,Android SDK已升级到最新),报错如下:

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

    此外,该工具是基于View体系的,如果使用了Jetpack Compose,很多的页面层次结构是无法展示出来的,特别是各种充当ViewGroup功能的组件,如Row、Column等。

(7)自动化测试简介

    自动化测试属于Instrumented test,使用上面介绍的Espresso或UI Automator,将用户的手动操作转换为特定步骤的测试代码,就可以实现自动化测试。
    这些特定的步骤,每一步都必须基于一个确定的行为。如果某一步存在不确定性,那么后续步骤就无法进行。例如某一步骤依赖接口返回,这存在断网、接口缓慢或者服务器错误等异常情况,就使得后续的步骤无法执行。
    Espresso保证只有在UI 空闲(此时主线程等待,已交出cpu时间)的情况下,才执行下一个步骤(术语:同步)。这在一定程度上减少了不确定性,但并不能解决所有的问题,如上面接口的异常等。一种解决方案是等待固定的时间,如2s,再执行下一步。

(8)Compose测试

    Espresso或UI Automator对于Compose并不适用,Compose的测试需要对应的测试框架。添加依赖项:

// Test rules and transitive dependencies:
androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_version")
// Needed for createAndroidComposeRule, but not createComposeRule:
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")

    使用示例:

class MyComposeTest {

    @get:Rule
    val composeTestRule = createComposeRule()
    // use createAndroidComposeRule<YourActivity>() if you need access to
    // an activity

    @Test
    fun myTest() {
        // Start the app
        composeTestRule.setContent {
            MyAppTheme {
                MainScreen(uiState = fakeUiState, /*...*/)
            }
        }

        composeTestRule.onNodeWithText("Continue").performClick()

        composeTestRule.onNodeWithText("Welcome").assertIsDisplayed()
    }
}

    与元素交互的方式有三种:

  1. 查找器(Finder):选择页面元素;
  2. 断言(Assert):验证元素是否有某些属性;
  3. 行为(Action):在元素上注入模拟的用户事件。

    onNodeWithText()是常见的查找元素方式。onNode()、onAllNode()分别选择一个和多个元素,示例如下:

composeTestRule.onNode(hasText("Button")) // Equivalent to onNodeWithText("Button")

composeTestRule .onAllNodes(hasText("Button")) // Equivalent to onAllNodesWithText("Button")

(9)命令行执行测试

    AS上可以方便的运行测试,在相应的测试文件上右键点击,出现“Run ...”选项,点击它即可。除此之外,还可以使用命令行来运行,如下:

./gradlew test :执行所有的单元测试;
./gradlew connectedAndroidTest :执行所有的Instrumented测试,可以简写为./gradlew cAT,第一个单词首字母小写,后续单词首字母大写;
./gradlew myLib:connectedAndroidTest :执行myLib模块的所有的Instrumented测试。

    Over !

相关文章

  • 测试

    测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试

  • 测试

    测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测...

  • 哈哈哈哈

    测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测...

  • 无标题文章

    测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测...

  • 测试

    测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测...

  • 测试

    测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测...

  • 2019-08-27

    测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测...

  • 18个葫芦娃

    测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测...

  • 测试测试

    测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试 测试测试测试测试测试测试 测试测试测试测...

  • 测试

    测试测试测试 测试测试测试 测试测试测试 测试测试测试 测试测试测试 测试测试测试 测试测试测试 测试测试测试 测...

网友评论

      本文标题:15_Android测试

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