广告
返回顶部
首页 > 资讯 > 移动开发 >Android单元测试与模拟测试
  • 228
分享到

Android单元测试与模拟测试

android单元测试测试Android 2022-06-06 12:06:17 228人浏览 独家记忆
摘要

  考虑可读性,对于方法名使用表达能力强的方法名,对于测试范式可以考虑使用一种规范, 如 RSpec-style。   不要使用逻辑流关键字(If/ese、for、do/

  考虑可读性,对于方法名使用表达能力强的方法名,对于测试范式可以考虑使用一种规范, 如 RSpec-style。   不要使用逻辑流关键字(If/ese、for、do/while、switch/case),在一个测试方法中,如果需要有这些,拆分到单独的每个测试方法里。   测试真正需要测试的内容,需要覆盖的情况,一般情况只考虑验证输出(如某操作后,显示什么,值是什么)。   考虑耗时,Android Studio默认会输出耗时。   不需要考虑测试 private 的方法,将 private 方法当做黑盒内部组件,测试对其引用的 public 方法即可。   尽可能的解耦对于不同的测试方法,不应该存在Test A与Test B存在时序性的情况。   II. Android自带基本的单元测试   control + shift + R (Android Studio 默认执行单元测试快捷键)。   1. 本地单元测试   直接在开发机上面进行运行测试。在没有依赖或者仅仅只需要简单的Android库依赖的情况下,有限考虑使用该类单元测试。   代码存储   如果是对应不同的flavor或者是build type,直接在test后面加上对应后缀(如对应名为 myFlavor 的单元测试代码,应该放在 src/testMyFlavor/java 下面)。   src/test/java   激活测试   在一个功能测试或验证的测试方法前面添加 @Test 的annotation。   Google官方推荐引用   dependencies {   // Required -- JUnit 4 framework,用于单元测试,google官方推荐   testCompile 'junit:junit:4.12'   // Optional -- Mockito framework,用于模拟架构,google官方推荐   testCompile 'org.mockito:mockito-core:1.10.19'   }   2. 模拟测试   运行在Android设备或者虚拟机上的测试   主要用于测试: 单元(Android架构引用相关的单元测试)、UI、应用组件集成测试(Service、Content Provider、etc.)   代码存储:   src/androidTest/java   Google官方推荐引用   dependencies {   androidTestCompile 'com.android.support:support-annotations:23.0.1'   androidTestCompile 'com.android.support.test:runner:0.4.1'   androidTestCompile 'com.android.support.test:rules:0.4.1'   // Optional -- Hamcrest library   androidTestCompile 'org.hamcrest:hamcrest-library:1.3'   // Optional -- UI testing with Espresso   androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'   // Optional -- UI testing with UI Automator   androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'   }   常见的UI测试   需要模拟Android系统环境   主要三点:   UI加载好后展示的信息是否正确。   在用户某个操作后UI信息是否展示正确。   展示正确的页面供用户操作。III. 拓展工具   1. AssertJ Android   square/assertj-android   极大的提高可读性。   // 一般的JUnit   assertEquals(View.GONE, view.getVisibility());   // AssertJ Android   assertThat(view).isGone();   2. Robolectric   Robolectric   让模拟测试直接在开发机上完成,而不需要在Android系统上。   主要是解决模拟测试中耗时的缺陷,模拟测试需要安装以及跑在Android系统上,也是需要在Android虚拟机或者设备上面,所以十分的耗时。基本上每次来来回回都需要几分钟时间。针对这类问题,业界其实已经有了一个现成的解决方案: Pivotal实验室推出的 Robolectric 。通过使用Robolectrict模拟Android系统核心库的 Shadow Classes 的方式,我们可以像写本地测试一样写这类测试,并且直接运行在工作环境的JVM上,十分方便。   3. Mockito   Mockito   快速模拟控制系统架构返回参数。   不同于Roblectric,Mockito可以通过模拟并控制或修改一些方法的行为。   // 无论什么时候调用 myQueryObject.getCurrentTime,返回值都会是 1363027600   Mockito.doReturn((long) 1363027600).when(myQueryObject).getCurrentTime();   4. Robotium   RobotiumTech/robotium   (Integration Tests)模拟用户操作,事件流测试。   通过模拟用户的操作的行为事件流进行测试,这类测试无法避免需要在虚拟机或者设备上面运行的。是一些用户操作流程与视觉显示强相关的很好的选择。   Building Local Unit Tests   Testing the Android way   timber中的单元测试实例   timber/build.gradle   A BDD (RSpec-like) testing library for Java   © 2012 - 2016, Jacksgong(blog.dreamtobe.cn). Licensed under the Creative Commons Attribution-NonCommercial 3.0 license (This license lets others remix, tweak, and build upon a work non-commercially, and although their new works must also acknowledge the original author and be non-commercial, they don’t have to license their derivative works on the same terms). Http://creativecommons.org/licenses/by-nc/3.0/


--结束END--

本文标题: Android单元测试与模拟测试

本文链接: https://www.lsjlt.com/news/28707.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作