码迷,mamicode.com
首页 > 移动开发 > 详细

Android 测试支持库介绍

时间:2015-11-21 11:45:26      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

测试支持库

Android的测试支持库为测试Android应用提供了大量框架。该库提供了一组API快速构建和运行测试代码,包括JUnit4和功能用户界面(UI)测试。可以从Android Studio IDE中或命令行这执行。

Android的测试支持库可通过Android SDK管理器获取。

测试支持库特性

AndroidJUnitRunner:兼容JUnit 4测试运行器。 Espresso:UI测试框架;适合在单个应用的功能UI测试。 UI Automator:UI测试框架;适用于跨应用的功能UI测试及安装应用。

AndroidJUnitRunner

AndroidJUnitRunner类是JUnit测试运行器,可以让你在Android设备上执行JUnit3或JUnit4中风格的测试类,兼容Espresso和UI Automator测试框架。测试运行器加载测试包和应用,运行测试并报告测试结果。该类取代 InstrumentationTestRunner类(仅支持JUnit 3)。

这个运行器的主要特点:

  • JUnit支持

  • 获得Instrumentation信息

  • 测试筛选

  • 测试分片

要求的Android2.2(API 8)或更高。

 

测试运行器兼容JUnit3和JUnit4的(最高JUnit4.10)测试。在同一个包混淆JUnit 3和和JUnit4测试代码可能会导致不可预测的结果。instrumented JUnit 4测试类在设备或仿真器上运行,必须在前面加上@RunWith(AndroidJUnit4.class)注释。

 

比如测试CalculatorActivity类中的加操作:

 

import android.support.test.runner.AndroidJUnit4;
import android.support.test.runner.AndroidJUnitRunner;
import android.test.ActivityInstrumentationTestCase2;

@RunWith(AndroidJUnit4.class)
public class CalculatorInstrumentationTest
        extends ActivityInstrumentationTestCase2<CalculatorActivity> {

    @Before
    public void setUp() throws Exception {
        super.setUp();

        // Injecting the Instrumentation instance is required
        // for your test to run with AndroidJUnitRunner.
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }

    @Test
    public void typeOperandsAndPerformAddOperation() {
        // Call the CalculatorActivity add() method and pass in some operand values, then
        // check that the expected value is returned.
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }
}

InstrumentationRegistry类可以访问测试运行的信息。该类包括Instrumentation对象,目标应用上下文对象,测试应用上下文对象及传递到测试的命令行参数。

JUnit 4.x的测试可以使用annotation来配置测试运行,并支持Androidannotation:

@RequiresDevice:物理设备上运行。

@SdkSupress:限定最低SDK版本。例如@SDKSupress(minSdkVersion=18)
@SmallTest,@MediumTest和@LargeTest:测试分级。

单个test suite可以分片,同一Instrumentation的同一分片可以作为一个组。每个片都有索引号。当运行测试,使用-e numShards选项指定片数和-e shardIndex选项来指定要运行的片。

例如分成10个碎片,仅执行第二片测试,请使用以下命令:

adb shell am instrument -w -e numShards 10 -e shardIndex 2

Espresso

Espresso提供了一组API来构建UI测试来测试用户流程。这些API让你写简洁和可靠运行的自动化UI测试。Espresso非常适合白盒自动测试,测试代码利用了被测应用的代码细节。

Espresso的主要特性:

  • 视图匹配(View matching): 灵活的API用于查看和适配目标应用。

  • Action API:一套扩展的action API自动化UI交互。

  • UI线程同步(UI thread synchronization)以提高测试的可靠性。

要求Android2.2(API 8)或更高。

Espresso.onView()方法可以访问目标应用程序的UI组件并与之交互。该方法接受一个Matcher参数,搜索视图层来定位视图实例。定位方法可以基于类名、内容描述、R.id、显示的文本。比如

onView(withId(R.id.my_button));


如果搜索成功,onView()方法返回对应view的引用,可执行用户操作和断言。

AdapterView由子view动态生成。如果目标视图在AdapterView(ListView或GridView)中,onView()方法可能不起作用,因为可能只加载了一部分,Espresso.onData()则可以。

