Android JUnit4 testing

ⅰ亾dé卋堺 提交于 2019-12-23 07:47:35

问题


I want to run some JUnit4 tests. The code relies on some Android libraries(Android XML parser), but does not create any activites and so on. When I try to run tests I got that an Android class that I need was not found. Is there any way to run JUnit4 tests with Android code, not to test activity but to test code with some libraries.


回答1:


There is no way (that I'm aware of) to use JUnit4 on Android. It does support JUnit3 though, if that's an option for you?

Alternatively, you could use Robolectric and run your tests on your development machine (where you'll be able to use whichever unit test framework you like). Whether this will work for you depends on exactly what you're testing, but it might be worth a go?




回答2:


I had the same problem and tried to adapt JUnit4 to Android's existing TestRunner - without success. Therefore I created a new project called JUnit4Android. It's a TestRunner application library for JUnit4 and JUnit3 tests and test suites. So you can run your existing JUnit4 tests with it. Please find more information on GitHub:

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




回答3:


It might be little bit late, but there's finally an official update from Google about junit4:

based on Android-test-kit project and some other sources it's clear that: The AndroidJUnitRunner is a new unbundled test runner for Android, which is part of the

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

Actually, it's already presented in Support Repository. If You go to

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

it's possible to find testing-support-lib in there (aar, jars etc.) which allows to use JUnit4. Even more, it contains espresso library same location which is handy for UI testing. Seems Android sites and support lib official references will be updated soon with that info.



来源:https://stackoverflow.com/questions/6788953/android-junit4-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!