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

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

问题描述

我们必须执行我们的Andr​​oid应用的单元测试,这使得资产使用它时,才需要进行单元测试的并不少见要求。

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的工作室,这是命中注定的事情,一个配置在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:

  • 添加一个测试封闭sourceSets Android的接线盒中(通过关闭我的意思是这是在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/']
}

  • 同样的事情,但是,用测试作为sourceSet的名称(而不是androidTest,同上),也与instrumentTest

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

    这个问题可以更一般地表述为一个人如何运用在机器人工作室的单元测试不同的目录。

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

    我们一直在从机器人的文件,要么我们不理解它,这是错误的,或者有一个错误的地方。

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

    任何帮助将非常AP preciated。

    Any help would be very much appreciated.

    推荐答案

    在我自己的问题的答案,为此,我打进了令人垂涎的风滚草奖保留用于激励一个特别显着的缺乏兴趣的问题,我迷迷糊糊。切勿-的少,可能证明是有用的人。

    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的摇篮构建一个错误,但在没有任何结论性的文件,那将必须保持一个意见。

    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 /资产是行不通的。然而调试/资产呢。所以,做调试/ Java的捎带因此解决办法是把任何和所有测试资源和code进入调试根,除了单位的测试本身。

    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.

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

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