Android Test Support Library Introduction

Source: Internet
Author: User
Tags android sdk manager

Test Support Library

The Android Test Support library provides a number of frameworks for testing Android apps. The library provides a set of APIs to quickly build and run test code, including JUNIT4 and functional user Interface (UI) testing. This can be done from the Android Studio IDE or from the command line.

Android's Test support library is available through the Android SDK manager.

Test Support Library Features

Androidjunitrunner: Compatible with JUnit 4 test runner. Espresso:ui test framework for functional UI testing in a single application. UI Automator:ui test framework for functional UI testing and installation applications across applications.

Androidjunitrunner

The Androidjunitrunner class is a JUnit test runner that lets you perform JUnit3 or JUNIT4-style test classes on your Android device, compatible with Espresso and UI Automator test frameworks. The test runner loads the test packages and applications, runs the tests, and reports the test results. This class supersedes the Instrumentationtestrunner class (only JUnit 3 is supported).

The main features of this runner:

    • JUnit Support

    • Get instrumentation information

    • Test filter

    • Test shards

Required Android2.2 (API 8) or higher.

Test Runner is compatible with JUnit3 and JUNIT4 (up to JUnit4.10) tests. Confusing JUnit 3 and JUNIT4 test code in the same package can cause unpredictable results. The instrumented JUnit 4 test class runs on a device or emulator and must be preceded by a @RunWith(AndroidJUnit4.class) comment.

For example, test the add operation in the Calculatoractivity class:

