AndroidStudio单元测试——instrumentation
来源:互联网 发布:淘宝半自动机器人 编辑:程序博客网 时间:2024/05/22 03:15
前言:这几天老大要我搞代码自动测试,eclispe的已经解决了,可他们都是用android studio,所以要在android studio 上重新试验,这个有难度啊,android studio国内资料极少,更不要说单元测试了。google了一两天才最终解决了这个问题,写个Demo分享给大家。
一、写在前面
1、我的android studio版本
2、注意事项
当你google的时候,你会发现铺天盖地的资料让你设置build.gradle,以寻求android studio对测试的支持,其实这些是完全没必要的!!!!android studio默认对测试是支持的,所以不用在build.gradle中添加任何的设置语句。
二、代码
1、新建test文件夹
在要测试工程目录的兄弟目录,新建一个test文件夹,如图:
2、测试代码
我在这个test文件夹中添加了一个类EexampleTest,该类派生自InstrumentationTestCase,并写了一个方法,完整代码如下:
- public class ExampleTest extends InstrumentationTestCase {
- public void test() throws Exception {
- final int expected = 1;
- final int reality = 1;
- assertEquals(expected, reality);
- }
- }
三、测试
在android studio一排按钮的最上层,点击“运行”旁边的配置按钮
打开配置对话框,选择Android Test
右侧Module选择你的工程名,这里是app
Test: 部分选择All in Package
Package:选择当前新建的test文件夹
整体配置好之后是这样的
运行出结果:
测试通过的结果是这样子的:
如果我们把test()的代码改成这样子:
- public void test() throws Exception {
- final int expected = 1;
- final int reality = 3;
- assertEquals(expected, reality);
- }
所以出错的结果是这样子的:(在中间窗口logcat中可以定位到出错位置)
OK,That's all……
参考文章:《Unit Test With Android Studio》:http://rexstjohn.com/unit-testing-with-android-studio/ (访问可能要翻墙)
源码地址:http://download.csdn.net/detail/harvic880925/7667341
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/38060361 万分感激!
- AndroidStudio单元测试——instrumentation
- AndroidStudio单元测试——instrumentation
- AndroidStudio单元测试——instrumentation
- AndroidStudio单元测试——instrumentation
- AndroidStudio单元测试——instrumentation
- AndroidStudio单元测试——instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- Android单元测试初探——Instrumentation
- instrumentation单元测试
- android基础知识12:android自动化测试06—Instrumentation 02 单元测试
- Android单元测试 Instrumentation
- Android单元测试 Instrumentation
- swirl 3: Sequences of Numbers
- 我是菜鸟:MapReduce编程模型
- 使用GCD
- iOS学习(其他小控件的使用)
- Codeforces B - Berland National Library
- AndroidStudio单元测试——instrumentation
- 反转单向链表
- 在一个文件中有10G个整数,乱序排列,要求找出中位数
- Linux实际内存使用计算
- Activity的启动模式与flag详解
- [Javascript] Array.prototype.slice.call(obj)用法
- int 的灵活使用可以当作char short和3字节使用(这里不考虑占用内存)
- java获得验证码图片
- 获取离当前时间最近的一条数据