Robolectric 是单元测试还是集成测试?

Robolectric is unit or integration test?

我知道在官方Robolectric网站上说这个框架是用于单元测试的,但是在所有示例robolectric上都用于测试视图...例如:Robolectric可以用来检查textview X的文本是否为"dog",可以检查按钮是否可点击...但它不是单元测试...它是集成测试,我也可以用espresso来做...

如果 robolectric 真的是单元测试,我可以用它来测试视图模型、存储库、扩展吗?

如果是单元测试,你能给我一个仅使用 Robolectric(无 Mockito)显示视图模型测试的示例吗?

几年前我在他们的网站上报道了一些解释 在这个旧 我写了

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.

要了解 Roboelectric 的概念,需要了解什么是模仿 Android class 的 Shadows 对象。当您需要测试 Android 实施时,可能很难执行 Junit 测试,因为有很多 Android 组件:Views、Intent、Bundle、Fragments只是列举其中一些 Roboelectric 可以测试的。此外,如果您的代码是紧密耦合的,则可能难以 运行 UI 测试,这些测试需要通过模拟器 运行 并且非常慢,尽管经常需要。 Junit 测试要快得多并且不需要模拟器。 Roboelectric 可帮助您测试特定 Android 组件,而无需使用模拟器。

您要求的 viewModel 样本实际上是一个通用问题,因为 ViewModels 可能真的不同。通常 viewModel 包含视图的业务逻辑 而通常不引用视图 ,因此应该可以使用 Mockito 进行测试。在 Whosebug 中,我们不对使用哪个框架发表意见,因此我不会做出任何判断。因此,任何 Roboelectric 示例都应该向您说明如何在 viewModel 或任何需要的地方使用它,视图可能是更好的地方。请注意,Roboelectric 作为每个框架都有利有弊,但我们无法在 Whosebug 上讨论这一点,互联网上的研究和您的玩具项目将为您提供帮助。

此外,当您进行 Junit 测试时,您是在孤立地进行测试,因此 class 具有相对价值,因为存在用于测试您的 SUT 被测系统的业务逻辑。