testing tools

9
Android Testing Tools Testing is a critical software development activity because it helps you improve the quality of your apps, ensure better user satisfaction, and reduce overall development time spent on fixing defects. The following sections describe tools that help you test your mobile apps for the Android platform. Android Testing Support Library 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. The Android Testing Support Library includes the following test automation tools: AndroidJUnitRunner : JUnit 4-compatible test runner for Android Espresso : UI testing framework; suitable for functional UI testing within an app UI Automator : UI testing framework; suitable for cross-app functional UI testing across system and installed apps Monkey This tool runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey tool to stress-test applications that you are developing, in a random yet repeatable manner. monkeyrunner This testing system provides an API for writing programs that control an Android device or emulator from outside of Android code.

Upload: vkm2013

Post on 28-Sep-2015

12 views

Category:

Documents


7 download

DESCRIPTION

The Android Testing Support Library provides an extensive framework for testing Android apps.

TRANSCRIPT

Android Testing ToolsTesting is a critical software development activity because it helps you improve the quality of your apps, ensure better user satisfaction, and reduce overall development time spent on fixing defects.The following sections describe tools that help you test your mobile apps for the Android platform.Android Testing Support LibraryThis 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. The Android Testing Support Library includes the following test automation tools: AndroidJUnitRunner: JUnit 4-compatible test runner for Android Espresso: UI testing framework; suitable for functional UI testing within an app UI Automator: UI testing framework; suitable for cross-app functional UI testing across system and installed appsMonkeyThis tool runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey tool to stress-test applications that you are developing, in a random yet repeatable manner.monkeyrunnerThis testing system provides an API for writing programs that control an Android device or emulator from outside of Android code.

Testing Support LibraryThe 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 theAndroid Studio IDEor from the command line.The Android Testing Support library is available through the Android SDK Manager. For more information, seeTesting Support Library SetupThis page provides information about what tools are provided in the Android Testing Support Library, how to use them in your testing environment, and information about library releases.Testing Support Library Features

The Android Testing Support Library includes the following test automation tools: AndroidJUnitRunner: JUnit 4-compatible test runner for Android Espresso: UI testing framework; suitable for functional UI testing within an app UI Automator: UI testing framework; suitable for cross-app functional UI testing across system and installed appsAndroidJUnitRunnerTheAndroidJUnitRunnerclass is aJUnittest runner that lets you run JUnit 3 or JUnit 4-style test classes on Android devices, including those using theEspressoandUI Automatortesting frameworks. The test runner handles loading your test package and the app under test to a device, running your tests, and reporting test results. This class replaces theInstrumentationTestRunnerclass, which only supports JUnit 3 tests.The key features of this test runner include: JUnit support Access to instrumentation information Test filtering Test shardingRequires Android 2.2 (API level 8) or higher.JUnit supportThe test runner is compatible with your JUnit 3 and JUnit 4 (up to JUnit 4.10) tests. However, you should avoid mixing JUnit 3 and and JUnit 4 test code in the same package, as this might cause unexpected results. If you are creating an instrumented JUnit 4 test class to run on a device or emulator, your test class must be prefixed with the@RunWith(AndroidJUnit4.class)annotation.Access to instrumentation informationYou can use theInstrumentationRegistryclass to access information related to your test run. This class includes theInstrumentationobject, target appContextobject, test appContextobject, and the command line arguments passed into your test. This data is useful when you are writing tests using the UI Automator framework or when writing tests that have dependencies on theInstrumentationorContextobjects.Test filteringIn your JUnit 4.x tests, you can use annotations to configure the test run. This feature minimizes the need to add boilerplate and conditional code in your tests. In addition to the standard annotations supported by JUnit 4, the test runner also supports Android-specific annotations, including: @RequiresDevice: Specifies that the test should run only on physical devices, not on emulators. @SdkSupress: Suppresses the test from running on a lower Android API level than the given level. For example, to suppress tests on all API levels lower than 18 from running, use the annotation@SDKSupress(minSdkVersion=18). @SmallTest,@MediumTest, and@LargeTest: Classify how long a test should take to run, and consequently, how frequently you can run the test.Test shardingThe test runner supports splitting a single test suite into multipleshards, so you can easily run tests belonging to the same shard together as a group, under the sameInstrumentationinstance. Each shard is identified by an index number. When running tests, use the-e numShardsoption to specify the number of separate shards to create and the-e shardIndexoption to specify which shard to run.For example, to split the test suite into 10 shards and run only the tests grouped in the second shard, use the following command:adb shell am instrument -w -e numShards 10 -e shardIndex 2To learn more about using this test runner, see theAPI reference.EspressoThe Espresso testing framework provides a set of APIs to build UI tests to test user flows within an app. These APIs let you write automated UI tests that are concise and that run reliably. Espresso is well-suited for writingwhite box-style automated tests, where the test code utilizes implementation code details from the app under test.The key features of the Espresso testing framework include: Flexible APIs for view and adapter matching in target apps. For more information, seeView matching. An extensive set of action APIs to automate UI interactions. For more information, seeAction APIs. UI thread synchronization to improve test reliability. For more information, seeUI thread synchronization.Requires Android 2.2 (API level 8) or higher.View matchingTheEspresso.onView()method lets you access a UI component in the target app and interact with it. The method accepts aMatcherargument and searches the view hierarchy to locate a correspondingViewinstance that meets some given criteria. You can refine searches by specifying such criteria as: The class name of the view The content description of the view TheR.idof the view Text displayed in the viewFor example, to target a button that has the ID value ofmy_button, you can specify a matcher like this:onView(withId(R.id.my_button));If the search is successful, theonView()method returns a reference which lets you perform user actions and test assertions against the target view.Adapter matchingIn anAdapterViewlayout, the layout is dynamically populated with children views at runtime. If the target view is inside a layout subclassed fromAdapterView(such as aListVieworGridView), theonView()method might not work because only a subset of the layouts views may be loaded in the current view hierarchy.Instead, use theEspresso.onData()method to access a target view element. TheEspresso.onData()method returns a reference which lets you perform user actions and test assertions against the elements in anAdapterView.Action APIsTypically, you test an app by performing some user interactions against the apps user interface. You can easily automate these actions in your test by using theViewActionsAPI. You can perform such UI interactions as: View clicks Swipes Key and button presses Typing text Opening a linkFor example, to simulate entering a string value and pressing a button to submit the value, you can write an automated test script like this. TheViewInteraction.perform()andDataInteraction.perform()methods take one or moreViewActionarguments and run the actions in the order provided.// Type text into an EditText view, then close the soft keyboardonView(withId(R.id.editTextUserInput)) .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());

