zoukankan      html  css  js  c++  java
  • 【Android测试】【第十五节】Instrumentation——官方译文

    版权声明:本文出自胖喵~的博客,转载必须注明出处。

       转载请注明出处:http://www.cnblogs.com/by-dream/p/5482207.html

    前言 


      前面介绍了不少Android UI自动化测试的东西,这里我们学习一下谷歌对安卓测试的一些理解。顺便做为Instrumentation的预习篇。原文章的链接:http://developer.android.com/intl/zh-cn/tools/testing-support-library/index.html

      这篇文章介绍了Android App的关键概念。它假设你已经有了JUnit的测试框架的一些基本知识。

    测试结构


      Android testing 测试框架是基于JUnit的。一般情况下,一个JUnit是一个方法 语句测试应用程序的一部分。你写一些测试方法到一个类里,就被称做是test case。当然你可进一步的组织这类到测试套件(test suites)。

      在JUnit当中,你建立一个或者多个测试类,并使用测试运行器(test runner)来执行它们。在Android中,你需要使用Android Studio(或 Android Plugin for Gradle)去建立一个或多个源文件到一个Android的测试App中。

      根据你的环境,你可以选择以下方式之一运行测试:

      1、在你本地机器上:编译测试类和使用JUnit test runner去调起他们执行在本地的JVM上。

      2、在模拟器或Android设备上:安装测试程序到设备上,然后用Android特有的test runner(例如 AndroidJUnitRunner)去执行你的测试。

      你的测试代码和你建立并运行Android Studio中的测试方式的结构取决于测试你正在执行的类型。下表总结了常见Android的测试类型的:

    Type Subtype Description
    Unit tests Local Unit Tests Unit tests that run on your local machine only. These tests are compiled to run locally on the JVM to minimize execution time. Use this approach to run unit tests that have no dependencies on the Android framework or have dependencies that mock objects can satisfy.
    Instrumented unit tests Unit tests that run on an Android device or emulator. These tests have access toInstrumentation information, such as the Context of the app under test. Use this approach to run unit tests that have Android dependencies which mock objects cannot easily satisfy.
    Integration Tests Components within your app only This type of test verifies that the target app behaves as expected when a user performs a specific action or enters a specific input in its activities. For example, it allows you to check that the target app returns the correct UI output in response to user interactions in the app’s activities. UI testing frameworks like Espresso allow you to programmatically simulate user actions and test complex intra-app user interactions.
    Cross-app Components This type of test verifies the correct behavior of interactions between different user apps or between user apps and system apps. For example, you might want to test that your app behaves correctly when the user performs an action in the Android Settings menu. UI testing frameworks that support cross-app interactions, such as UI Automator, allow you to create tests for such scenarios.

      根据你创建的测试类型,你需要按照《Getting Started with Testing》中描述的在Android Studio中配置你测试代码的路径和项目依赖。

    Testing APIs


      下面总结了Android测试相关的公共API。 

      Junit

        你在编写单元测试或者集成测试类时需要把它作为Jnit 4的类, JUnit是Java中最流行和广泛使用的单元测试框架。该框架提供了一个方便的方法去在你的应用中调用setup, teardown 和 assertion 。

        一个基本JUnit 4测试类是包含一个或多个Java测试类。一个测试方法是以一个@Test的标注开始,代码的内容是就是验证要测试组件的单一功能(也就是一个逻辑单元)。

        下面的代码片段显示了使用Espresso API来执行UI元素上点击动作的JUnit 4集成测试的一个例子,这个例子是来检查是否显示了预期的字符串。

     1 @RunWith(AndroidJUnit4.class)
     2 @LargeTest
     3 public class MainActivityInstrumentationTest {
     4 
     5     @Rule
     6     public ActivityTestRule mActivityRule = new ActivityTestRule<>(
     7             MainActivity.class);
     8 
     9     @Test
    10     public void sayHello(){
    11         onView(withText("Say hello!")).perform(click());
    12 
    13         onView(withId(R.id.textView)).check(matches(withText("Hello, World!")));
    14     }
    15 }

        你可以使用Junit的Assert类来验证对象状态的正确性,通过断言的方法来比较值,当实际结果与预期结果不一致的时候抛出异常。更多详细的断言内容可以参考Assertion classes(断言类)。

      Instrumentation

        Android Instrumentation在安卓系统上是一组控制函数或者是hooks(钩子)。这些钩子在自己的生命周期独立的控制一个安卓组件,他们也控制着安卓如何加载应用程序。

        下图总结了Instrumentation的测试框架:

        通常情况下,Android的一个组件在运行在系统指定的生命周期中。举个例子,一个Activity对象的生命周期开始就是被Intent激活的时候,系统调用该对象的onCreate()方法,然后调用onResume()方法,当用户在切换到别的应用的时候,系统又调用onPause()方法,如果在Activity的代码中调用finish()方法时,系统则会调用的onDestroy()方法。Android框架的API不提供对你的代码直接调用这些回调函数,但你可以通过Instrumentation来完成。

        系统运行一个应用的所有组件都是在同一个进程中,你可以让某些组件(例如content providers)在单独的进程中运行,但是你不能强制让一个应用程序和另一个已经运行的程序运行在同一个进程中。

        Instrumentation可以同时加载。一旦你的应用程序和你的测试程序在一个进程当中了,你的测试程序就可以调用组件中的方法,并且在组件中修改和验证变量。

    Android Testing Support Library APIs


      The Android Testing Support Library 提供了一系列的API,可以让你快速的建立和运行你的测试程序,包括JUnit4和功能层面的用户界面(UI)测试。下面这些库都是基于Instrumentation的,你可以在做自动化测试的时候选择它们:

      AndroidJUnitRunner:运行在安卓上的兼容JUnit 4的test runner;

      Espresso:UI测试框架,适用于在App内的UI功能测试;

      UIAutomator:UI测试框架,适用于跨应用的UI功能测试。

  • 相关阅读:
    博客收藏
    日常开发工具收藏
    2016工作总结
    REST设计规则
    spring boot 学习笔记(二) 构建web支持jsp
    数据库事务中的隔离级别和锁+spring Transactional注解
    springboot学习笔记(一)
    Python爬虫实例(四)网站模拟登陆
    Python爬虫实例(三)代理的使用
    Python爬虫实例(二)使用selenium抓取斗鱼直播平台数据
  • 原文地址:https://www.cnblogs.com/by-dream/p/5482207.html
Copyright © 2011-2022 走看看