Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Driven Development For Android

Can we use JUnit for test driven development in Android ? If not, is there an alternative similar to JUnit ?

I searched a bit on google and also read a SO post Android Test Driven Development It looks like Android was never made with TDD in mind. I wanted to be sure before I begin learning TDD and doing Android development at the same time.

like image 905
Apple Grinder Avatar asked Dec 04 '12 10:12

Apple Grinder


People also ask

What is TDD and BDD Android?

What is BDD? BDD (Behavior Driven Development) is an emerged process from the TDD (Test Driven Development). TDD's cycle consists of 1) writing a failing unit tests, 2) writing code to make the tests pass, and 3) refactor code for simplifying and making sure the modification doesn't break the tests.

What are the 3 steps of test driven development?

Red, Green and Refactor is the three phase of Test Driven Development and this the sequence that get followed while writing code. When followed, this order of steps helps ensure that you have tests for the code you are writing and you are writing only the code that you have to test for.

What is testing in Android development?

Testing your app is an integral part of the app development process. By running tests against your app consistently, you can verify your app's correctness, functional behavior, and usability before you release it publicly. Testing also offers the following advantages: Rapid feedback on failures.


3 Answers

Yeah we can use JUnit for test driven development. To initiate with you can refer to following link : http://developer.android.com/tools/testing/testing_android.html#JUnit Following the documentation we can use the junit.framework to have unit testing done.

like image 20
Prati Avatar answered Sep 19 '22 02:09

Prati


I think you can completely rely on Robolectric for running your tests in JVM. You get to use JUnit4 for testing your POJOs and Robolectric provides you the support for testing the Android components.

I am also a beginner in TDD for Android Development. I find Robolectric really useful for test driving my code.

This video will tell you almost everything it provides you for unit testing the Android code.

UPDATE: With the Android studio support and the new Android ecosystem, now unit testing can be done as a first class practice. Refer http://developer.android.com/training/testing/unit-testing/local-unit-tests.html for more details.

There are couple of good approaches to test drive the android code. The most effective ones I have found so far is using MVVM(model-view-viewmodel) or MVP(model-view-presenter) approach, where the business logic as well as presentation logic is decoupled from the view and can be easily be unit tested.

like image 99
Jigish Chawda Avatar answered Sep 20 '22 02:09

Jigish Chawda


Here is a bit of explanation of the problem space: http://www.techwell.com/2013/04/applying-test-driven-development-android-development

The conclusion is you should use Robolectric. Unfortunately Robolectric is slow, and if you follow TDD even on a pragmatic level, you will end up with a couple of hundreds of test, that will run for couple of 10 seconds. And that is not you want to do with TDD. TDD test package should run at most in seconds.

My advise is:

  • Create wrapper classes around Android classes that simply call the Android class.
  • Write your app logic in pure Java.
  • Use Junit (or TestNG or whatever pleases you) to test you model/business logic
  • Use occasionally Robolectric for the wrapper classes (probably you won't have to use)
  • (You can write integration tests that use multiple classes, Robolectric, etc. but only run it on a separate Continous Integration server eg. hourly)

With this approach your logic will be more portable as well.

like image 31
Miklos Jakab Avatar answered Sep 19 '22 02:09

Miklos Jakab