android 测试一二三
Android中的测试
android测试支持库包含三个测试自动化工具:
AndroidJUnitRunner :JUnit 4对android的兼容包,提供JUnit风格的单元测试
Espresso :UI测试框架;适用于App内的功能性UI测试
UI Automator : UI测试框架;适用于跨越系统和已安装app的通过性UI功能测试
AndroidJUnitRunner
类AndroidJUnitRunner可以让你使用JUnit3或者是JUnit4风格在Android设备上进行测试,包括使用Espresso和UI Automator测试框架。这个测试引擎会将你的测试包和被测试包部署到一个设备上,运行测试并报告测试结果。这个类用来替换InstrumentationTestRunner类,后者只支持JUnit 3风格的测试。
包含以下四个特性:
JUnit support
Access to instrumentation information
Test filtering
Test sharding
需要 Android 2.2(API lever 8)或更高
JUnit support
虽然同时支持JUnit3 和 JUnit 4 测试,但是你要避免在同一个包种混合使用他们,因为这个可能会导致不希望的结果。如果你要创建一个JUnit 4测试类,你必须在类的开头加上@RunWith(AndroidJUnit4.class)注解
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();
}
}
Access to instrumentation information
你可以用InstrumentationRegistry 类去访问和你测试相关的信息。这个类包含Instrumentation 对象、目标app的Context对象、测试app的Context对象和用命令行执行传递过来的参数。这些数据信息在你编写需要使用UI Automator测试框架或依赖Instrumentation、Context对象时会非常有用。
Test filtering
Test sharding
在Android Studio 默认支持,一般不用做特殊配置。可以用命令 adb shell pm list instrumentation 查看可以运行的测试
一般的安装的应用没有测试用例,需要运行一下测试用例
用法在AndroidJUnitRunner类的说明中(参照http://developer.android.com/intl/zh-cn/reference/android/support/test/runner/AndroidJUnitRunner.html)
未完待续....
附注:
(参考地址http://developer.android.com/intl/zh-cn/tools/testing-support-library/index.html)

浙公网安备 33010602011771号