如何将Android(仪器)测试文件放置在项目目录之外? [英] How to place Android (instrumentation) test files outside of project directory?

查看:83
本文介绍了如何将Android(仪器)测试文件放置在项目目录之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android项目(由Cordova生成),我想向其添加(仪器)测试.它只有一个要测试的MainActivity.java.

I have an Android project (generated by Cordova) that I want to add (instrumentation) tests to. It has only one MainActivity.java that should be tested.

通常,这意味着向build.gradle添加一些依赖项,并使用MainActivityTest类和一些测试方法创建文件/src/androidTest/java/org/example/package/MainActivityTest.java. (在Android Studio中,我什至可以通过运行->记录Espresso测试"来生成这些代码-如此简单且有效).

Normally this means adding some dependencies to build.gradle and creating a file /src/androidTest/java/org/example/package/MainActivityTest.java with an MainActivityTest class and some test methods. (In Android Studio I can even generate those by using Run -> "Record Espresso Test" - so really simple and works).

不幸的是,我现在要求这些测试文件确实位于项目目录的外部.在现有的Cordova项目上应该只进行最小的更改(因为它是作为生成工件进行重新生成和处理的). MainActivity.java最好与Android项目位于/android的同一个根文件夹中.

Unfortunately, I now have the requirement that those test files do actually live outside the project directory. There should be only minimal changes on the existing Cordova project (as it is regenerated and handled as a build artifact). The MainActivity.java would best be in the same root folder where the Android project is in /android.

我该如何实现?

可以将build.gradle更改添加到实际的build.gradle已经包含的build-extras.gradle文件中,因此需要注意这一点.

The build.gradle changes can be added to a build-extras.gradle file that the real build.gradle already includes, so this is taken care of.

但是我不知道如何将MainActivityTest.java放置在项目文件夹结构之外,并且仍然能够在项目内部运行它.

But I have no idea how to place the MainActivityTest.java outside of the project folder structure and still be able run it inside the project.

对于iOS,您可以使用绝对路径将外部文件链接到项目中.这样的东西在这里也很完美.

For iOS you can link external files into the project with absolute paths. Something like that would be perfect here as well.

我查看了sourceSets,但是我不确定如何将其集成到已经具有此非默认值的Cordova Android项目中(我认为吗?)sourceSets:

I looked at sourceSets but I am not sure how to integrate this in the Cordova Android project which already has this non default (I think?) sourceSets:

android {
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }
    }
    ...
}     

推荐答案

有两种方法:

  1. 您只能使用Android Studio 3.0创建独立的 Android项目(您可以将其放置在应用程序项目之外的任何文件夹中)进行仪器测试.为此,我使用了:

  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 Studio 3.0 Beta 6

Android Gradle插件:"com.android.tools.build:gradle:3.0.0-beta6"

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

  • 您可以创建一个单独的模块(您可以将其放置在应用程序项目之外的任何文件夹中)进行仪器测试.为此,您可以使用:

  • 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:

    使用Android Studio 3.0.0

    With Android Studio 3.0.0

    使用Android Gradle插件3.0.0

    With Android Gradle Plugin 3.0.0

    使用Gradle Wrapper 4.2.1-all

    With Gradle Wrapper 4.2.1-all

    如果您收到错误,该错误表示无法将Instrumentation-test-module的AndroidManifest和app-module合并,可能仅限于

    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

    在Android Studio 2.3.3和3.0.0上进行了测试

    Tested with Android Studio 2.3.3 and 3.0.0

    最高的Android Gradle插件为2.2.3

    The highest Android Gradle Plugin will be 2.2.3

    Gradle Wrapper 3.3-all(或3.4.1/4.2.1)

    Gradle Wrapper 3.3-all (or 3.4.1 / 4.2.1)

    注意:这对于Android Gradle Plugin 2.3.0来说是无效的

    我创建了 示例项目 来演示两种情况下的测试结构

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

    项目/模块的 build.gradle 必须使用此插件:

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

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

    此插件使用TestExtension(链接到其DSL ).使用 TestExtension 'com.android.test'插件,您的gradle文件将如下所示:

    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'
    }
    

    请注意,此处不支持"androidTestCompile"

    不要忘记创建AndroidManifest.xml.它看起来像:

    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>
    

    请注意测试源文件位于""文件夹中(而不是 androidTest ):

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

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

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

    然后,您可以在 settings.gradle 中将该测试项目链接到您的应用程序项目:

    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")
    

    在Android Studio中,您必须创建" Android Instrumented Tests "运行配置.它看起来像: 现在运行测试:

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

    如果您在产品口味方面存在构建问题,则应在应用程序的build.gradle中添加 publishNonDefault :

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

    android {
        ...
        defaultConfig {
           ...
        }
        publishNonDefault true
    }
    

    这篇关于如何将Android(仪器)测试文件放置在项目目录之外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