简体   繁体   English

用Robolectric测试什么?

[英]What to test with Robolectric?

It seems to me that I am fundamentally misunderstanding the purpose of Robolectric. 在我看来,我从根本上误解了Robolectric的目的。 I've been battling with it for a week already, and so far getting a new error message is considered as making progress. 我已经与它争斗了一个星期,到目前为止,获得新的错误消息被视为取得进展。 I am able to test some basic stuff like static views in an activity, but when something more complicated things come into play things just fall apart. 我能够在一个活动中测试一些基本的东西,比如静态视图,但是当一些更复杂的东西发挥作用时,事情就会崩溃。 I had to extend Robolectric to support 3-rd party libraries with certain parameters, Appcompat action bars and numerous other things which was extremely time-consuming and wasn't really documented anywhere, and things are advancing at a pretty much glacial pace. 我不得不扩展Robolectric以支持具有某些参数的第3方库,Appcompat操作栏和许多其他非常耗时的事情,并且在任何地方都没有真正记录,并且事情正在以相当快的速度发展。 I am starting to think that I am using it in a wrong way and it simply isn't supposed to do what I want it to do. 我开始认为我以错误的方式使用它,它根本不应该做我想做的事情。

The general app logic is quite straightforward so there isn't really much to unit test, the most complicated stuff is in the UI and remote API calls. 一般的应用程序逻辑非常简单,因此单元测试并不多,最复杂的是UI和远程API调用。 Is Robolectric just supposed to make unit testing for Android less painful than with JUnit because it can run on the JVM and supports a few Android classes? Robolectric是否应该让Android的单元测试比JUnit更少痛苦,因为它可以在JVM上运行并支持一些Android类? Perhaps a black-box behaviour testing framework like Espresso would be more suitable for my needs? 也许像Espresso这样的黑盒行为测试框架更适合我的需求? But we use continuos integration, and Robolectric was nice and easy to set up to run tests on the CI server, and I'd kind of like to keep it that way. 但我们使用连续集成,并且Robolectric很好并且很容易设置为在CI服务器上运行测试,我有点想保持这种方式。

What do you use Robolectric for? 你用什么Robolectric? A lot of blog posts recommend it for "activity lifecycle testing", but since I'm also quite new in the Android world, I don't really understand the purpose of it, especially since the app I'm testing is portrait-only. 很多博客文章推荐它用于“活动生命周期测试”,但由于我在Android世界中也很新,我真的不明白它的目的,特别是因为我正在测试的应用程序是仅限肖像的。 Could someone please give an overview of what you use Robolectric for, and how do you do it, preferably with code examples and explain why and how those tests are important? 有人可以概述您使用Robolectric的方法,以及如何使用,最好使用代码示例并解释为什么以及如何重要这些测试?

We use it for: 我们用它来:

  • unit testing: all components from parsers and utils, to controllers and presenters 单元测试:解析器和工具,控制器和演示者的所有组件
  • integration/acceptance testing: the business logic of the app, per screen (which falls into integration and/or acceptance testing) 集成/验收测试:应用程序的业务逻辑,每个屏幕(属于集成和/或验收测试)

We don't use it for (and have found it difficult to use for these): 我们不使用它(并且发现很难用于这些):

  • testing the network layer (we run all tests by injecting the test data in the same way the network layer would; parsers are tested separately) 测试网络层(我们通过以与网络层相同的方式注入测试数据来运行所有测试;解析器单独测试)
  • user flows through different screens 用户流经不同的屏幕

If you're looking for more of the latter, perhaps Espresso/Robotium are better suited for your needs. 如果您正在寻找更多后者,也许Espresso / Robotium更适合您的需求。 And you absolutely can run these as part of your CI pipeline, but you'll need to invest some time in setup, or integrating with something like Appurify. 您绝对可以将这些作为CI管道的一部分运行,但是您需要花一些时间进行设置,或者与Appurify之类的东西集成。

If you are finding it very difficult to write your tests, it might have to do more with the way your app is architected than the way you're using robolectric. 如果您发现编写测试非常困难,那么构建应用程序的方式可能需要比使用robolectric的方式更多。 See my answer here as well, it might help you: Writing Android acceptance tests with robolectric: how could it be done? 在这里也可以看到我的答案,它可能会对你有所帮助: 使用robolectric编写Android验收测试:如何才能完成?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM