一、Android JUnit Runner介绍

1.Android JUnit Runner

1)是一个测试运行器,用于运行Junit3和Junit4的Android测试包
2)替换Instrumentation Test Runner(一个比较旧的测试运行器)
3)支持Instrumentation Test Runner所有特性,但又进行了扩展
4)保持了所有Instrumentation Test Runner的命令格式

2.继承关系
  1. java.lang.Object
  2.  
  3. android.app.Instrumentation
  4.  
  5. android.support.test.runner.MonitoringInstrumentation
  6.  
  7. android.support.test.runner.AndroidJUnitRunner

二、Instrument测试命令

1.命令格式:
  1. am instrument [-r] [-e <NAME> <VALUE>] [-p <FILE>] [-w]
  2. [--user <USER_ID>|CURRENT]
  3. [--no-window-animation] [--abi <ABI>] <COMPONENT>
  4.  
  5. //COMPONENT:包名/运行Runner
  • 运行所有测试用例命令的例子:
  1. adb shell am instrument
  2. -w com.testDemo.test/android.support.test.runner.AndroidJUnitRunner
2.测试命令

参数

说明

-r

打印原始结果(如果不加测试结果信息会很少)
使用[-e perf true]生成原始输出性能度量

-e

设置参数

为测试运行器常见的形式是:

[-e [,…]]

将profiling数据写入到文件中

注:需要先建立一个trace文件

-w

等待测试完成(这是一个必需参数)

–user |

current

指定用户运行,如果不指定则使用当前用户运行
–no-window-animation 关掉窗口动画(相当于开场动画)再运行

–abi

通过所选的ABI启动进程,选择支持的CPU架构

可以使用(adb shell getprop
3.实例(使用Android Studio):

1)在github中搜索android-testing选中googlesamples/android-testing点进去,然后下载.zip压缩包

2)下载完成后进行解压,所有的依赖都在android-testing-master\ui\espresso\BasicSampleBundled\libs路径下

3)file–new–new project(等待工程创建完成)–app–java–[packageName右键]–new–java class

注:点开java后会有两个选项,上面的一个是apk代码,下面是测试代码,我们写测试用例一般都写在测试代码包内

4)复制android-testing-master\ui\espresso\BasicSampleBundled\libs目录下除android-support-v4.jar以外的全部文件到创建的工程的libs文件夹中

5)选中copy进去的所有文件右键—add as library—在弹出的提示窗口中选择app—OK

6)增加完成后可以切换到Android视图,在Gradle Scripts/build.gradle(Module:app)中进行查看,并且注释掉第一行*(注释的那一行表示它会把libs下面所有的jar包编译进去,因为我们已经手动把它添加进去了所以就不需要了),代码显示如下:

  1. dependencies {
  2. //compile fileTree(include: ['*.jar'], dir: 'libs')
  3. testCompile 'junit:junit:4.12'
  4. compile 'com.android.support:appcompat-v7:23.1.1'
  5. compile 'com.android.support:design:23.1.1'
  6. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-contrib-2.2-release-no-dep.jar')
  7. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-core-2.2-release-no-dep.jar')
  8. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-idling-resource-2.2-release-no-dep.jar')
  9. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-intents-2.2-release-no-dep.jar')
  10. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-web-2.2-release-no-dep.jar')
  11. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/exposed-instrumentation-api-publish-0.3-release-no-dep.jar')
  12. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/guava-18.0.jar')
  13. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/hamcrest-core-1.3.jar')
  14. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/hamcrest-integration-1.3.jar')
  15. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/hamcrest-library-1.3.jar')
  16. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/javawriter-2.1.1.jar')
  17. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/javax.annotation-api-1.2.jar')
  18. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/javax.inject-1.jar')
  19. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/jsr305-2.0.1.jar')
  20. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/junit-4.12.jar')
  21. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/rules-0.3-release-no-dep.jar')
  22. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/runner-0.3-release-no-dep.jar')
  23. compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/uiautomator-v18-2.1.1-release-no-dep.jar')
  24. }

5)点击导航栏Run—Edit Configurations…—点击弹出界面左上角的+号—Android Tests,配置参考下图

  • 截图说明:

    a.name可以自行设置

    b.Target选项请根据实际情况设置,我这边是拿模拟器进行调试的

