开发者

Android Test Driven Development

I have considerable experience in making Android applications. For my new project, we have deci开发者_开发技巧ded 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


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.


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.


  • I use ActivityInstrumentationTestCase2 in the case of Activities for TDD (or BDD rather), and write normal unit tests for all logic. This also helps me separate logic from Activities.
  • Mobile applications by nature are UI centric. Therefore it does not make sense to mock out the UI, even if it makes the Unit test look like a Functional Test.
  • For adding Extras to intents, you can set a custom intent for the test, or do it for all tests by overriding setup.
  • Mocks give a lot of issues on Android, so I use stubs.

An example is given below. The Activity shows Hello World when you click a button -

public class HelloWorldActivityTest extends
        ActivityInstrumentationTestCase2<HelloWorldActivity> {

    private HelloWorld activity;

    public HelloWorldActivityTest() {
        super(HelloWorldActivityTest.class);
    }

    public void testShouldRenderGreetingOnButtonClick() {
        activity = this.getActivity();
        Button button = (Button) activity.findViewById(R.id.btn_greet);
        TouchUtils.clickView(this, button);
        assertEquals("Hello World!",
                ((TextView) activity.findViewById(android.R.id.greeting_text))
                        .getText());
    }

}

EDIT: Things have changed since I posted the answer. Mockito now has reasonably good support for Android. And for tests, we've moved from ActivityInstrumentationTestCase2 to Robolectric, just in order to tap the sheer speed of JVM in the development phase. The Android Testing Framework is great for Integration and Functional testing, just not for Unit Tests.


To apply TDD for android, Android Testing Codelab will be very helpful to you. Code lab shows a use of the testing tool and how you can apply TDD for android.I tried it and it was very helpful to me.

Bonus: Check Clean Architecture


Android Testing Support Library provides an extensive framework for testing Android apps. This library provides a set of APIs that allow you to quickly build and run test code for your apps, including JUnit 4 and functional user interface (UI) tests. You can run tests created using these APIs from the Android Studio IDE or from the command line.

Read more about:

  • Espresso
  • AndroidJUnitRunner
  • JUnit4 Rules
  • UI Automator

Thank you :)


we have

https://developer.android.com/training/testing/start/index.html

and can able to test local(Runs on JVM) and instrumental test (Runs on Device or Emulator )

For this we need to add

Android Testing Support Library

The Android SDK includes two tools for functional-level app testing

Monkey and monkeyrunner

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