Android Studio 中 Android 仪器测试和单元测试的区别?

Difference between Android Instrumentation test and Unit test in Android Studio?

从 Android Studio 1.1rc 开始,有 Unit testing support 和 我想知道 Android 仪器测试和单元测试之间有什么区别。

据我了解:
单元测试 对于测试不调用 Android 的代码很有用 API,Android 仪器测试 比较集成 用于测试 Android API 个特定元素或 GUI 组件的测试。

但是,如果您在您的应用程序中使用 RobolectricMockito 这样的框架 单元测试,如果我没记错的话,你可以测试 Android 代码(不需要设备)。


这是正确的,还是有更大的区别?如果是这样,每个都有什么用?

在我看来,仪器测试是集成测试,能够控制应用程序的生命周期和事件(onStart、onCreate 等)。

单元测试,据我了解,是测试一个单元(例如Class)的数据和行为。

例如,假设您有一款游戏:该游戏在 activity(主要 activity)上运行,并且您有一个基于机器人 class 的角色,该角色有 2方法(射击和移动)。您将使用仪器测试来测试主要 activity 以查看它在您离开应用程序时是否正确保存,在您恢复它时是否正确恢复等等,并且您将使用单元测试来测试 Robot,以测试其属性和行为。

免责声明:我不是 java 人,但我对你的问题很感兴趣,并根据网上的小搜索回答了它。您可能需要更深入地研究才能找到更详细的答案。

单元测试 隔离被测组件,这就是为什么经常与 Mocks 框架一起使用的原因,因为 Mockito:because 将单元与其依赖项隔离。请注意你所说的关于 Android API 的部分是正确的,因为还有 Instrumented Unit tests, namely Instrumentation is part of the Junit package as well, and also the classes that extend TestCase as the class AndroidTestCase 是 Junit 包的一部分但允许使用 A)Context,你可以调用getContext() 和 B)属于 Android API 的资源!另外请考虑 AndroidTestCase 是一个基础 class 并且还有其他几个非常有用的 classes 扩展这个 class。他们专门测试加载器、ContentProvider 甚至服务,并且他们还可以访问 Android API。因此这些 classes 提供了 JUnit 测试框架以及 Android 特定的方法。现在有了 Junit4,有直接从 Object 扩展的 ServiceTestRule,让您更容易测试服务,尽管您不能直接在此 class.

中启动 Intent

Instrumentation tests它们也在Junit包中,但是AndroidAPI的控制相当全,因为Instrumentation Tests是在任何应用程序代码之前的系统都是运行,要测试你需要打开真正的应用程序(模拟器或通过USB连接的phone)。它们访问 android 组件(例如单击按钮)和应用程序生命周期,它们通常比扩展 TestCase 的 Junit 测试(上面检查的那些)慢,典型用途是使用具有功能测试方法的 ActivityInstrumentationTestCase2,更面向用户。

编辑:关于 Roboelectric 和 Mockito,它们与 Espresso 一起属于目前最流行的测试框架(2016 年 7 月 13 日), Roboelectric 允许您在几秒钟而不是几分钟内 运行 进行多项测试,这对于必须 运行 连续测试并且需要持续集成的团队来说非常方便。

来自 Robolectric 的站点:

An alternate approach to Robolectric is to use mock frameworks such as Mockito or to mock out the Android SDK. While this is a valid approach, it often yields tests that are essentially reverse implementations of the application code. Roboelectric allows a test style that is closer to black box testing, making the tests more effective for refactoring and allowing the tests to focus on the behavior of the application instead of the implementation of Android. You can still use a mocking framework along with Robolectric if you like.

Mockito 也可以与 Junit 一起使用,除了必须管理最终 classes、匿名 classes 或原始类型之外,确实可以使用。

单元测试

Unit tests that run on your local machine only. These tests are compiled to run locally on the JVM to minimize execution time. Use this approach to run unit tests that have no dependencies on the Android framework or have dependencies that mock objects can satisfy.

所以基本上,您 运行 纯 java 代码来测试例如内容提供程序、数据库连接、方法的输入和输出。 Android 上没有 运行。 运行 你不需要设备。

仪器测试

Unit tests that run on an Android device or emulator. These tests have access to Instrumentation information, such as the Context of the app under test. Use this approach to run unit tests that have Android dependencies which mock objects cannot easily satisfy.

所以它模拟了用户将如何使用实际应用程序,因此您需要一个设备(物理或模拟器)来 运行 它。它可以访问视图、活动、上下文等。

参考:http://developer.android.com/tools/testing/testing_android.html

单元测试:

通常单元测试被称为“本地测试”或“本地单元测试”。这样做的主要原因似乎是您希望能够 运行 在没有连接设备或模拟器的情况下进行测试。

如果不模拟 Activity 等对象,单元测试无法为您的应用测试 UI。


仪器测试:

在设备或模拟器上进行仪器测试 运行。在后台,将安装您的应用程序,然后还将安装一个测试应用程序,它将控制您的应用程序,启动它并根据需要 运行 宁 UI 测试。

仪器测试也可用于测试 none UI 逻辑。当您需要测试依赖于上下文的代码时,它们特别有用。


Ref link for example

单元测试

它只在本地机器上运行。

仪器测试用例

它在 android 设备或​​模拟器上运行。如果您检查它在模拟器或 android 设备上运行的测试用例

Android 测试

[Test types]

Android世界测试类型图

本地单元测试:JUnit、Mockito、PowerMock
仪器测试(一种功能测试):Espresso,Robolectric,Robotium

//Functional tests(Integration, UI)
src/androidTest/java

//Unit tests
src/test/java

[Run test via Command line]

[Test double types]

单元测试 关注一小部分代码(即 class 级方法)并提供代码是否按预期工作的基本验证。 Espresso 测试 提供 UI 是否按预期工作的基本验证。