在 Android Studio 中为单元测试设置自定义资产目录 [英] Setting a custom assets directory for unit testing in Android Studio

查看:23
本文介绍了在 Android Studio 中为单元测试设置自定义资产目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们经常需要执行我们的 Android 应用程序的单元测试,这些单元测试使用仅用于单元测试所需的资产.

We have the not uncommon requirement of executing unit tests of our Android application which make use of assets which are only required for unit testing.

对于基于 Eclipse 的 SDK,这很简单而且显而易见 - 测试项目是一个单独的项目,具有自己的资产文件夹.对于 Android Studio,这意味着可以在 build.gradle 中配置(或按照惯例).

With the Eclipse-based SDK this was simple and obvious - the test project was a separate project with its own assets folder. With Android Studio this is meant to be something that one configures in build.gradle (or by convention).

我们的单元测试最终正在执行,但我已经用尽了我发现的关于自定义(并且理想情况下是合并的)资产文件夹的所有建议.这是我尝试过的:

Our unit tests are executing, finally, but I've exhausted every suggestion I've found find regarding a custom (and, ideally, merged) assets folder. Here's what I've tried:

  • 在 android 闭包中向 sourceSets 添加一个测试闭包(闭包是指 build.gradle 中的大括号之间的那个).

  • Adding a test closure to sourceSets within the android closure (by closure I mean that which is between the curly braces in build.gradle).

androidTest {
    assets.srcDirs = ['src/androidTest/assets/']
}

  • 同样的事情,但使用test"作为 sourceSet 的名称(而不是上面的androidTest")和instrumentTest".

  • Same thing, but with "test" as the name of the sourceSet (as opposed to "androidTest", above) and also with "instrumentTest".

    这个问题可以更笼统地表述为如何在 Android Studio 中使用不同的目录进行单元测试".

    This question could be more generically stated as "how does one employ different directories for unit tests in Android Studio".

    我们已经阅读了 Android 的文档,但要么我们不理解它,要么是错误的,要么是某个地方存在错误.

    We've been over the documentation from Android and either we're not understanding it, it's wrong, or there's a bug somewhere.

    非常感谢任何帮助.

    推荐答案

    我偶然发现了自己问题的答案,为此我获得了梦寐以求的风滚草"奖.尽管如此,它可能对某人有用.

    I've stumbled upon an answer for my own question, for which I scored the coveted "Tumbleweed" award reserved for questions that inspire a particularly notable lack of interest. Never-the-less it may prove useful to someone.

    最后,我将把它描述为 Android 的 gradle 版本中的一个错误,但在没有任何决定性文档的情况下,必须保留意见.

    In the end I'm going to describe this as a bug in Android's gradle build but in the absence of any conclusive documentation that's going to have to remain an opinion.

    androidTest/assets"似乎不起作用.但是调试/资产"确实如此.顺便说一下,debug/java"也是如此,因此解决方案是将所有测试资源和代码放入调试根目录中,单元测试本身除外.

    It would appear that "androidTest/assets" just doesn't work. However "debug/assets" does. So does "debug/java" incidentally so the solution is to put any and all testing resources and code into the debug root, with the exception of the unit tests themselves.

    我还在 build.gradle 中添加了以下修改:

    I also added the following modifications to my build.gradle:

        androidTest {
            assets.srcDirs = ['src/main/assets', 'src/androidTest/assets/', 'src/debug/assets/']
            java.srcDirs = ['src/main/java', 'src/androidTest/java', 'src/debug/java']
        }
    

    现在我有使用发布版本未知的资产和模拟的功能单元测试.

    And now I have functioning unit tests employing assets and mocks that are unknown to the release build.

    这篇关于在 Android Studio 中为单元测试设置自定义资产目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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