views:

15564

answers:

12

I'd like to unit test my Android application but I found that test driven development in Android is far from trivial at the moment.

Any tips, tricks, war stories for building light weight and preferably fast running tests?

+4  A: 

Aside from easily testing non platform dependent logic I haven't found a clever way to run tests, so far (at least for me) any actual platform logic testing is cumbersome. It's almost non trivial anyway because I've found differences in implementation between the emulator and my actual device and I hate to run a unit test implementation on my device just to remove the application afterwards.

My strategy has been: Try to be concise and make the logic well thought out and then test implementation piece by piece (less then desirable).

Quintin Robinson
+3  A: 

Working in android has helped me keep the separation of concerns in order. Keep as much logic out of the view as possible. Follow a common UI design pattern like MVC or MVP. Then that model logic can be unit tested with straight jUnit. I have three projects setup in eclipse.

  • One is the Android applications project.
  • The second is the test project where the view-dependent tests are run by adb
  • The third is where the standard JUnit tests are located.

This doesn't change the fact that using adb shell to run the Android tests is cumbersome. All I've been able to do is minimize the number of cumbersome tests.

As far as war stories: I was happy to figure out round trip testing the custom Parcelable.

James A Wilson
+12  A: 

Looking at ApiDemos sample app, i found ActivityUnitTestCase and ActivityInstrumentationTestCase classes.

Seems that these are utility classes for testing android programs.

Here are the links to the reference:
http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase.html
and
http://developer.android.com/reference/android/test/ActivityUnitTestCase.html

Also android.jar includes a subset of JUnit test framework for plain old unit test. Take a look at ApiDemos sample for learning how to write and run it.

Hope this help!

Gian Marco Gherardi
ActivityIntrumentationTestCase is now deprecated. The docs points to ActivityInstrumentationTestCase2:http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html
Ricky AH
A: 

I recommend using EasyMock extensively for unit testing. I recommend it highly -- the only problem is that somehow on Android (because of some Dalvik VM issues), it can only mock interface classes, and throws errors otherwise. We work around this by creating a TestableClass for every Class that we have, and making that class implement a mock interface that we can test against. I can describe more if people are interested.

Artem
I'm interested in using EasyMock, would you care to elaborate on your workaround? What does your TestableClass look like?
Sarp Centel
+1  A: 

Anyone tried the Mockito Framework in conjunction with android development? I always wanted to try it out. It is similar to EasyMock but doesn't depend on interfaces. It seems to create descending classes from your classes under test transparently. The only limitation I'm aware of is testing of final classes and function does not work...

Frank Meißner
+2  A: 

I found this presentation over at Slideshare to be helpful (http://www.slideshare.net/dtmilano/testing-on-android). This combined with the blog post over at 8th Light (http://blog.8thlight.com/articles/2009/7/12/up-and-running-with-tdd-on-android) and looking at the unit test examples, scarce as they may be, in the API Demos app helped me to get started doing TDD on my Android app.

Jeremy Anderson
+22  A: 

You should try Robotium! Go to Robotium.org and download the example test project. Robotium is a really easy to use framework that makes testing of android applications easy and fast. I created it to make testing of advanced android applications possible with minimum effort. Its used in conjunction with ActivityInstrumentationTestCase2.

Renas
that looks great, thanks for tip.
Matthias
How did you get a referance to textviews, buttons and edittext's? I tried to reach them by enterText(R.id.myTextView, "Hello") but that did not work. enterText requires an int, which I assumes was the int index/name of the Textview in R.java.
Sara
+2  A: 

Three projects aren't necessary (per James answer above). You can get POJO junit 4 tests to run in the Android test project without having the emulator running or device connected. I think the best practice is two projects - one for source, one for tests, and within tests, expect to have tests that are POJO (no android references/emulation required) and tests that require emulation.

But a catch (and a fix)... When I used the Eclipse Android Plugin to "Create a New Android Test Project", Eclipse wouldn't run the junit test with the junit test runner, it will only run them with the Android test runner on the emulator or attached device. Even after I created a new JUnit 4 test and eclipse added Junit 4 jar to the project.

The fix: Go to Run > Run Configurations... Select your Junit test case run config - the one that failed. To the left of Apply it will probably say "Using Eclipse JUnit Test Launcher - Select other...". Even though this seems right, it's not. Click Select other... and choose Android JUnit Test Launcher. Click Run and it should work. If it doesn't, right click on your test case and chose Run As... > JUnit Test. Thanks to Dan Syrstad for the tip.

I believe the initial run config should be Android JUnit Test, but that when you run you can select the JUnit Test config to not require emulation.

And be sure to connect your device for Android unit tests - its WAY faster than the emulator - no start-up time. Very easy to configure - see instructions here:

http://developer.android.com/guide/developing/device.html

Peter Pascale
A: 

Is there anyway testing be done on existing android applications like music player and camera? Robotium seems to be a very easy way to do testing but it cannot be used for existing android applications :(

pysup
why would you want to write a test suite for an app you haven't written? or wait, are you the new QA intern at Google? just kidding.
Matthias
+1  A: 

Here's a ScreenCast I made on how I got Unit Tests to work. Simple Unit Tests and more complex unit tests that depend on having a reference to Context or Activity objects.

http://www.gubatron.com/blog/2010/05/02/how-to-do-unit-testing-on-android-with-eclipse/

Gubatron
+3  A: 

I've been working with Android Unit and Functional tests using the Instrumentation Framework. Its documentation is now clearer than in the past. These links can guide you to start testing:

paulovic
+2  A: 

Although, this is the old question.

Just to keep all answers togeher: Here Stephen Ng provides good aproach for real Unit Test for Android projects solution: https://sites.google.com/site/androiddevtesting/

denis
But I stuck to test WebVeiw with his android.jar. So I've wrote small parcer on Perl with regexp and made android.jar without any exception. It works fine for me. Hope it would be useful for somebody else.http://www.4shared.com/get/KWwSl5an/android.html
denis
More details on problem solwing in my blog: http://denistimofeev.livejournal.com/3072.html
denis