Instrumentation Testing

This document describes how to use the Instrumentation Framework to write test cases. Instrumentation testing allows you to verify a particular feature or behavior with an automated JUnit TestCase. You can launch activities and providers within an application, send key events, and make assertions about various UI elements.

You should have a working knowledge of the following:

  • Android Application Framework
  • Using adb, am and various logging functionality
  • A brief understanding of the application of interest, that is, the names of the classes which handle the intents etc.
  • JUnit testing.

Each Android application runs in its own process. Instrumentation kills the application process and restarts the process with Instrumentation. Instrumentation gives a handle to the application context used to poke around the application to validate test assertions, allowing you to write test cases to test applications at a much lower level than UI screen shot tests. Note that Instrumentation cannot catch UI bugs.

Instrumentation Framework

Classes

The following classes help glue together Instrumentation with JUnit testing.

ClassDescription
InstrumentationTestCase

This extends the standard JUnit TestCase and offers access to an Instrumentation class. Write tests inside your instrumentation class any way you see fit. For example, your test might launch activities and send key events. For this to work properly, the instrumentation needs to be injected into the test case.

InstrumentationTestRunnerThe instrumentation test runner is an instrumentation that runs instrumentation test cases and injects itself into each test case. Instrumentation test cases need to be grouped together with an instrumentation test runner with the appropriate target package.
InstrumentationTestSuiteThe instrumentation test suite is a simple extension of the standard JUnit TestSuite that keeps a member Instrumentation variable on hand to inject into each TestCase before running them. It is used by InstrumentationTestRunner.

Three additional base classes extend InstrumentationTestCase to allow you to test Activity and Provider classes:

ClassDescription
ActivityTestCase

This class can be used to write tests for a specific activity. An activity is launched in its setUp() method and finished with tearDown. If you write a test case that extends ActivityTestCase, you can write tests that access the activity using getActivity() and assume it has been set up properly.

ServiceTestCaseThis test case provides a framework in which you can test Service classes in a controlled environment. It provides basic support for the lifecycle of a Service, and hooks by which you can inject various dependencies and control the environment in which your Service is tested.
SingleLaunchActivityTestCaseThis class is similar to ActivityTestCase except that the activity is launched once per class instead of every time the test case calls setup.
ProviderTestCaseThis class is similar to ActivityTestCase except that it will setup, tear down, and provide access to the Provider of your choice.
Understanding the am Command

The am command is a command-line interface to the ActivityManager (see http://code.google.com/android/reference/android/app/ActivityManager.html for details). am is used to start and instrument activities using the adb shell command, as shown in the snippet below:

> adb shell am
usage: am [start|instrument]
       am start [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
                [-c <CATEGORY> [-c <CATEGORY>] ...]
                [-e <EXTRA_KEY> <EXTRA_VALUE> [-e <EXTRA_KEY> <EXTRA_VALUE> ...]
                [-n <COMPONENT>] [-D] [<URI>]
       am instrument [-e <ARG_NAME> <ARG_VALUE>] [-p <PROF_FILE>]
                [-w] <COMPONENT>
For example, to start the Contacts application you can use
> adb shell am start -n com.google.android.contacts/.ContactsActivity

Platform Test Suites

This section provides an overview for various unit and functional test cases that can be executed through the instrumentation framework.

Framework Tests

Framework test cases test the Android application framework or specific Android application functionality that requires an Android runtime context. These tests can be found in //device/tests and //device/apps/AndroidTests.

Core Library

Core library test cases test the Android library functionality that does not require an Android runtime context. These tests are split into Android library (android.* package space) tests at //device/java/tests and Java library (java.*, javax.*, etc. packages) tests at //device/dalvik/libcore/.../tests.

Running Tests

Each instrumentation test case is similar to an Android application with the distinction that it starts another application. For example, have a look in the tests/Contacts directory.

  • There should be a Makefile and an Android Manifest file.
  • Tests are located in tests/Contacts/src/com/google/android/contactstests.
  • The Instrumentation Test Runner is located at tests/Contacts/src/com/google/android/contactstests/functional/ContactsInstrumentationTestRunner.java.

Suppose you have a makefile with Contactstests as the target.

  • make Contactstests: Compiles the test cases.
  • adb install Contactstests.apk: Installs the apk on the device.
  • Use the adb shell am command to run them.

To run your tests, use the am instrument command with your InstrumentationTestRunner as its argument. Results are printed as a result of the instrumentation. For example, the following snippet displays the output after running the framework tests with one test failing (note the unusual syntax caused by how instrumentations are run via am):

$ adb shell am instrument -w com.google.android.frameworktest/.tests.FrameworkInstrumentationTestRunner
INSTRUMENTATION_RESULT: test results:=.......F.......
Time: 6.837
There was 1 failure:
1) testSetUpConditions(com.google.android.frameworktest.tests.focus.RequestFocusTest)junit.framework.AssertionFailedError: requestFocus() should work from onCreate.
        at com.google.android.frameworktest.tests.focus.RequestFocusTest.testSetUpConditions(RequestFocusTest.java:66)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at android.test.InstrumentationTestSuite.runTest(InstrumentationTestSuite.java:73)
        at android.test.InstrumentationTestSuite.runTest(InstrumentationTestSuite.java:73)
        at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:151)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1088)

FAILURES!!!
Tests run: 14,  Failures: 1,  Errors: 0

<RETURN> to continue

INSTRUMENTATION_CODE: -1
$ 
All Tests with Default TestRunner behavior

If no class or package is passed in to run, InstrumentationTestRunner will automatically find and run all tests under the package of the test application (as defined by the android:targetPackage attribute of the instrumentation defined in its manifest file).

 
$ adb shell am instrument -w \
  com.android.samples.tests/android.test.InstrumentationTestRunner
 
