Android测试开发相关知识
来源:互联网 发布:java编程入门视频 编辑:程序博客网 时间:2024/05/23 02:01
# Android测试开发相关知识
#### 一、概要
###### 本文介绍以下三个框架:
[Google 官方文档](https://developer.android.com/topic/libraries/testing-support-library/index.html)
[Google 官方教程](https://developer.android.com/training/testing/fundamentals.html)
* AndroidJUnitRunner
* Espresso
* UIAutomator
#### 二、AndroidJUnitRunner:一个Junit在Android平台测试运行器,组织测试结构
##### 1、引用方式:
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:' + runnerVersion
androidTestCompile 'com.android.support.test:rules:' + rulesVersion
##### 2、重要的类:
* `AndroidJUnitRunner`:测试的入口.继承于MonitoringInstrumentation,可自己实现subclass做一些初始化的操作;
* `MonitoringInstrumentation`:监控Application的各种状态,继承于ExposedInstrumentationApi;
* `ExposedInstrumentationApi`:父类`Instrumentation`,时间上只是对android系统 hide类`Instrumentation`方法公开化,由此可见`AndroidJunitRunner`是一个加强化的`Instrumentation`;
##### 3、重要的注解:
* `@RunWith`:类注解,`AndroidJUnit4.class`或者`AndroidJUnit3.class`,运行 JUnit 3或 JUnit 4 样式测试类,只能选定一种;
* `@Rule` :方法注解,必须的属性注解,表明测试目标;
* `@Before` :方法注解,测试执行之前的准备,比如需要登录系统,可以将登录操作在注解的方法中;
* `@Test` :方法注解,书写测试用例;
* `@After` :方法注解,测试执行结束之后调用,释放资源;
##### 4、Demo:
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import com.xxx.android.test.MainActivity;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* author xxx
* time 2017/9/12:下午5:28
* email xxx
*/
@RunWith(AndroidJUnit4.class)
public class TestMainActivity {
@Rule
public ActivityTestRule mRule = new ActivityTestRule(MainActivity.class);
@Before
public void doBefore() throws Exception {
}
@Test
public void testCase() {
}
@After
public void end() throws Exception {
mRule.getActivity().finish();
}
}
#### 三、 Espresso:适合应用中的功能性 UI 测试
##### 1、引用方式:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
espressoVersion = 3.0.1
androidTestCompile 'com.android.support.test.espresso:espresso-core:' + espressoVersion
androidTestCompile 'com.android.support.test.espresso:espresso-web:' + espressoVersion
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:' + espressoVersion
androidTestCompile 'com.android.support.test.espresso:espresso-intents:' + espressoVersion
androidTestCompile 'com.android.support.test.espresso:espresso-accessibility:' + espressoVersion
androidTestCompile 'com.android.support.test.espresso.idling:idling-concurrent:' + espressoVersion
##### 2、使用说明
* `Matcher`:匹配器,组织匹配原则(`ViewMatchers`,`Matchers`中已定义大量通用的Matcher,也可以自定义;
* `DataInteraction`、`ViewInteraction`:分别通过`Espresso.onData(matcher)`、`Espresso.onView(matcher)`获取,得到需要进行测试的视图或组件;
* `ViewAction`:组织特定的自动化操作(`ViewActions`中已定义大量通用的`ViewAction`,也可以自定义);
* `dataInteraction.perform(action)`或 `viewInteraction.perform(action)`自动化UI交互;
* `ViewAssertion`:组织测试行为和断言;
* `viewInteraction.check(viewAssertion)`或`dataInteraction.check(viewAssertion)`执行某项测试;
#### 四、 UI Automator适合跨系统和已安装应用的跨应用功能性 UI 测试
##### 1、引用方式:
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
##### 2.UiDevice:很多有意思的方法,对设备进行操作
* 具体可见[Google官方文档](https://developer.android.com/reference/android/support/test/uiautomator/UiDevice.html)
##### 3.关键类:
* `UiCollection`:枚举容器的 UI元素以便计算子元素个数,或者通过可见的文本或内容描述属性来指代子元素。[API](https://developer.android.com/reference/android/support/test/uiautomator/UiCollection.html)
* `UiObject`:表示设备上可见的 UI元素。[API](https://developer.android.com/reference/android/support/test/uiautomator/UiObject.html)
* `UiScrollable`:为在可滚动 UI容器中搜索项目提供支持。[API](https://developer.android.com/reference/android/support/test/uiautomator/UiScrollable.html)
* `UiSelector`:表示在设备上查询一个或多个目标 UI元素。[API](https://developer.android.com/reference/android/support/test/uiautomator/UiSelector.html)
* `Configurator`:允许您设置运行 UI Automator测试所需的关键参数。[API](https://developer.android.com/reference/android/support/test/uiautomator/Configurator.html)
##### 4. 使用说明:
* UiObject:
UiObject okButton = mDevice.findObject(new UiSelector()
.text("OK"))
.className("android.widget.Button"));
// Simulate a user-click on the OK button, if found.
if(okButton.exists() && okButton.isEnabled()) {
okButton.click();
}
* UiSelector :
UiObject appItem = new UiObject(new UiSelector()
.className("android.widget.ListView")
.instance(0)
.childSelector(new UiSelector()
.text("Apps")));
* Assert 断言,使用Junit的断言验证结果:
private static final String CALC_PACKAGE = "com.myexample.calc";
public void testTwoPlusThreeEqualsFive() {
// Enter an equation: 2 + 3 = ?
mDevice.findObject(new UiSelector()
.packageName(CALC_PACKAGE).resourceId("two")).click();
mDevice.findObject(new UiSelector()
.packageName(CALC_PACKAGE).resourceId("plus")).click();
mDevice.findObject(new UiSelector()
.packageName(CALC_PACKAGE).resourceId("three")).click();
mDevice.findObject(new UiSelector()
.packageName(CALC_PACKAGE).resourceId("equals")).click();
// Verify the result = 5
UiObject result = mDevice.findObject(By.res(CALC_PACKAGE, "result"));
assertEquals("5", result.getText());
}
##### 5.Demo
import org.junit.Before;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Until;
...
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ChangeTextBehaviorTest {
private static final String BASIC_SAMPLE_PACKAGE
= "com.example.android.testing.uiautomator.BasicSample";
private static final int LAUNCH_TIMEOUT = 5000;
private static final String STRING_TO_BE_TYPED = "UiAutomator";
private UiDevice mDevice;
@Before
public void startMainActivityFromHomeScreen() {
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// Start from the home screen
mDevice.pressHome();
// Wait for launcher
final String launcherPackage = mDevice.getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT);
// Launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
// Clear out any previous instances
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
// Wait for the app to appear
mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
LAUNCH_TIMEOUT);
}
}
#### 五、综述
最好配合使用以上三个框架进行Andrroid自动化测试。
AndroidJUnitRunner用于搭建测试结构。
Espresso和UIAutomator部分功能重叠,查找视图和执行自动化操作以及进行断言都可实现。
Espresso对于某组件的定位和perform操作更加精准,并且实现了UI线程同步,消除了对之前的计时解决方法的需求,确保测试操作与断言更可靠地运行,所以用来写测试用例。
UIAutomator则更方便的在不同应用之间或者应用与系统之间进行交互时使用。
- Android测试开发相关知识
- Robotium 测试工具,android 开发相关知识积累
- Android开发相关概念知识
- Android开发像素相关知识
- Android界面开发的相关知识
- Android界面开发的相关知识
- Android界面开发的相关知识
- android NDK开发库相关的知识
- Android 蓝牙开发相关知识总结
- Android 开发之 RecyclerView相关知识
- Android NDK开发相关知识集合
- Android开发短信相关的知识
- 测试相关知识总结
- 阿里测试开发笔试题--字符串相关知识
- Android开发新工具Android Studio相关知识
- Android开发新工具Android Studio相关知识
- Web开发相关知识
- 计算器开发相关知识
- java练习
- windows线程与内核对象的同步
- Adapter条目点击,跳转到新的Activity,获取到值之后回显到adapter 条目中的某个控件中去
- 关于在RecyclerView中使用UniversalImageLoader加载图片刷新数据时引起的闪烁问题
- 产品上市之路
- Android测试开发相关知识
- 常用排序算法归纳
- LeetCode-118:Pascal's Triangle (生成指定行数的杨辉三角)
- 抽象类和接口的区别
- 关于NoSQL
- tomcat调优
- Android 图片选择 图册中图片选择
- 【bzoj4872】[Shoi2017]分手是祝愿 数论+期望dp
- MySQL中关于JDBC