ImportANDROID.SUPPORT.TEST.RUNNER.ANDROIDJUNIT4;ImportAndroid.support.test.runner.AndroidJUnitRunner;ImportAndroid.test.ActivityInstrumentationTestCase2; the @RunWith (ANDROIDJUNIT4.class) Public classcalculatorinstrumentationtestextendsActivityinstrumentationtestcase2<calculatoractivity>{@Before Public voidSetUp ()throwsException {Super. SetUp (); //injecting the instrumentation instance is required//For your test to run with Androidjunitrunner.injectinstrumentation (Instrumentationregistry.getinstrumentation ()); Mactivity=getactivity (); } @Test Public voidtypeoperandsandperformaddoperation () {//Call the Calculatoractivity Add () method and pass in some operand values, then//Check the expected value is returned.} @After Public voidTearDown ()throwsException {Super. TearDown (); }}

The Instrumentationregistry class can access information about the test run. The class includes the instrumentation object, the target application context object, the test application context object, and the command-line arguments passed to the test.

JUnit 4.x tests can use annotation to configure test runs and support androidannotation:

@RequiresDevice: Run on a physical device.

@SdkSupress: Limits the minimum SDK version. For example @SDKSupress(minSdkVersion=18) .
@SmallTest, @MediumTest and @largetest: Test ratings.

A single test suite can be fragmented, and the same shard of the same instrumentation can be used as a group. Each slice has an index number. When you run the test, use the-e numshards option to specify the number of slices and the-e shardindex option to specify the slices to run.

For example, split into 10 fragments and only perform a second piece of testing, use the following command:

ADB shell am instrument-w-E numshards 10-e Shardindex 2
Espresso

Espresso provides a set of APIs to build UI tests to test user processes. These APIs allow you to write automated UI tests that are concise and reliable to run. Espresso is ideal for white box automated testing, and the test code takes advantage of the code details of the application being tested.

Key features of the espresso:

    • View matching: A flexible API for viewing and adapting to target applications.

    • Action API: A set of Extended Action API Automation UI interactions.

    • UI thread synchronization (UI thread synchronization) to improve the reliability of your tests.

Requires Android2.2 (API 8) or higher.

The Espresso.onview () method can access and interact with the UI components of the target application. The method takes a Matcher parameter and searches the view layer to locate the view instance. The positioning method can be based on the class name, content description, r.id, displayed text. Like what

Onview (Withid (R.id.my_button));


If the search succeeds, the Onview () method returns a reference to the view, which can perform user actions and assertions.

Adapterview is dynamically generated by the child view. If the target view is in Adapterview (ListView or GridView), the Onview () method may not work because only a portion of it may be loaded, espresso.ondata ().

Viewactions can perform a view click (view clicks), swipe (swipes), key or button (key and button press), text input (Typing text), open links (Opening a link).

// Type text into a EditText view, then close the soft keyboard Onview (Withid (r.id.edittextuserinput))    . Perform (TypeText (string_to_be_typed), Closesoftkeyboard ()); // Press the button to submit the textchange Onview (Withid (R.ID.CHANGETEXTBT)). Perform (click ());

Testing for random failures on Android devices due to time issues. Previously resolved by sleep and timeout processing. The espresso test framework handles the synchronization between the instrumentation and UI threads, which solves these problems well.

API Reference: Developer.android.com/reference/android/support/test/package-summary.html
Test reference: Http://developer.android.com/training/testing/ui-testing/espresso-testing.html

UI Automator

UI Automator provides a set of APIs to build tests based on interactive UIs. The API allows you to perform actions such as opening the Settings menu, ideal for black box automation testing, and testing code that does not depend on the application's internal implementation.

Key Features:

    • UI Automator Viewer: Checks the layout hierarchy.

    • API to get Device status information and perform operations.

    • API across application testing.

Requires Android4.3 (API level 18) or higher.

Uiautomatorviewer provides a convenient graphical user interface for scanning and analyzing UI components that are currently displayed on Android devices. You can use this tool to check the layout hierarchy and view the UI components.

The Uidevice class can access the device and operate it. You can call its methods to access device properties, such as the current orientation or display dimensions. The Uidevice class also allows you to perform operations such as rotating the device, pressing the D-pad button, pressing back, Home, menu, etc., opening the notification tree bar, the current window, and so on. For example, press the Home key: Uidevice.presshome ().

More app-related api:uicollection enumerate the UI elements of the container to count, or by text (or attributes, etc.) to set the seat element; UIObject represents a UI element that is visible on the device; Uiscrollable: Provides lookup support for scrollable UI containers; Uiselector: Querying one or more UI elements; Configurator: Sets the parameters. Like what:

//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 (NewUiselector (). Description ("Apps"));//Perform A click on the button to bring up the launcherAllappsbutton.clickandwaitfornewwindow ();

API Reference: Http://developer.android.com/reference/android/support/test/package-summary.html
Example: http://developer.android.com/training/testing/ui-testing/uiautomator-testing.html

Https://pypi.python.org/pypi/uiautomator uses Python to encapsulate the uiautomator, which is easier to operate and recommend.

Test Support Library Installation
Android {    Defaultconfig {        testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner"    }}

  • Launch Android SDK manager.

  • In the SDK Manager window, scroll to the end of the package list, locate the other folder, and, if necessary, expand to display its contents.

  • Select Android Support Repository.

  • Click Install Packages.

    After downloading, install the support library file to your existing Android SDK directory. The library file is located in the subdirectory of your SDK: <sdk>/extras/android/m2repository.

    The Android Test Support Library is located in the Android.support.test package.

    These dependencies are added in Gradle using: Build.gradle files:

    dependencies {  androidtestcompile ' com.android.support.test:runner:0.4 '  //Set This dependency to use JUnit 4 Rules  androidtestcompile ' com.android.support.test:rules:0.4 '  //Set This dependency to build and run Espresso Tests  androidtestcompile ' com.android.support.test.espresso:espresso-core:2.2.1 '  //Set this dependency to Build and run UI Automator tests  androidtestcompile ' com.android.support.test.uiautomator:uiautomator-v18:2.1.2 ' }


    Set Androidjunitrunner as the default test:
    It is highly recommended that you use the Android test support library together with the Android Studio IDE. Android Studio supports test development, such as:

    A flexible build system based on Gradle that supports the dependency management of test code.
    A single project contains the application source code and the test code.
    Supports deployment and run testing of virtual or physical devices, supporting command-line or graphical user interfaces

    For more information see: http://developer.android.com/sdk/index.html

Resources
    • Testing Support Library

Android Test Support Library Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.