Android Test Driven Development

2019-03-07 17:07发布

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

6条回答
我命由我不由天
2楼-- · 2019-03-07 17:26

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

查看更多
孤傲高冷的网名
3楼-- · 2019-03-07 17:35
  • 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.

查看更多
相关推荐>>
4楼-- · 2019-03-07 17:37

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

查看更多
来,给爷笑一个
5楼-- · 2019-03-07 17:44

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.

查看更多
趁早两清
6楼-- · 2019-03-07 17:46

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:

Thank you :)

查看更多
手持菜刀,她持情操
7楼-- · 2019-03-07 17:47

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

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.

查看更多
登录 后发表回答