Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JUnit4 testing

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.

like image 534
gregory561 Avatar asked Jul 22 '11 10:07

gregory561


People also ask

What is Android instrumentation testing?

In summary, an instrumentation test provides a special test execution environment as launched via the am instrument command, where the targeted application process is restarted and initialized with basic application context, and an instrumentation thread is started inside the application process VM.

Why do you use the Android Junitrunner when running UL tests?

When you use AndroidJUnitRunner to run your tests, you can access the context for the app under test by calling the static ApplicationProvider. getApplicationContext() method. If you've created a custom subclass of Application in your app, this method returns your custom subclass's context.


2 Answers

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

like image 125
Dr. Daniel Thommes Avatar answered Sep 18 '22 15:09

Dr. Daniel Thommes


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?

like image 32
Paul Butcher Avatar answered Sep 18 '22 15:09

Paul Butcher