简体   繁体   English

在releseUnitTest中使用调试资产,而不是发布应用程序资产

[英]Use debug assets in releseUnitTest instead of the release app assets

I run my unit tests with robolectric. 我用robolectric运行单元测试。 For some test I added json files in the debug assest folder and get them via: RuntimeEnvironment.application.getResources().getAssets().open(filename); 为了进行一些测试,我在调试assest文件夹中添加了json文件,并通过以下方式获取它们: RuntimeEnvironment.application.getResources().getAssets().open(filename);

In the debugUnitTest I have access to the the debugAssests, but if I run my releaseUnitTest I don't. 在debugUnitTest中,我可以访问debugAssests,但是如果我运行releaseUnitTest,则不能。

How can I tell the releaseUnitTest to get the assets from the debug folder? 如何告诉releaseUnitTest从debug文件夹中获取资产? Or where would you save the json files to use them in the unit tests? 还是将json文件保存在单元测试中的哪里?

Rather than keeping your json files in diff flavour's assets folder, you can put them in the test assets folder, which will allow all different flavours to access the assets contents. 您可以将它们放在test资产文件夹中,而不是将json文件保存在diff flavour的资产文件夹中,这将允许所有不同的风味访问资产内容。

这是截图供参考

Try adding this to the android{} block in your build.gradle file 尝试将其添加到build.gradle文件中的android {}块中

sourceSets{
    test {
        assets.srcDirs = ['src/test/assets']
    }
}

So I finally managed to get it working. 所以我终于设法使它工作了。 I found the answer in this robolectric github issue . 我在这个robolectric github 问题中找到了答案。 Thanks to nebiros. 感谢nebiros。

  • Add your fixture files to the resources folder of your test one, here: src/test/resources. 将您的灯具文件添加到测试之一的resources文件夹中,这里:src / test / resources。
  • Add test resources folder to androidTest, resources.srcDirs += ['src/test/resources'], here's an example: android { sourceSets { String sharedTestJavaDir = 'src/sharedTest/java' 将测试资源文件夹添加到androidTest,resources.srcDirs + = ['src / test / resources'],下面是一个示例:android {sourceSets {字符串sharedTestJavaDir ='src / sharedTest / java'

     test { java.srcDirs += [sharedTestJavaDir] } androidTest { java.srcDirs += [sharedTestJavaDir] resources.srcDirs += ['src/test/resources'] } 

    } } }}

  • Access fixture files from your androidTest env this way: this.getClass().getClassLoader().getResourceAsStream(filename); 通过您的androidTest env访问固定装置文件:this.getClass this.getClass().getClassLoader().getResourceAsStream(filename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM