Android Test Driven Development

后端 未结 6 1828
轻奢々
轻奢々 2021-01-29 22:32

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 Robot

相关标签:
6条回答
  • 2021-01-29 23:01

    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.

    0 讨论(0)
  • 2021-01-29 23:07

    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 讨论(0)
  • 2021-01-29 23:13
    • 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.

    0 讨论(0)
  • 2021-01-29 23:14

    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 :)

    0 讨论(0)
  • 2021-01-29 23:18

    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.

    0 讨论(0)
  • 2021-01-29 23:22

    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

    0 讨论(0)
提交回复
热议问题