简体   繁体   English

在 Android 设备上运行 Robolectric 测试的最佳方法

[英]Best way to run Robolectric tests on Android device

I have a Robolectric test project setup, but I'd like to also run these tests on my device to check that I don't get bit by JVM vs Dalvik implementation differences.我有一个 Robolectric 测试项目设置,但我还想在我的设备上运行这些测试,以检查我没有被 JVM 与 Dalvik 实现差异所困扰。

Unlike robolectric tests, I won't run these tests frequently.与 robolectric 测试不同,我不会经常运行这些测试。 My concern is that there's little effort to maintain the test suite and that they verify actual device functionality.我担心的是维护测试套件并验证实际设备功能的努力很少。

What's the best way to do that?最好的方法是什么?


What I've currently got:我目前拥有的:

My robolectric test project as a test case TestPackage.我的 robolectric 测试项目作为测试用例 TestPackage。 I created an Android Test project with a test case TestRoboOnAndroid.我使用测试用例 TestRoboOnAndroid 创建了一个 Android 测试项目。 It creates a TestPackage and has a test for each test in TestPackage.它创建一个 TestPackage 并为 TestPackage 中的每个测试提供一个测试。

Right now, every time I add a test to my robolectric suite, I need to manually add it to my device suite.现在,每次我向我的 robolectric 套件添加测试时,我都需要手动将它添加到我的设备套件中。 Is there some way to do that automatically with reflection?有没有办法通过反射自动做到这一点?

Also, Robolectric uses JUnit 4 (by default) and Android uses JUnit 3. So I have to write all of my Robolectric tests using JUnit 3 style (importing from junit.framework instead of org.junit ). Also, Robolectric uses JUnit 4 (by default) and Android uses JUnit 3. So I have to write all of my Robolectric tests using JUnit 3 style (importing from junit.framework instead of org.junit ).

The whole point of Robolectric is NOT to run it on the device and the design is based on that. Robolectric 的重点不是在设备上运行它,设计就是基于此。 If you want to run something on the device look at the default instrumentation tests from the SDK or Robotium .如果您想在设备上运行某些东西,请查看 SDK 或Robotium中的默认仪器测试。

It is totally feasible to run your Robolectric tests on the JVM and in addition create Robotium tests that rely on device specific interaction (eg creating screenshots, hardware differences...) and run on devices and emulators all combined into one build.在 JVM 上运行您的 Robolectric 测试是完全可行的,此外还可以创建依赖于设备特定交互的 Robotium 测试(例如创建屏幕截图、硬件差异......)并在设备和模拟器上运行,所有这些都组合成一个构建。

The easiest way to do that is to use the Android Maven Plugin .最简单的方法是使用Android Maven 插件

I use a tiered system, where I prefer earlier tiers where possible:我使用分层系统,在可能的情况下我更喜欢较早的层:

  • Pure unit tests .纯单元测试 I try to make as much code as possible fully independent of Android APIs, and then use "pure" unit tests which can run on any JVM.我尝试使尽可能多的代码完全独立于 Android API,然后使用可以在任何 JVM 上运行的“纯”单元测试。 These tests are the fastest, and it helps keep code that has no need to be Android-specific portable.这些测试是最快的,它有助于保持不需要特定于 Android 的可移植代码。

  • Robolectric-supported unit tests . Robolectric 支持的单元测试 Where my code has only small dependencies on Android APIs, that can be satisfied by Robolectric shadows, I test it with Robolectric.我的代码对 Android API 的依赖性很小,可以通过 Robolectric 阴影来满足,我用 Robolectric 对其进行了测试。 There is a little more setup time for Robolectric compared to pure tests, but it's still faster than starting/running on an emulator.与纯测试相比,Robolectric 的设置时间要多一些,但它仍然比在模拟器上启动/运行要快。

  • Android framework tests . Android 框架测试 Where Robolectric doesn't cut it - either because the shadows don't exist, or because I'm heavily using Android APIs (and therefore want to test against the Real Thing) - I write test that run on the emulator/device with the default framework. Robolectric 没有削减它的地方 - 要么是因为阴影不存在,要么是因为我大量使用 Android API(因此想针对真实事物进行测试) - 我编写了在模拟器/设备上运行的测试默认框架。

The point of the tiers is to keep things as simple as possible, which keeps the full suite faster and helps promote cleaner code.层级的重点是使事情尽可能简单,这使整个套件更快,并有助于促进更清洁的代码。

We use Robolectric for unit testing against the JVM and Calabash-Android for system testing against Dalvik.我们使用 Robolectric 对 JVM 进行单元测试,并使用 Calabash-Android 对 Dalvik 进行系统测试。 Both can be integrated into our Jenkins CI build and between the two tools I feel that we cover all the bases.两者都可以集成到我们的 Jenkins CI 构建中,并且在这两个工具之间,我觉得我们涵盖了所有基础。

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

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