In this tutorial, we’ll discuss Android Unit testing which forms an integral part of Android Application Development. We’ll specifically implement local Unit Testing using JUnit4.
在本教程中,我们将讨论Android单元测试,它构成了Android应用程序开发的组成部分。 我们将特别使用JUnit4实现本地单元测试。
Android单元测试 (Android Unit Testing)
As the name says, Unit testing is testing every unit of your code.
顾名思义,单元测试就是测试代码的每个单元。
Unit testing is a must to build robust applications. It is an important element while building quality applications.
要构建可靠的应用程序,必须进行单元测试。 这是构建高质量应用程序时的重要元素。
Unit testing consists of test cases which are used to check the business logic of your code.
Many times when you have been asked to or plan to add a feature in a working application just to realize that it broke some other part of your code. It’s too much to perform all the tests manually everytime when you refactor your code or add new things in it. This is where Unit Testing comes to our rescue.
单元测试由测试用例组成,这些用例用于检查代码的业务逻辑。
很多时候,当您被要求或计划在正在运行的应用程序中添加功能时,只是意识到它破坏了代码的其他部分。 每次在重构代码或向其中添加新内容时,手动进行所有测试都是太多了。 这就是单元测试为我们提供帮助的地方。
It performs quick automatic tests and alerts you if any test failed. You can quickly identify the issue.
它执行快速的自动测试,并在任何测试失败时提醒您。 您可以快速找出问题所在。
In general Testing is largely divided into the following types:
一般而言,测试大致分为以下几种类型:
- Unit Tests 单元测试
- Integration Tests 整合测试
- UI Tests UI测试
Unit tests are the smallest (individually) and with the least execution time.
单元测试是最小的(单个)并且执行时间最少。
Following is an illustration quantifying each of the testing types from the Google Docs:
以下是量化Google文档中每种测试类型的图示:
Following are some of the testing frameworks used in Android:
以下是Android中使用的一些测试框架:
Whenever you start a new Android Studio Project, JUnit dependency is already present in the build.gradle
(also Expresso Dependency).
每当您启动新的Android Studio项目时, build.gradle
就已经存在JUnit依赖关系(也build.gradle
Expresso依赖关系)。
In your Android Studio Project, the following are the three important packages inside the src folder:
在您的Android Studio项目中