简体   繁体   English

Gradle、Robolectric 和 Espresso

[英]Gradle, Robolectric, and Espresso

Has anyone successfully gotten Robolectric and Espresso working (together) when building with Gradle (Android Studio)?有没有人在使用 Gradle (Android Studio) 构建时成功地让 Robolectric 和 Espresso 工作(一起)?

I've gotten Robolectric working in Android Studio due largely to Peter Friese's post http://www.peterfriese.de/android-testing-with-robolectric/ , but I'm unsure of how to integrate Espresso due to instrumentRunner/instrumentTest collisions.我在 Android Studio 中使用了 Robolectric 主要是因为 Peter Friese 的帖子http://www.peterfriese.de/android-testing-with-robolectric/ ,但由于 instrumentRunner/instrumentTest 冲突,我不确定如何集成 Espresso .

Ideally I'd have a directory structure like the following:理想情况下,我会有一个如下所示的目录结构:

 |--src
    └── main (application source - exists)
    └── test (Robolectric unit tests go here - exists)
    └── testEspresso [*new*] (espresso tests go here)

My Gradle+Android knowledge isn't very extensive, and I'm unsure of whether this is really a feasible thing (time, complexity, and fragility levels aren't too extreme) given the current state of Gradle+Android and if it is, how to go about doing so.我的 Gradle+Android 知识不是很广泛,考虑到 Gradle+Android 的当前状态,我不确定这是否真的可行(时间、复杂性和脆弱性级别不是太极端),如果是,如何去做。 Also in case it's of relevance, I have 2 different build flavors.此外,如果它具有相关性,我有 2 种不同的构建风格。

For a good example of Robolectric and Espresso working together in harmony, check out this sample project created by the Robolectric team:有关 Robolectric 和 Espresso 协同工作的一个很好的例子,请查看由 Robolectric 团队创建的这个示例项目:

https://github.com/robolectric/deckard-gradle https://github.com/robolectric/deckard-gradle

Another option, which doesn't include any "hacks, Gradle plugins, IDE plugins, or IML editing" is http://blog.blundellapps.co.uk/android-gradle-app-with-robolectric-junit-tests/ .另一个不包括任何“黑客、Gradle 插件、IDE 插件或 IML 编辑”的选项是http://blog.blundellapps.co.uk/android-gradle-app-with-robolectric-junit-tests/ Jake Wharton recommended this approach. Jake Wharton 推荐了这种方法。 With this, your Robolectric tests are in a separate Java module, separate from your main project.有了这个,您的 Robolectric 测试位于一个单独的 Java 模块中,与您的主项目分开。 Espresso code lives with your main project. Espresso 代码存在于您的主要项目中。

Using JW's double-espresso ( https://github.com/JakeWharton/double-espresso ) will simplify including Espresso and the dependency management madness that can come with it.使用 JW 的 double-espresso ( https://github.com/JakeWharton/double-espresso ) 将简化包括 Espresso 和它可能带来的依赖管理疯狂。

The solution @plackemacher mentioned, deckard-gradle, is still an option and is being maintained, though it does require some hacking/surgery to get working with the IDE. @plackemacher 提到的解决方案 decard-gradle 仍然是一个选项并且正在维护,尽管它确实需要一些黑客/手术才能使用 IDE。

Update : See the discussion here regarding Robolectric and Gradle (relates to Gradle): https://groups.google.com/forum/#!topic/robolectric/xsOpEwtdTi4/discussion更新:请参阅此处有关 Robolectric 和 Gradle(与 Gradle 相关)的讨论: https ://groups.google.com/forum/#!topic/robolectric/xsOpEwtdTi4/discussion

Update : While I have not used it, this looks to be the ticket.更新:虽然我没有用过,但这看起来是票。 https://github.com/JakeWharton/double-espresso . https://github.com/JakeWharton/double-espresso


For the time being, I elected to create a separate Espresso branch (git) which has the proper testRunner and test directory set.目前,我选择创建一个单独的 Espresso 分支 (git),它具有适当的 testRunner 和测试目录集。 Not a horribly elegant solution... does require some vigilance to keep everything up-to-date, but it's working for now!不是一个非常优雅的解决方案......确实需要一些警惕以保持所有内容都是最新的,但它现在正在工作!

Will definitely change the answer should somebody find a better solution.如果有人找到更好的解决方案,肯定会改变答案。

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

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