6)在Gradle Scripts/build.gradle(Module:app)文件的defalutConfig中增加调试器,增加后的代码显示如下:

  1. android {
  2.  
  3. compileSdkVersion 23
  4.  
  5. buildToolsVersion "23.0.2"
  6.  
  7. defaultConfig {
  8.  
  9. applicationId "com.example.xuzhonghui.runnerdemo"
  10.  
  11. minSdkVersion 15
  12.  
  13. targetSdkVersion 23
  14.  
  15. versionCode 1 versionName "1.0"
  16.  
  17. //引号中的内容为刚刚创建的运行器
  18.  
  19. testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
  20.  
  21. }
  22.  
  23. buildTypes {
  24.  
  25. release {
  26.  
  27. minifyEnabled false proguardFiles
  28.  
  29. getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  30.  
  31. }
  32.  
  33. }
  34.  
  35. }

7)至此,基本的配置完成了,然后在刚刚创建的类中写测试代码了,代码如下:

  1. package com.example.xuzhonghui.runnerdemo;
  2.  
  3. import android.app.Instrumentation;
  4. import android.support.test.InstrumentationRegistry;
  5. import android.support.test.runner.AndroidJUnit4;
  6. import android.support.test.uiautomator.UiDevice;
  7.  
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11.  
  12. //下面一行一定要的,就是指定哪个运行器运行
  13. @RunWith(AndroidJUnit4.class)
  14. //测试用例
  15. public class RunnerTest {
  16. //初始化设备
  17. UiDevice device;
  18.  
  19. @Before
  20. public void setip{
  21. device=UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
  22. }
  23. //用例1
  24. @Test
  25. public void test1(){
  26. device.pressMenu();
  27. }
  28. //用例2
  29. @Test
  30. public void test2(){
  31. device.pressBack();
  32. }
  33. //用例3
  34. @Test
  35. public void test3(){
  36. device.pressMenu();
  37. }
  38. //用例4
  39. @Test
  40. public void test4(){
  41. device.pressBack();
  42. }
  43. }

8)代码写好后需要重新配置一下运行器,配置图如下:

  • 配置说明:

    a.在Test中选中测试类,然后将刚刚写好测试代码的那个类添加进去,也就是标红的位置了

    b.show device chooser Dialog表示弹出一个选择目标设备的对话框

9)然后运行即可

  • 注意事项:

我第一次按照上述步骤运行的时候就出现了问题,具体原理暂时还没搞清楚,以后明白了补上,问题和解决方案请参考这里:http://blog.csdn.net/qq_26967883/article/details/50477071的第一条:Excution faild for task ‘:app:transformResourcesWithMergeJavaResForDebug’

4.Instrument命令简单使用
  1. #列出手机中所有已安装的包,界面中可以看到刚刚使用Android Studio安装的包
  2. adb shell pm list instrumentation
  3.  
  4. #该命令中不打印详细日志,-e后面的参数是刚刚Android Studio中创建用例的<packageName>.<className>
  5. adb shell am instrument -w -e class com.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  6.  
  7. #跟上一条命令相比,只是多了一个<-r>参数,是让他输出详细的运行日志,相当于eclipes调试uiautomator的时候输出的日志
  8. adb shell am instrument -w -r -e class com.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  9.  
  10. #跟上一条相比多了一个-p+file的参数,目的是为了将运行日志保存到.trace文件中。使用该命令要先在adb shell 模式下在设备中使用命令"touch runner.trace"创建一个自定义名称的.trace文件
  11. adb shell am instrument -w -r -p /data/local/tmp/runner.trace -e class com.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner

三、Android JUnit Runner过滤与例子

1.过滤器

过滤参数

说明

-e testFile 运行文件中指定的用例
-e package 运行这个包中的所有用例
-e size [small|medium|large] 运行注释Small/MediumTest/largeTest的用例
-e annotation 运行指定注释的用例
-e notAnnotation 运行不包含指定注释的用例
-e numShards 将用例分割成不同的切片
-e shardIndex 运行指定切片id的用例
2.例子

3.实例

