Android单元测试的简单使用

发表于:2017-8-28 14:47

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:单身狗的清香    来源:51Testing软件测试网采编

  我对测试的粗浅理解
  任何软件的开发过程中,测试绝对是不可或缺的一环,其重要程度几乎和开发不相上下。
  然鹅!
  在我多年的开发经验中(容我装个B(。???)ノ),遇到的包含单元测试的项目或者愿意主动写测试的程序员却少之又少。
  当然我们有一大堆不写测试的理由,比如项目时间太紧张或者项目太简单,测试没必要,再或者我是大神,代码根本不会出现bug。
  还有更根本的原因就是大多数开发者在学习过程中,注重的更多是开发相关的知识掌握和技能水平的提高,很少有人注重提高自己的测试水平,大多数人都是了解一下就跳过了(包括我自己,捂脸.jpg)。由于各方面的不重视,现在好多新手对单元测试一脸懵逼。我也亲眼目睹过好多项目在开发伊始,程序员就先删掉了单元测试相关的依赖和文件目录...
  其实就算不是为了提早发现代码中隐藏的bug,写单元测试也能给我们的开发过程带来很大的便利。
  举一个简单的例子:如果没有单元测试,那么我们如何验证项目中一段代码的正确性,它的输出是不是符合我们的预期?大概你会说我跑一遍项目不就可以了么?事实上你也只能这么干。但是有了单元测试就不一样了,我们不必为了验证一段代码去重新build整个项目,尤其是一些纯Java的,与整个Android SDK无关的代码。这样会大大的提高我我们的开发效率。要知道Android的编译速度,那简直是...男默女泪(也可能是我电脑太渣)
  扯了这么多,我就是想说,学会写单元测试对开发者来说很重要,不光是对Android开发者,对任何开发者都是,它是任何一个高逼格的程序员所应当具备的基本素质之一。
  再次然鹅!
  网上的单元测试教程都特别深奥或者千篇一律,像我这种小白看完之后也是摸不着头脑,也不知道该如何下手,所以我就写了这篇简单的Android单元测试教程,也算是抛砖引玉。
  官方简介
  现阶段,Android Studio对单元测试的支持已经相当人性化了,开发者只需动动鼠标就能构建出相关的测试类,甚至大部分测试代码。而我们只要加上需要测试的部分就可以了。
  先贴一段Google爸爸对单元测试的简单介绍:
  Getting Started with Testing
  Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as instrumented tests on an Android device.
  原文戳这里(需科学上网):https://developer.android.com/training/testing/start/index.html?hl=zh-cn#test-types
  测试前的准备
  首先要在 moudle-name下的build.gradle文件里面引入相关的依赖:
  (只贴出了和测试相关的)
  其中Espresso是Google官方提供并推荐使用的测试库。
  apply plugin: 'com.android.application'
  android {
      defaultConfig {
           testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      }
  }
  dependencies {
      androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
          exclude group: 'com.android.support', module: 'support-annotations'
      })
      testCompile 'junit:junit:4.12'
  }
  然后在你项目的src文件夹下新建测试相关的目录
  新建单元测试目录
  这几个目录的名字基本上是固定的,一个是androidTest,一个是test;顾名思义,一个是用来进行Android API相关的测试,一个用作Java本地测试。这两个目录下面又各有一个java目录,再下面有以项目的ApplicationId命名的目录,这个名字能不能改我倒是没有研究过,不过最好还是别改了吧。然后再往里面就是你的测试类了。
  如果是新建项目自动生成的测试相关目录,这些测试类包括里面的一部分代码都是生成好了的,但是假若新建项目的时候没有勾选自动生成单元测试的选项或者单元测试后来被删除了的话就需要手动新建这些目录和测试类了。
  开始编写测试代码并进行单元测试
  开始之前我们先来看下Google官方的描述:
  Local unit tests
  Located at module-name
  /src/test/java/
  These tests run on the local JVM and do not have access to functional Android framework APIs.
  To get started, see Building Local Unit Tests.
  Instrumented tests
  Located at module-name
  /src/androidTest/java/
  These are all tests that must run on an Android hardware device or an Android emulator.
  Instrumented tests are built into an APK that runs on the device alongside your app under test. The system runs your test APK and your app under tests in the same process, so your tests can invoke methods and modify fields in the app, and automate user interaction with your app.
  For information about how to create instrumented tests, see the following topics:
  Building Instrumented Unit Tests: Build complex unit tests with Android dependencies that cannot be satisfied with mock objects.
  Automating User Interface Tests: Create tests to verify that the user interface behaves correctly for user interactions within a single app or for interactions across multiple apps.
  Testing App Component Integrations: Verify the behavior of components that users do not directly interact with, such as a Service or a Content Provider.
  从上面的描述我们可以了解到本地化测试和图形测试的步骤:

21/212>
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号