`
jerome_wang
  • 浏览: 147479 次
  • 性别: Icon_minigender_1
  • 来自: 云南
社区版块
存档分类
最新评论

AndroidStudio单元测试——instrumentation

 
阅读更多

转自 

http://blog.csdn.net/harvic880925/article/details/38060361

亲测成功 

 

AndroidStudio单元测试——instrumentation

分类: 5、andriod开发 7739人阅读 评论(11) 收藏 举报
 

目录(?)[+]

 

前言:这几天老大要我搞代码自动测试,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,并写了一个方法,完整代码如下:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public class ExampleTest extends InstrumentationTestCase {  
  2.   
  3.         public void test() throws Exception {  
  4.             final int expected = 1;  
  5.             final int reality = 1;  
  6.             assertEquals(expected, reality);  
  7.         }  
  8. }  
注意:在android studio中,所有的测试代码必须以test-作为前缀,不然android studio无法识别它为测试代码。

 

三、测试

在android studio一排按钮的最上层,点击“运行”旁边的配置按钮

打开配置对话框,选择Android Test

右侧Module选择你的工程名,这里是app

Test: 部分选择All in Package

Package:选择当前新建的test文件夹

整体配置好之后是这样的

运行出结果:

测试通过的结果是这样子的:

如果我们把test()的代码改成这样子:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public void test() throws Exception {  
  2.     final int expected = 1;  
  3.     final int reality = 3;  
  4.     assertEquals(expected, reality);  
  5. }  
明显1不等于3,所以assertEquals肯定会出错!
所以出错的结果是这样子的:(在中间窗口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》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/38060361

    Android单元测试初探——Instrumentation

    学习Android有一段时间了,虽然前段时间对软件测试有了一些了解,不过接触android的单元测试却是头一次。这几天在物流大赛上也用了不少时间,所以对于android的单元测试没有太深入的研究,所以先写个基本入门吧!...

    单元测试instrumentation入门---源码

    博客《单元测试instrumentation入门》对应的源码,博客地址:http://blog.csdn.net/harvic880925/article/details/37924189

    详解Android单元测试最佳实践

    在Android原生应用开发中,存在两种单元测试:本地JVM测试和Instrumentation测试。本文仅介绍本地JVM测试 本地jvm的单元测试 这种方式运行速度快,对运行环境没有特殊要求,可以很方便的做自动化测试,是单元测试...

    Android UiAutomator 自动化测试

    Instrumentation是早期Google提供的Android自动化测试工具类 UiAutomator也是Android提供的自动化测试框架,基本上支持所有的Android事件操作 Espresso,Android Studio工程,以apk的形式执行测试 UiAutomator2,...

    android-support-multidex-instrumentation.jar.zip

    android-support-multidex-instrumentation.jar.zip

    Android中Hook Instrumentation 的实现

    参考博客 【Android中Hook Instrumentation 的实现】 http://blog.csdn.net/u012341052/article/details/71191409

    如何对Android系统手机进行单元测试

    中加入:外面加入:MISSIONandroid:name="android.permission.RUN_INSTRUMENTATION"/>android:label="Testformyapp"/编写单元测试代码:必须继承自AndroidTe如何对Android系统手机进行单元测试 如何进行Android单元...

    android-support-multidex-instrumentation.jar

    android-support-multidex-instrumentation.jar android-support-multidex-instrumentation.jar

    Android系统单元测试方法

    android源代码中每个app下中都自带了一个test用例,下面主要介绍下camra单元测试用例 在AndroidManifest.xml中标明了测试用例instrumentation函数入口 Java代码  <?xml ...

    Android自动化测试

    Android自动化测试,该jar包支持robotium测试,当然支持所有的继承Instrumentation类型的测试

    instrumentation

    Instrumentation可以把测试包和目标测试应用加载到同一个进程中运行。既然各个控件和测试代码都运行在同一个进程中了,测试代码当然就可以调用这些控件的方法了,同时修改和验证这些控件的一些数据

    Iocomp.Instrumentation.WF40

    Iocomp.Instrumentation.WF40.Common.dll,Iocomp.Instrumentation.WF40.Pro Iocomp.WF40.OPC

    instrumentation_demo.7z

    使用instrumentation创建测试用例示例代码

    Java Instrumentation笔记

    Java Instrumentation笔记 Java SE 6新特性:Instrumentation,利用 Java 代码,即 java.lang.instrument 做动态 Instrumentation 是 Java SE 5 的新特性,它把 Java 的 instrument 功能从本地代码中解放出来,使之...

    单元测试记录器:在使用应用程序时记录单元测试

    单元测试记录器 使用应用程序时记录单元测试。 TL; DR:类固醇的笑话快照 安装 npm i -g unit-test-recorder 用法 程序 确保您使用的是git,并且没有未提交的更改。 运行unit-test-recorder <your>.js 。 通常是...

    android自动化测试框架robotium的jar包、doc文档及source code

    著名的android自动化测试框架robotium出了4.1版本 在原有基础上增加了对webview的支持 方便好用,但是需要测试者有一定java开发基础 对android instrumentation有一定了解

    Android手机测试的自动化测试工具

    Android自动化测试相对来说还是比较难,Instrumentation比较难以使用。下面和大家分享一个Android自动化测试工具Robotium。Robotium是一款测试AndroidApp的测试框架,它使得编写黑盒测试代码更加容易和稳定。通过...

    Fundamentals of Instrumentation and Measurement .pdf

    Product Description This title presents the general principles of instrumentation processes. It explains the theoretical analysis of physical phenomena used by standard sensors and transducers to ...

Global site tag (gtag.js) - Google Analytics