1)首先修改一下上面使用Android Studio创建的用例代码,多加几个测试方法,我这里加了10个,都是按键信息,加完后记得先跑一次,把包push到手机中

  1. #列出手机中所有已安装的包找到com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner就是我们使用Android创建并安装到手机中的包
  2. adb shell pm list instrumentation
  3.  
  4. #运行全部用例,没有日志信息
  5. C:\Users\xuzhonghui>adb shell am instrument -w com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  6.  
  7. #运行全部用例,有日志信息
  8. C:\Users\xuzhonghui>adb shell am instrument -w -r com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  9.  
  10. #运行指定类的用例(指定类=包名.类名)
  11. C:\Users\xuzhonghui>adb shell am instrument -w -r -r class com.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  12.  
  13. #运行指定类中的某一条用例(在指定类后面加一个"#用例名")
  14. C:\Users\xuzhonghui>adb shell am instrument -w -r -r class com.example.xuzhonghui.runnerdemo.RunnerTest#test3 com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  15.  
  16. #运行多个类的用例,运行前先在之前先在Android Studio中多创建一个类并复制几条用例进去方便(这里只演示两个类,多个类同理,加个逗号即可)
  17. C:\Users\xuzhonghui>adb shell am instrument -w -r -r class com.example.xuzhonghui.runnerdemo.RunnerTest,com.example.xuzhonghui.runnerdemo.RunnerTest2 com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner

创建一个.txt文档,文档内容如下图,然后使用命令”adb push F:\testFile.txt /data/local/tmp”将文档push到手机中

  1. #运行刚刚.txt文件中指定的用例
  2. adb shell am instrument -w -r -e testFile /data/local/tmp/testFile.txt com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  3.  
  4. #运行com.example.xuzhonghui.runnerdemo包里的所有用例
  5. adb shell am instrument -w -r -e package com.example.xuzhonghui.runnerdemo com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner

修改一下之前在Android Studio中创建的用例,增加几个注释,结果如下图(记得修改完后先在手机上运行一下):

  1. #运行刚刚注释为small的用例
  2. adb shell am instrument -w -r -e size small com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  3.  
  4. #运行刚刚注释为medium的用例
  5. adb shell am instrument -w -r -e size medium com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  6.  
  7. #运行刚刚注释为large的用例
  8. adb shell am instrument -w -r -e size large com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner

修改AndroidStudio中的用例,上一步中的smallText注释的Import找到注释方法的编写,然后根据这个自己来编写注释方法,编写好的方法如下图:

编写好后,按照上一步的方式将注释加入到用例中,修改好用例后记得先运行一次

  1. #运行指定自定义注释SmokeAnnotation的用例(这里需要注意的是annotation后面的跟的是自己编写的注释的<包名>.<类名>)
  2. adb shell am instrument -w -r -e annotation com.example.xuzhonghui.runnerdemo.SmokeAnnotation com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
  3.  
  4. #运行除指定自定义注释SmokeAnnotation的所有其他用例
  5. adb shell am instrument -w -r -e notAnnotation com.example.xuzhonghui.runnerdemo.SmokeAnnotation com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner

四、Android JUnit Runner参数

主要是用在白盒测试上的,而不是UiAutomator,有兴趣可以深入研究下

参数 说明
-e log true 只在日志模式下运行

这个选项将加载和遍历所有测试的类和方法,但会绕过实际测试执行。用于快速获取信息要执行的测试的仪器命令
-e coverage true 生成代码覆盖率

这需要一个EMMA的构建。默认情况下,代码覆盖率结果文件将被保存在/data//coverage.ec文件,除非有coverageFile标志
-e coverageFile 指定代码覆盖率文件保存路径

-e coverageFile /sdcard/myFile.ec
-e listener 指定一个或多个RunListeners观察测试运行

-e listener com.foo.Listener,com.foo.Listener2
-e timeout_mse 设置超时时间(单位:毫秒)将被应用到每一个测试

-e timeout_mec 5000
-e disableAnlytics true 禁用谷歌分析
  • 参数还可以在AndroidMainfest文件中进行指定

如何使用监听器:

实际使用过程中,只需在AndroidManifect清单文件中加入上面代码即可,实际使用参考下图

  • 参数通过shell命令传入将覆盖清单指定参数

