Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.2——使用Android Testing Support Library进行测试
问题:
你想要测试app的Android组件。
解决方案:
使用新的测试类实现JUnit风格的测试。
讨论:
测试像activities,services等的Android组件,需要将app部署到连接的设备或者模拟器上面。测试类基于JUnit,但是严格意义上不是单元测试。它们是集成测试还是功能测试,取决于你怎么使用。
因为看到这里的目的都是驱使部署的app正常运行并且UI正常改变,功能测试在这比较合适。你可以在文档中经常看到集成测试。
Android Testing Support Library通过SDK Manager作为可选依赖添加:

测试时”Android Support Library“的一部分。测试类在android.support.test包下面。
使用dependencies将所有相关类添加到Gradle配置文件里面:

AndroidJUnitRunner类以及支持JUnit4的注解。你可以在你的测试类上面添加JUnit的@RunWith注解,或者在defaultConfig块下面增加设置:

是用test support类可以很方便的测试layout上面的一个labels。例子如下:
@MediumTest @RunWith(AndroidJUnit4.class)
public class MyActivityLayoutTest
extends ActivityInstrumentationTestCase2<MyActivity> {
private MyActivity activity;
private TextView textView;
private EditText editText;
private Button helloButton;
public MyActivityLayoutTest() { super(MyActivity.class); @Before
public void setUp() throws Exception {
super.setUp()
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
activity = getActivity();
textView = (TextView) activity.findViewById(R.id.text_view);
editText = (EditText) activity.findViewById(R.id.edit_text);
helloButton = (Button) activity.findViewById(R.id.hello_button);
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testPreconditions() {
assertNotNull("Activity is null", activity);
assertNotNull("TextView is null", textView);
assertNotNull("EditText is null", editText);
assertNotNull("HelloButton is null", helloButton);
}
@Test
public void textView_label() {
final String expected = activity.getString(R.string.hello_world);
final String actual = textView.getText().toString();
assertEquals(expected, actual);
}
@Test
public void editText_hint() {
final String expected = activity.getString(R.string.name_hint);
final String actual = editText.getHint().toString();
assertEquals(expected, actual);
}
@Test
public void helloButton_label() {
final String expected = activity.getString(R.string.hello_button_label);
final String actual = helloButton.getText().toString();
assertEquals(expected, actual);
} }
新的AndroidJUnitRunner是Android Support Test Library的一部分。它增加了对JUnit4的支持,所以可以注解替代老的JUnit3的命名惯例。它有其他额外的能力。详细信息可以查看https://google.github.io/android-testing-support-library/。
属性代表用户界面上的组件。@Before方法查找组件,并将它们分配给属性。文档推荐使用testPreconditions测试,演示组件被找到。测试和其它没有什么区别,只是失败后会很容易找到哪里出错了。
其它测试都从string资源里面查找字符串,和labels里面的值进行比较。注意任何东西都没有被修改,测试大体上是只读的。
最后,@MediumTest注解被用来指示测试方法的大小。只要很短时间的测试用@SmallTest标记,耗时超过100毫秒的是@MediumTest,更长的使用@LargeTest。
通过Gradle,运行那些需要连接设备或者模拟器的测试可以通过connectedCheck任务完成。
例子如下:
> ./gradlew connectedCheck
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:prepareDebugDependencies
// ... lots of tasks ...
:app:packageDebugAndroidTest UP-TO-DATE
:app:assembleDebugAndroidTest UP-TO-DATE
:app:connectedDebugAndroidTest
:app:connectedAndroidTest
:app:connectedCheck
BUILD SUCCESSFUL
输出的报告在app/build/reports/androidTests/connected目录下面。实例报告如下:

实例显示了模拟器的名字,和所有测试的结果。点击”Devices“按钮来切换视图:

Android Support Test Library里面的类可以做更多,并且非常的块。当你想要通过增加数据,点击按钮来驱动UI,有像Robotium和Espresso这样的可以库。
Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.2——使用Android Testing Support Library进行测试的更多相关文章
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)4.5——使用Android Libraries
问题: 你想要在app当中增加新的library模块 解决方案: 使用library插件,增加一个library模块作为依赖. 讨论: 不可以通过使用java库给app增加许多功能,通常是使用jar包 ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.7——使用Android Studio签署发布apk
问题: 想要使用Android studio生成签名配置,给他们分配build类型. 解决方案: Build菜单提供了生成签名配置,Project Structure窗口有tab用于分配不同的type ...
- [Android]官网《Testing Support Library》中文翻译
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5048524.html 翻译自 Android Develope ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)6.2——DSL文档
问题: 你需要查找Android Gradle DSL的完整文档. 解决方案: 访问Gradle Tools网站,从Android开发网站下载ZIP文件. 讨论:Android开发网站首页有完整的AP ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.1——单元测试
问题: 你想要测试app中的非android部分. 解决方案: 可以使用Android Studio1.1里面增加的单元测支持和Android的Gradle插件. 讨论: ADT插件只支持集成测试,并 ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)4.1——编写自己的任务
问题: 你想用自己的任务定制gradle的构建过程. 解决方案: 在gradle的build文件里面增加task元素.用Android插件支持的extra属性使得开发更容易. 讨论: Gradle的D ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.3——用Eclipse ADT导出App
问题: 想在一个已经存在的Eclipse ADT的项目中使用Gradle 解决方案: Eclipse ADT插件可以帮助生成Gradle文件 讨论: Eclipse的ADT插件是在2013年推出Gra ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.4——使用Espresso测试Activity
问题: 你想要使用Google的Espresso测试Activity. 解决方案: 在Gradle配置里面增加Espresso,书写测试脚本. 讨论: Espresso测试库已经被添加进“Androi ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.3——使用Robotium进行功能测试
问题: 你想要使用Robotium库测试activity. 解决方案: 增加Robotium依赖,编写自己的测试脚本. 讨论: Android Test Support Library提供类可以操作a ...
随机推荐
- 【DB2】If 'db2' is not a typo you can run the following command to lookup the package that contains the binary: command-not-found db2 bash: db2: command not found
数据库安装以后,db2报错如下: If 'db2' is not a typo you can run the following command to lookup the package that ...
- Spring Boot(二)Application events and listeners
一.自定义监听器: 1.创建: META-INF/spring.factories 2.添加: org.springframework.context.ApplicationListener=com. ...
- PHP中根据IP地址判断所在城市等信息
本篇文章由:http://xinpure.com/php-based-on-information-such-as-the-ip-address-in-your-city/ 获得IP地址 在 PHP ...
- Solr4.0使用
http://blog.sina.com.cn/s/blog_64dab14801013k7g.html Solr简介 Solr是一个非常流行的,高性能的开源企业级搜索引擎平台,属于Apache Lu ...
- thinkphp 编辑器kindeditor
首先,去官网下载最新版的kindeditor,然后把里面asp,jsp,net,example的全删除,然后改名为editor放进public(最外层目录的public)文件夹里面 在目录lib目录建 ...
- NFS介绍
一.NFS服务介绍 NFS是 Network File system的缩写 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机 ...
- cocos2dx 3.x Node::schedule
auto callback = [=](float dt){ //do something }; node->schedule(callback, 1.0/60, "mySchedul ...
- 设计模式_Observable与Observer
一.基本概念 java.util.Observable 被观察者类,需要继承这个类 java.util.Observer 观察者类,需要实现这个接口中的update()方法 二.举例 Door ...
- linux命令之高级使用 du
du命令:disk usage,顾名思义,是关于目录使用情况的,它的作用就是计算目录大小的. 1. 想看当前目录下所有目录以及子目录的大小: # du -h . “.”代表当前目录下.也可以换成一个明 ...
- (译)Getting Started——1.2.4 Tutorial:Storyboard(故事板)
该教程是基于你在前面的课程中构建的项目上进行的.学完本教程后,你将使用你前面学到的视图.视图控制器.动作.导航的内容,还会为应用创建一些关键的用户界面,并在场景中添加行为 以下就是本节课的内容: 1. ...