1. 程式人生 > >Android application testing with the Android test framework

Android application testing with the Android test framework

4.1. Android test projects

Android organizes tests into separate Android test projects. Instead of Android components, an Android test application contains one or more test classes.

Note

The application which is tested is typically called the application under test.

The Android development tools (ADT) provide support for the creation of Android test projects. via a project creation wizard. This wizard can be reached under File

New Other...Android Android Test Project.

Note

It is good practice to use the name of the project under test and add Test or .test to it. We use the .test notation to have the project name the same as the package name.

The project creation wizard adds the project which should be tested as dependency to the test project. It also create a version of the AndroidManifest.xml

file which specifies that the android.test.runner test library should be used and it specifies an instrumentation.

A test project also specifies the package of the application to test in the AndroidManifest.xml file the under android:targetPackage attribute. The following listing shows an example AndroidManifest.xml

for a test project.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.vogella.android.test.target.test"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <instrumentation 
        android:targetPackage="de.vogella.android.test.target" 
        android:name="android.test.InstrumentationTestRunner" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">

    <uses-library android:name="android.test.runner" />
    </application>
</manifest>