Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Test Driven Development

I have considerable experience in making Android applications. For my new project, we have decided to do Test Driven Development (TDD). I have been getting my hands wet on Robotium for User Scenario Testing, and it works fine and looks easy too.

For unit testing, I tried to mock Context using (MockContext Android Class) but I am unable to do so. I went through this blog http://sites.google.com/site/androiddevtesting/ and through this http://sdudzin.blogspot.com/2011/01/easy-unit-testing-for-android.html , which suggests that mocking in Android apps is still very limited and hard, and have suggested to use PowerMock, jMockit, JeasyTest, or Roboelectric (in combination with Mockito and Maven) and even RoboGuice.

I would like to get any suggestions from you guys on which unit testing framework in your opinion is the best for testing Android applications. (particularly testing Android classes, possibly giving mock Contexts and other mocking features so that I can make my test cases as independent as possible). Any suggestions or pointers would be helpful . Thanks

like image 523
Muhammad Shahab Avatar asked Jun 07 '11 06:06

Muhammad Shahab


People also ask

What is test-driven development in Android?

Test-driven development, or TDD, is a testing method where you first convert the feature requirements to tests and then write code to make the tests pass.

What is TDD and BDD Android?

TDD is a development practice while BDD is a team methodology. In TDD, the developers write the tests while in BDD the automated specifications are created by users or testers (with developers wiring them to the code under test.)

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.

Is Jasmine A TDD?

Jasmine is a JavaScript testing framework that supports a software development practice called Behaviour-Driven Development, or BDD for short. It's a specific flavour of Test-Driven Development ( TDD ).


2 Answers

For off-device testing, look at Robolectric

For on-device testing, look at Borachio

Bottom line: it's still very, very difficult to do well. Things are improving (the situation is dramatically better today than it was 6 months ago) but Android is comfortably the most test-hostile environment I've ever written programs for.

like image 89
Paul Butcher Avatar answered Sep 28 '22 02:09

Paul Butcher


To do TDD in Android, I personally use all of the following:

  • assertj-android: assertions for Android
  • Mockito: Mocking Framework
  • Robolectric: Unit testing framework that runs without the need of Android emulator
  • Robotium: UI tests (Needs emulator or device to run)

Also: Using dependency injection libraries such as Dagger or Roboguice will greatly simplify your unit/integration tests. To run tests on multiple devices, consider using Spoon.

like image 27
Nima G Avatar answered Sep 28 '22 02:09

Nima G