Android Studio 中的 Android Instrumentation 测试和单元测试的区别? [英] Difference between Android Instrumentation test and Unit test in Android Studio?

查看:39
本文介绍了Android Studio 中的 Android Instrumentation 测试和单元测试的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Android Studio 1.1rc 开始,有单元测试支持和我想知道 Android Instrumentation 测试和单元测试有什么区别.

As of Android Studio 1.1rc there's Unit testing support and I'm wondering what's the difference between Android Instrumentation Tests and Unit tests.

据我所知:
单元测试对于测试不调用 Android 的代码很有用API 和 Android 仪器测试是相当集成的用于测试 Android API 特定元素或 GUI 组件的测试.

As I understand it:
Unit tests are useful for testing code which doesn't call the Android API, and the Android instrumentation tests are rather integration tests to test Android API specific elements or GUI components.

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

However if you use a framework like Robolectric or Mockito in your unit tests, you can test Android code (without the need of a device) if I'm not mistaken.

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

Is this correct, or is there a bigger difference? If so, what's the use of each?

推荐答案

单元测试 隔离被测组件,这就是为什么经常与 Mocks 框架一起用作 Mockito 的原因:因为隔离单元从它们的依赖项.请注意您关于 Android API 的说法部分正确,因为还有 Instrumented Unit tests,即Instrumentation 也是 Junit 包的一部分,以及扩展 TestCase 作为类的类 AndroidTestCase 是 Junit 包的一部分,但允许使用 A)Context,您可以使用 getContext() 调用它,和 B) 属于 Android API 一部分的资源!还请考虑 AndroidTestCase 是一个基类,还有其他几个非常有用的类扩展了这个类.他们专门测试加载器、内容提供者甚至服务,并且他们还可以访问 Android API.所以这些类提供了 JUnit 测试框架以及 Android 特定的方法.现在使用 Junit4 有直接从 Object 扩展的 ServiceTestRule 并允许您更轻松地测试服务,尽管您不能直接在此类中启动 Intent.

Unit tests isolate the component under test, and this is the reason why are often used together with Mocks frameworks as Mockito:because isolate the unit from their dependencies. Please notice what you say regarding the Android API is partially true, because there are also Instrumented Unit tests, namely Instrumentation is part of the Junit package as well, and also the classes that extend TestCase as the class AndroidTestCase is part of the Junit package but allows the use of A)Context, that you can call with getContext(), and B)Resources that are part of the Android API! Also please consider AndroidTestCase is a base class and there are several other classes quite useful that extend this class. They test specifically Loaders, ContentProviders and even Services and also them have access to the Android API. so these classes provide JUnit testing framework as well as Android-specific methods. Now with Junit4 there is the ServiceTestRule that extends directly from Object and allow you easier to test a Service, although you cannot start an Intent directly inside this class.

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

Instrumentation tests they are also into the Junit package, but the control of the Android API is quite total because Instrumentation Tests are instantiated in the system before any application code is run, and to test you need to open the real application(emulator or a phone connected with USB). They access to android components(ex. click a button) and application life cycle, they are usually slower than Junit tests that extend TestCase( the ones examined above) the typical use is with ActivityInstrumentationTestCase2 that has a functional test approach, more user oriented.

关于 Roboelectric 和 Mockito,它们与 Espresso 在目前最流行的测试框架之间(2016 年 7 月 13 日),Roboelectric 可让您在几秒钟而不是几分钟内运行多个测试,这对于必须运行持续测试且需要持续集成的团队来说非常方便.

Regarding Roboelectric and Mockito, that are togheter with Espresso between the most popular testing frameworks at the moment(13 July 2016), Roboelectric allows you to run multiple tests in seconds instead of minutes, and this comes really handy in teams that have to run continuous tests, and are subject to continuous integration.

来自 Robolectric 的网站:

From the site of Robolectric:

Robolectric 的另一种方法是使用模拟框架,例如Mockito 或模拟 Android SDK.虽然这是一个有效的方法,它通常会产生本质上相反的测试应用程序代码的实现.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.

也可以与 Junit 一起使用的 Mockito,除了必须管理 final 类、匿名类或原始类型时,真正使用.

Mockito that also can be used with Junit, is really used with the exception of when have to manage final classes, anonymous classes or primitive types.

这篇关于Android Studio 中的 Android Instrumentation 测试和单元测试的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