INSTRUMENTATION_RESULT: Test results for InstrumentationTestRunner=..........
Time: 2.317
 
OK (10 tests)
 
 
INSTRUMENTATION_CODE: -1
Running all Tests Under Single Package

If you have many tests under one package, use the -e package <packagename> option to run all tests under that package without having to manually create a test suite.

 
$ adb shell am instrument -w \
  -e package com.android.samples.view \
  com.android.samples.tests/android.test.InstrumentationTestRunner
INSTRUMENTATION_RESULT: Test results for InstrumentationTestRunner=........
Time: 1.587
 
OK (8 tests)
Running a Single Test Suite

If you prefer to explicitly state which tests comprise all of your tests, you can define a test suite and run that directly. By convention, all test packages in your system should have at least one suite called AllTests (see AllTests.java). To run all of the tests using the AllTests suite for the api demos test app:

 
$ adb shell am instrument -w \
  -e class com.android.samples.AllTests \
  com.android.samples.tests/android.test.InstrumentationTestRunner
 
INSTRUMENTATION_RESULT: Test results for AllTests=..........
Time: 2.286
 
OK (10 tests)
 
 
INSTRUMENTATION_CODE: -1
A Single Test Case
 
$ adb shell am instrument -w \
  -e class com.android.samples.view.Focus2ActivityTest \
  com.android.samples.tests/android.test.InstrumentationTestRunner
 
INSTRUMENTATION_RESULT: Test results for Focus2ActivityTest=....
Time: 1.359
 
OK (4 tests)
 
 
INSTRUMENTATION_CODE: -1
A Single Test
 
$ adb shell am instrument -w \
  -e class com.android.samples.view.Focus2ActivityTest#testGoingLeftFromRightButtonGoesToCenter \
  com.android.samples.tests/android.test.InstrumentationTestRunner
 
INSTRUMENTATION_RESULT: Test results for Focus2ActivityTest=.
Time: 0.51
 
OK (1 test)
 
 
INSTRUMENTATION_CODE: -1
Attaching a debugger to your test

In order to debug your test code, instruct the controller to stop and wait for the debugger by adding -e debug true to yourcommand line. This causes the test runner to stop and wait for the debugger just before calling your setUp() method. For example,

 
$ adb shell am instrument -w \
  -e debug true \
  com.android.samples.tests/android.test.InstrumentationTestRunner

Writing Tests

When writing tests, refer to the ApiDemos tests as models (located at //device/samples/ApiDemos). This section provides an overview of the test structure with ApiDemos.

Location of Files

Test packages should use the following structure and include Android.mk, AndroidManifest.xml, AllTests.java, and a src directory that mirrors the src directory of the tested application.

Files are located within a tests directory found in the root directory:

 
$ find samples/ApiDemos/tests
samples/ApiDemos/tests
samples/ApiDemos/tests/Android.mk
samples/ApiDemos/tests/AndroidManifest.xml
samples/ApiDemos/tests/src
samples/ApiDemos/tests/src/com
samples/ApiDemos/tests/src/com/google
samples/ApiDemos/tests/src/com/google/android
samples/ApiDemos/tests/src/com/google/android/samples
samples/ApiDemos/tests/src/com/google/android/samples/AllTests.java
samples/ApiDemos/tests/src/com/google/android/samples/ApiDemosTest.java
samples/ApiDemos/tests/src/com/google/android/samples/os
samples/ApiDemos/tests/src/com/google/android/samples/os/MorseCodeConverterTest.java
samples/ApiDemos/tests/src/com/google/android/samples/view
samples/ApiDemos/tests/src/com/google/android/samples/view/Focus2ActivityTest.java
samples/ApiDemos/tests/src/com/google/android/samples/view/Focus2AndroidTest.java
Contents of makefile

The contents of the makefile are similar to a normal application with the addition of a LOCAL_INSTRUMENTATION_FOR declaration.

 
# Add appropriate copyright banner here
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
 
# We only want this apk build for tests.
LOCAL_MODULE_TAGS := tests
 
# Include all test java files.
LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
# Notice that we don't have to include the src files of ApiDemos because, by
# running the tests using an instrumentation targeting ApiDemos, we
# automatically get all of its classes loaded into our environment.
 
LOCAL_PACKAGE_NAME := ApiDemosTests
 
LOCAL_INSTRUMENTATION_FOR := ApiDemos
 
include $(BUILD_PACKAGE)
Content of Manifest

Use the following example to create an AndroidManifest.xml file that declares the instrumentation. Specify that the framework supplied Instrumentation TestRunner targest the package of your application, allowing the tests that are run with the instrumentation to get access to all of the classes of your application without having to build the source into the test app. The name of the test application is typically the same as your target application with .tests appended.

 
# Add appropriate copyright banner here
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.samples.tests">
 
    <uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
 
    <!--
    This declares that this app uses the instrumentation test runner targeting
    the package of com.android.samples.  To run the tests use the command:
    "adb shell am instrument -w com.android.samples.tests/android.test.InstrumentationTestRunner"
    -->
    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="com.android.samples"
                     android:label="Tests for Api Demos."/>
 
</manifest>

 

The following snippet will prefix the /android.test.InstrumentationTestRunner when running tests from the command line:

 
$ adb shell am instrument -w \
  com.android.samples.tests/android.test.InstrumentationTestRunner
New Instrumentation TestRunner

Create a class that derives from this class. You must override two abstract methods; one that returns the class loader of the target package, and another that defines all of the tests within the package. For example, the snippet below displays the test runner for the framework tests.

public class FrameworkInstrumentationTestRunner extends InstrumentationTestRunner {
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值