你的位置:首页 > 软件开发 > 操作系统 > Android 测试支持库介绍

Android 测试支持库介绍

发布时间:2015-11-21 12:00:28
测试支持库Android的测试支持库为测试Android应用提供了大量框架。该库提供了一组API快速构建和运行测试代码,包括JUnit4和功能用户界面(UI)测试。可以从Android Studio IDE中或命令行这执行。Android的测试支持库可通过Android SDK管 ...

测试支持库

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();  }}
例如分成10个碎片,仅执行第二片测试,请使用以下命令:

adb shell am instrument -w -e numShards 10 -e shardIndex 2
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 keyboardonView(withId(R.id.editTextUserInput))  .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());// Press the button to submit the text changeonView(withId(R.id.changeTextBt)).perform(click());
API参考:developer.android.com/reference/android/support/test/package-summary.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 instancemDevice = UiDevice.getInstance(getInstrumentation());// Perform a short press on the HOME buttonmDevice().pressHome();// Bring up the default launcher by searching for// a UI component that matches the content-description for the launcher buttonUiObject allAppsButton = mDevice    .findObject(new UiSelector().description("Apps"));// Perform a click on the button to bring up the launcherallAppsButton.clickAndWaitForNewWindow();
下载后安装支持库文件到您现有的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'}
    基于Gradle的灵活的构建系统,支持测试代码的依赖管理。更多介绍参见:http://developer.android.com/sdk/index.html

参考资料

  • Testing Support Library

  • http://wiki.jikexueyuan.com/project/android-weekly/issue-145/android-ui-auto-testing.html
  • http://developer.android.com/intl/zh-cn/training/testing/ui-testing/espresso-testing.html
  • http://www.stevenmarkford.com/android-ui-testing-with-espresso-basics-tutorial/
  • http://www.csdn.net/article/2015-08-19/2825493-using-espresso-for-easy-ui-testing

 


 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Android 测试支持库介绍

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录