From b9f4ca896498405ca1e4496ab80b7749fab3393f Mon Sep 17 00:00:00 2001
From: Rich Slogar See also
@@ -121,3 +122,104 @@ and <animated-selector>
, are supported.
Android Studio supports adding a separate test
module to your app so you can
+generate a test APK. This test
module resides at the same level as your app and
+contains: the tests and instrumentation used to run the test APK on an Android device; an
+Android Manifest.xml
file for test APK configuration settings; and, a
+build.gradle
file for build settings.
The test
module cannot contain a src/androidTest/
folder and does
+not support build variants. If you have different product flavors in your main application APK,
+create a different test module for each build variant.
To create a test APK module: + +
test
module consisting of the following directories and files:
+ ./test/
./test/build.gradle
./test/src/main/java/com/android/tests/basic/MainTest.java
./test/src/main/AndroidManifest.xml
build.gradle
file, add the required properties to the
+ android
block.
+ targetProjectPath ':<app name>'
specifies the main application APK
+ to test. targetVariant ':<buildType>'
specifies the target build type.Here is an example of build.gradle
file property settings:
+android { + compileSdkVersion 19 + buildToolsVersion = ‘21.1.3’ + + targetProjectPath ':app' + targetVariant 'debug' +} ++
Here is an example of <instrumentation>
settings in the manifest file:
+<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.tests.basic.test"> + + <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="16" /> + + <application> + >uses-library android:name="android.test.runner" /> + </application> + + <instrumentation android:name="android.test.InstrumentationTestRunner" + android:targetPackage="com.android.tests.basic" + android:handleProfiling="false" + android:functionalTest="false" + android:label="Tests for com.android.tests.basic"/> +</manifest< ++ +
Note: The targetPackage
in the instrumentation
+settings specifies the package of the test variant.
build.gradle
file for the tested app, include additional artifacts
+ that the test APK requires, such as the classes.jar
file, by adding the
+ {@code publishNonDefault} property to the {@code Android} block, and assigning that property
+ a value of {@code true}.
+ Here is an example of the build.gradle
file that includes additional
+ artifacts:
+android { + compileSdkVersion 19 + buildToolsVersion = ‘21.1.3’ + + publishNonDefault true +} ++ + + + +
In the {@code test} module in this example, the {@code build.gradle} file specifies the +properties for the project path and target build type variant.
+ + + + + +Note: By default, the test module's build variant uses the
+debug
build type. You can configure additional build types using the
+testBuildType
property in the defaultConfig
block in the main
+app's build.gradle
file.