ViewActions可以执行视图点击(View clicks),滑动(Swipes),按键或者按钮(Key and button press)、文本输入(Typing text)、打开链接(Opening a link)。

// Type text into an EditText view, then close the soft keyboard
onView(withId(R.id.editTextUserInput))
    .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
// Press the button to submit the text change
onView(withId(R.id.changeTextBt)).perform(click());

由于时间问题,在Android设备上测试随机失败。之前一般通过sleep和超时处理解决。Espresso测试框架处理Instrumentation和UI线程之间的同步,很好地解决了这些问题。

API参考:developer.android.com/reference/android/support/test/package-summary.html
测试参考:http://developer.android.com/training/testing/ui-testing/espresso-testing.html

 

UI Automator

UI Automator提供了一组API来构建基于交互UI的测试。API允许你执行操作,如打开设置菜单,非常适合黑盒自动化测试,测试代码不依赖于应用的内部实现。

主要特性:

  • UI Automator Viewer:检查的布局层次。

  • API来获取设备状态信息并执行操作。

  • API跨应用测试。

要求Android4.3(API等级18)或者更高。

uiautomatorviewer提供了一个方便的图形用户界面进行扫描和分析在Android设备上当前显示的UI组件。您可以使用此工具来检查的布局层次和查看UI组件。

UiDevice 类可以访问设备并进行操作。你可以调用它的方法来访问设备属性,如当前的方向或显示尺寸。该UiDevice类也让您执行操作,例如:旋转设备;按下D- pad按钮;按Back、Home、Menu等;打开通知树栏;当前窗口截图等。比如按Home键:UiDevice.pressHome()。

更 多应用相关的API: UiCollection枚举容器的UI元素以计数,或通过文字(或属性等)定位子元素; UIObject表示是在设备上可见的UI元素; UiScrollable:为可滚动UI容器提供查找支持; UiSelector:查询一个或者多个UI元素; Configurator: 设置参数。比如:

// Initialize UiDevice instance
mDevice = UiDevice.getInstance(getInstrumentation());
// Perform a short press on the HOME button
mDevice().pressHome();
// Bring up the default launcher by searching for
// a UI component that matches the content-description for the launcher button
UiObject allAppsButton = mDevice
        .findObject(new UiSelector().description("Apps"));
// Perform a click on the button to bring up the launcher
allAppsButton.clickAndWaitForNewWindow();

API参考:http://developer.android.com/reference/android/support/test/package-summary.html
实例:http://developer.android.com/training/testing/ui-testing/uiautomator-testing.html

https://pypi.python.org/pypi/uiautomator 用python封装了uiautomator,操作起来更简单,推荐使用。

 

测试支持库安装

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

 

  • 启动Android SDK管理器。

  • 在SDK管理器窗口,滚动到软件包列表的末尾,找到其他文件夹,如有必要,展开以显示其内容。

  • 选择 Android Support Repository。

  • 点击Install packages。

    下载后安装支持库文件到您现有的Android SDK目录。该库文件位于你的SDK的子目录:<sdk>/extras/android/m2repository。

    Android的测试支持库位于android.support.test包。

    Gradle中使用:build.gradle文件中添加这些依赖关系:

    dependencies {
      androidTestCompile ‘com.android.support.test:runner:0.4‘
      // Set this dependency to use JUnit 4 rules
      androidTestCompile ‘com.android.support.test:rules:0.4‘
      // Set this dependency to build and run Espresso tests
      androidTestCompile ‘com.android.support.test.espresso:espresso-core:2.2.1‘
      // Set this dependency to build and run UI Automator tests
      androidTestCompile ‘com.android.support.test.uiautomator:uiautomator-v18:2.1.2‘
    }


    设置AndroidJUnitRunner为默认测试:
    强烈建议您一起使用Android测试支持库与Android Studio IDE。 Android的Studio支持测试开发,如:

        基于Gradle的灵活的构建系统,支持测试代码的依赖管理。
        单个项目包含应用程序源代码和测试代码。
        支持虚拟或物理设备的部署和运行测试,支持命令行或图形用户界面

    更多介绍参见:http://developer.android.com/sdk/index.html

参考资料

 

Android 测试支持库介绍

标签:

原文地址:http://www.cnblogs.com/pythontesting/p/4983072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!