Android JUnit4测试

8

我想运行一些JUnit4测试。代码依赖于一些Android库(Android XML解析器),但不会创建任何活动等内容。当我尝试运行测试时,我得到了一个需要的Android类找不到的错误。有没有办法运行带有Android代码的JUnit4测试,而不是测试活动,而是测试带有某些库的代码。


可能重复:https://dev59.com/KnI95IYBdhLWcg3wvwsQ - Jeff Axelrod
3个回答

9
我曾经遇到同样的问题,尝试将JUnit4适应Android现有的TestRunner,但未获成功。因此我创建了一个名为JUnit4Android的新项目。它是一个TestRunner应用程序库,可用于JUnit4和JUnit3测试和测试套件。因此,您可以使用它来运行现有的JUnit4测试。请在GitHub上查找更多信息:

https://github.com/dthommes/JUnit4Android/wiki


1
对于现在阅读此内容的任何人,我也建议研究一下AndroidJUnit4。它将JUnit 4和DBUnit等带到了Android平台。https://github.com/esmasui/AndroidJUnit4 - spaaarky21

3

据我所知,目前无法在Android上使用JUnit4。但如果您可以使用JUnit3,那么它是支持的。

或者,您可以使用Robolectric在开发机器上运行测试(这样您就可以使用任何喜欢的单元测试框架)。是否适用取决于您要测试的内容,但值得一试。


0

或许有些晚了,但 Google 终于发布了 JUnit4 的官方更新:

根据 Android-test-kit 项目 和其他一些来源可以明确,AndroidJUnitRunner 是 Android 的新解绑定测试运行器,是该平台的一部分。

Android Support Test Library and can be downloaded via the Android Support Repository. The new runner contains all improvements of GoogleInstrumentationTestRunner and adds more features:

- JUnit4 support
- Instrumentation Registry for accessing Instrumentation, Context and Bundle Arguments
- Test Filters @SdkSupress and @RequiresDevice
- Test timeouts
- Sharding of tests
- RunListener support to hook into the test run lifecycle
- Activity monitoring mechanism ActivityLifecycleMonitorRegistry

实际上,它已经在Support Repository中提供了。如果您前往

%ANDROID_HOME%\extras\android\m2repository\com\android\support\test\testing-support-lib\

您可以在其中找到testing-support-lib(aar、jar等),这允许使用JUnit4。更重要的是,它包含了Espresso库相同的位置,这对于UI测试非常方便。看起来Android网站和支持库官方参考资料将很快更新该信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接