Robolectric VS Android的测试框架 [英] Robolectric vs Android Test Framework

查看:231
本文介绍了Robolectric VS Android的测试框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问 Robolectric 提供比任何明显的好处 Android的测试框架?我读过有关这两个框架,但据我可以看到有关的唯一明确的利益文档的 Robolectric 的是,它运行在JVM而不是DalvikVM,使得它的速度比Android框架。

Does Robolectric provide any clear benefits compared to Android Test Framework ? I've read the docs regarding both the frameworks but as far as i can see the only clear cut benefit regarding Robolectric is that it runs on JVM rather than the DalvikVM, making it faster than Android framework.

是否有脱颖而出任何其他主要的好处?

Are there any other major benefits that stand out ?

推荐答案

更新APR-2015年:摇篮构建工具和Android工作室的现在正式支持单元测试和prevent的android.jar从投掷存根(没有真正的实现)错误。所以,是的它可以运行在Java虚拟机测试,当存根是适当嘲笑。它是一个开端,但仍没有可比性与Robolectric的力量。

Update Apr-2015: Gradle build tools and Android Studio now officially support unit testing and prevent android.jar from throwing stub (no real implementation) error. So, yes its possible to run tests on Java VM, when stubs are appropriately mocked. Its a start but is still not comparable with Robolectric's power.

现在,大约Robolectric:

Now, about Robolectric :

优点:下面是关于如何已被证明是在单元测试中非常有用几点:

Pros: here are a few points about how it has proved to be useful in unit testing:

  1. 您不必运行模拟器,所以你可以测试一些项目的非UI部分,而不需要仿真器或设备。这也适用于在持续集成运行测试/构建服务器,没有仿真器实例需要推出。

  1. You need not run an emulator, So you can test some of the project's non UI parts without requiring an emulator or device. This also applies to running tests on continuous integration/ build servers, no emulator instances need be launched.

随着Android的工作室,就可以快速运行一个特定的测试类,你的工作在实现时,以满足测试案例。您可以调试你写code。这是一个巨大的增值。

With Android Studio, you can quickly run one particular test class, when you are working on the implementation to satisfy the test cases. You can debug as you write code. This is a massive productivity gain.

可以伪造,几乎每一个Android相关的东西的影子对象,甚至是SQLite的。加上每个阴影对象公开其正常的Andr​​oid同行不提供许多有用的功能。与Android对象的影子同行,你可以做内部检查或致电特殊的方法。

Can fake almost every android related thing as shadow objects, even SQLite. Plus each shadow object exposes many useful functions which their normal android counterparts don't offer. With shadow counterparts of android Object, you can do internal checking or call special methods.

真的测试类似的AsyncTask S,活套和<$多线程code时闪耀C $ C>处理程序等,您可以暂停和快进线活套,即使主线程。优秀的处理器基于回调测试。

Really shines when testing multi threaded code like AsyncTasks, Loopers and Handlers etc. You can pause and fast-forward thread Loopers, even main thread. Excellent for Handler based callback testing.

的JUnit 4格式的支持。 Android仍持有到JUnit 3中我最后一次检查。

JUnit 4 format supported. Android is still holding onto JUnit 3 last time I checked.

可以用类似的Mockito,ES $ P $其他测试工具结合PSSO等等等等。

Can be combined with other test tools like Mockito, Espresso etc etc.

支持模拟活动实例创建 Robolectric.buildActivity()并通过 ActivityController 其控制。片段/视图操作也适用于这样的模拟活动情况。

Supports mock Activity instance creationRobolectric.buildActivity() and its control via ActivityController. Fragment/View manipulation also works on such mock activity instances.

有现在提供附加模块覆盖多地塞米松,V4支持,发挥服务,地图和HTTP客户端也是如此。所以,它现在很容易使用这些库功能以及测试code。

There are now provided add-on modules that cover multi-dex, v4-support, play services, maps and http client as well. So, its now easy to test code using these library functions as well.

缺点:当我发现它不那么好:

Cons: Where I found it not so good:

  1. Robolectric擅长辅助单元测试,但并没有涵盖所有功能的真正的设备或仿真器可以提供。例如传感器,全球定位系统,开放-GL等等等等。

  1. Robolectric excels at aiding Unit testing, but does not cover all the functionality a real device or emulator can offer. For example sensors, gps, open-gl etc etc.

在做整合或UI测试时,您将需要一个仿真器或真实设备,使活动和服务能与完整的Andr​​oid环境互动(其他应用程序,像使用相机应用程序,以获取有关您的应用程序的图片),不是有限的。在这里,您将需要使用默认的测试框架,因为它具有功能测试的用户界面也是如此。

You'll need an emulator or real device when doing integration or UI testing, so that Activities and services can interact with full android environment (other apps, like using camera app to get a picture for your app), not a limited one. Here you'll need to use the default test framework as it has functions to test UI as well.

JNI装载似乎并没有被支持。因此code与本地的依赖无法进行测试。

JNI loading seems not to be supported. So code with native dependency can't be tested.

截至目前,Robolectric有一个硬接线的依赖谷歌地图上坛子工作。并且将从行家下载其它的android.jar。因此,项目设置可能需要一点修修补补。 更新:作为V3似乎拉通过摇篮所有的依赖没有太多大惊小怪

As of now, Robolectric has a hard wired dependency on google maps jar to work. And will download another android.jar from maven. So, project setup may require a bit of a tinkering. Update: as of v3 it seems to pull all dependencies via Gradle without much fuss.

较新的Andr​​oid的工具支持的覆盖面和报告生成等,但只有当测试一个设备上运行。因此,与Robolectric你必须创建额外的摇篮任务(运行Jaococ)来为你做它。

Newer Android tools support coverage and reports generation etc, but only when test are run on a device. So with Robolectric you'll have to create extra Gradle tasks (run Jaococ) to do it for you.

由于两个摇篮和Android构建工具发运较新版本的版本以很快的速度,稳定的Robolectric版本有时会开始有问题,改变的构建工具。最典型的问题包括:SDK版本不兼容,明显没有找到,建立输出路径不匹配,资源不加载,建立配置问题等一些问题也关系到Android的工具的bug。有时你甚至可能需要编写自己的自定义测试运行或应用解决方法,直到下一个版本修复了这些问题。退房href="https://github.com/robolectric/robolectric/issues" rel="nofollow">开放问题

As both gradle and android build tools are shipping out newer build versions at a fast rate, stable Robolectric versions will sometimes start having problems with the changed build tooling. Most typical problems include: sdk version incompatible, manifest not found, build output paths mismatch, resources not loading, build config issues etc. Some issues are also related to bugs in android tools. At times you may even have to write your own custom test runner or apply workarounds till next version fixes those issues. Check out open issues and configure tests accordingly.

这篇关于Robolectric VS Android的测试框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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