1.Android JUnit Runner(使用AndroidStudio)的更多相关文章

  1. Android Junit测试框架

    对应用进行单元测试: 使用Junit测试框架,是正规Android开发的必用技术.在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性. 1.配置指令集和函数库: (1)配置指令集,指定 ...

  2. 在Android Studio进行“简单配置”单元测试(Android Junit)

    起因 在Android studio 刚出.本人就想弄单元测试,可惜当时Android studio不知道抽什么风(准确来说,应该是我不会弄而已).无法执行到相应的代码.后来今天突然自己又抽风.又想去 ...

  3. Android JUnit Test——批量运行测试代码

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ Android测试三要素 写Android测试用例有三要素,一是我们用的“安卓模拟器device” ...

  4. Android JUnit test

    Android单元测试步骤 1.修改AndroidManifest.xml文件. 添加instrumentation节点.其中name是固定值,targetPackage为需要测试的类所在的包.如:  ...

  5. Android -- junit测试框架,logcat获取log信息

    1. 相关概念 白盒测试: 知道程序源代码. 根据测试的粒度分为不同的类型   方法测试 function test         单元测试 unit test                 集成 ...

  6. JUnit org.junit.runner.Request.classWithoutSuiteMethod解决方法

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  7. Android+Junit单元测试1

    学习参考: http://my.oschina.net/liux/blog/52469 http://mobile.51cto.com/android-229614.htm 一,权限配置 <ap ...

  8. JUnit4.8.2来源分析-2 org.junit.runner.Request

    JUnit4.8.2源代码,最为yqj2065兴趣是org.junit.runner.Request,现在是几点意味着它? ①封装JUnit的输入 JUnit4作为信息处理单元,它的输入是单元測试类- ...

  9. java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter

    今天想写个随笔,最近经常遇到使用junit的时候报java.lang.NoClassDefFoundError,今天算是恍然大悟了,原来junit虽然在gradle里面配置了,也在Project an ...

随机推荐

  1. mysql5.5 升级到 5.7 的坑

    1.大概思路,docker 新启一个mysql5.7 端口映射到3307 2. 导出5.5 的.sql文件,导入5.7中 3.测试通过后,可将5.5关闭.5.7端口改回3306 GRANT ALL P ...

  2. oozie的shell-action中加入hive脚本命令启动执行shell同时操作hive,抛异常Container killed on request. Exit code is 143 Container exited with a non-zero exit code 143

    使用oozie来调度操作,用shell的action执行命令,其中shell里包含着hive -e 操作执行时,oozie窗口报 WARN ShellActionExecutor: - SERVER[ ...

  3. Linux学习——操作文件与目录

    1. ls:列出文件及目录信息. 命令格式:ls [选项] ... 常用选项: -a 显示指定目录下所有子目录与文件,包括隐藏文件. -A 显示指定目录下所有子目录与文件,包括隐藏文件.但不列出“.” ...

  4. DP---(POJ1159 POJ1458 POJ1141)

    POJ1159,动态规划经典题目,很适合初学者入门练手. 求:为了使字符串左右对称,应该插入的最小字符数目. 设字符串为S1 S2 S3 - Sn. 这个字符串有n个字符,根据DP的基本思路,减少问题 ...

  5. CDOJ ABCDE dp(前缀和优化)

    题目链接: http://acm.uestc.edu.cn/#/problem/show/1307 ABCDE Time Limit: 1000/1000MS (Java/Others)Memory ...

  6. Microsoft Orleans 之简介

    Microsoft Orleans 在.net用简单方法构建高并发.分布式的大型应用程序框架. 原文:http://dotnet.github.io/orleans/ 在线文档:http://dotn ...

  7. 【week12】psp

    psp 项目 内容 开始时间 结束时间 被打断 净时间 12月2日 写博客 对各小组评价 11:20 12:05 0 45 写博客 final评价1 23:40 23:57 0 17 12月5日 看论 ...

  8. 第190天:js---String常用属性和方法(最全)

    String常用属性和方法 一.string对象构造函数 /*string对象构造函数*/ console.log('字符串即对象');//字符串即对象 //传统方式 - 背后会自动将其转换成对象 / ...

  9. css实现 显示一行文字,超出用...代替

    overflow:hidden; white-space:nowrap; text-overflow:ellipsis;

  10. C#中的静态常量(const)和动态常量(static和readonly)用法和区别

    C#中有两种常量类型,分别为readonly(运行时常量)与const(编译时常量),本文将就这两种类型的不同特性进行比较并说明各自的适用场景.工作原理 readonly为运行时常量,程序运行时进行赋 ...