How to place Android (instrumentation) test files outside of project directory?

戏子无情 提交于 2019-12-01 02:54:28
Sheikh

There are two ways:

  1. You can create independent Android project (which you can put at any folder outside the app project) for your instrumentation tests only using Android Studio 3.0. For this purpose I used:

    Android Studio 3.0 Beta 6

    Android Gradle Plugin: 'com.android.tools.build:gradle:3.0.0-beta6'

  2. You can create a separate module (which you can put at any folder outside the app project) for your instrumentation tests. For this you can use:

    With Android Studio 3.0.0

    With Android Gradle Plugin 3.0.0

    With Gradle Wrapper 4.2.1-all

    If you receive an error that the AndroidManifest of the instrumentation-test-module and the app-module cannot be merged, you might be limited to old Gradle versions which

    Tested with Android Studio 2.3.3 and 3.0.0

    The highest Android Gradle Plugin will be 2.2.3

    Gradle Wrapper 3.3-all (or 3.4.1 / 4.2.1)

    Note: This is broken for Android Gradle Plugin 2.3.0!

I created SAMPLE PROJECT to demonstrate structure of tests in both cases.


The build.gradle of the project/module has to use this plugin:

// A plugin used for test-only-modules
apply plugin: 'com.android.test'

This plugin uses TestExtension (link to its DSL). With TestExtension and 'com.android.test' plugin your gradle file will look like:

apply plugin: 'com.android.test'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 26

        // The package name of the test app
        testApplicationId 'com.example.android.testing.espresso.BasicSample.tests'
        // The Instrumentation test runner used to run tests.
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
    // Set the target app project. The module specified here should contain the production code
    // test should run against.
    targetProjectPath ':app'
}

dependencies {
    // Testing-only dependencies
    // Force usage of support annotations in the test app, since it is internally used by the runner module.
    compile 'junit:junit:4.12'
    compile 'com.android.support:support-annotations:25.4.0'
    compile 'com.android.support.test:runner:1.0.1'
    compile 'com.android.support.test:rules:1.0.1'
    compile 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Pay attention that here no "androidTestCompile" is supported!

Do not forget to create AndroidManifest.xml. It will look like:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.android.testing.espresso.BasicSample.tests">

<!-- Specify runner and target application package -->
<instrumentation
    android:name="android.support.test.runner.AndroidJUnitRunner"
    android:functionalTest="false"
    android:handleProfiling="false"
    android:label="Tests for com.example.android.testing.espresso.BasicSample"
    android:targetPackage="com.example.android.testing.espresso.BasicSample"/>

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

</manifest>

Pay attention that test source files are in "main" folder (not androidTest):

src->main->java->package.name.folders

Then you can link this test project to your application project in settings.gradle:

include ':module-androidTest'
project(':module-androidTest').projectDir = new File("../BasicSampleTests/test")

In Android Studio you must create "Android Instrumented Tests" run configuration. It will look like:

Now run your tests:

If you have build issues with product flavors then you should add publishNonDefault in build.gradle of your application:

android {
    ...
    defaultConfig {
       ...
    }
    publishNonDefault true
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!