// Press the button to submit the text changeonView(withId(R.id.changeTextBt)).perform(click());UI thread synchronizationTests on Android devices can fail randomly because of timing issues. This testing issue is referred to astest flakiness. Prior to Espresso, the workaround was to insert a sufficiently long sleep or timeout period into a test or to add code to keep retrying the failing operation. The Espresso testing framework handles synchronization between theInstrumentationand the UI thread; this removes the need for the previous timing workarounds and ensures that your test actions and assertions run more reliably.To learn more about using Espresso, see theAPI referenceandTesting UI for a Single Apptraining.UI AutomatorThe UI Automator testing framework provides a set of APIs to build UI tests that perform interactions on user apps and system apps. The UI Automator APIs allows you to perform operations such as opening the Settings menu or the app launcher in a test device. The UI Automator testing framework is well-suited for writingblack box-style automated tests, where the test code does not rely on internal implementation details of the target app.The key features of the UI Automator testing framework include: A viewer to inspect layout hierarchy. For more information, seeUI Automator Viewer. An API to retrieve state information and perform operations on the target device. For more information, seeAccess to device state. APIs that support cross-app UI testing. For more information, seeUI Automator APIs.Requires Android 4.3 (API level 18) or higher.UI Automator ViewerTheuiautomatorviewertool provides a convenient GUI to scan and analyze the UI components currently displayed on an Android device. You can use this tool to inspect the layout hierarchy and view the properties of UI components that are visible on the foreground of the device. This information lets you create more fine-grained tests using UI Automator, for example by creating a UI selector that matches a specific visible property.Theuiautomatorviewertool is located in the/tools/directory.Access to device stateThe UI Automator testing framework provides aUiDeviceclass to access and perform operations on the device on which the target app is running. You can call its methods to access device properties such as current orientation or display size. TheUiDeviceclass also let you perform actions such as: Change the device rotation Press a D-pad button Press the Back, Home, or Menu buttons Open the notification shade Take a screenshot of the current windowFor example, to simulate a Home button press, call theUiDevice.pressHome()method.UI Automator APIsThe UI Automator APIs allow you to write robust tests without needing to know about the implementation details of the app that you are targeting. You can use these APIs to capture and manipulate UI components across multiple apps: UiCollection: Enumerates a container's UI elements for the purpose of counting, or targeting sub-elements by their visible text or content-description property. UiObject: Represents a UI element that is visible on the device. UiScrollable: Provides support for searching for items in a scrollable UI container. UiSelector: Represents a query for one or more target UI elements on a device. Configurator: Allows you to set key parameters for running UI Automator tests.For example, the following code shows how you can write a test script that brings up the default app launcher in the device:// Initialize UiDevice instancemDevice = UiDevice.getInstance(getInstrumentation());

// Perform a short press on the HOME buttonmDevice().pressHome();

// Bring up the default launcher by searching for// a UI component that matches the content-description for the launcher buttonUiObject allAppsButton = mDevice .findObject(new UiSelector().description("Apps"));

// Perform a click on the button to bring up the launcherallAppsButton.clickAndWaitForNewWindow();To learn more about using UI Automator, see theAPI referenceandTesting UI for Multiple Appstraining.