如何在Android中的单元测试和仪器测试之间共享资源? [英] How to share resources between unit test and instrumentation test in android?
问题描述
我正在关注此帖子, https://gist.github.com/nebiros/91a68aaf6995fa635507
I'm following this post, http://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/, to share code, but how to share an asset?, like a fixture file?, I want to mock an api response, so I have a JSON file to do it, but I try this: https://gist.github.com/nebiros/91a68aaf6995fa635507
在单元测试中,此方法有效:
In Unit Test, this works:
ClassLoader.getSystemResourceAsStream("some_response.json");
但是在Android Intrumentation Tests中却没有,我可以在哪里放置那些Fixture文件?.
but in Android Intrumentation Tests, it doesn't, where can I put those fixture files?.
推荐答案
我发现这种方式,可以将夹具文件从test
共享到androidTest
:
I found this way, to share fixture files from test
to androidTest
:
- 将您的灯具文件添加到您的
test
文件夹的resources
文件夹中,这里:src/test/resources
. -
将
test
资源文件夹添加到androidTest
,resources.srcDirs += ['src/test/resources']
,这是一个示例:
- Add your fixture files to the
resources
folder of yourtest
one, here:src/test/resources
. Add
test
resources folder toandroidTest
,resources.srcDirs += ['src/test/resources']
, here's an example:
android {
sourceSets {
String sharedTestJavaDir = 'src/sharedTest/java'
test {
java.srcDirs += [sharedTestJavaDir]
}
androidTest {
java.srcDirs += [sharedTestJavaDir]
resources.srcDirs += ['src/test/resources']
}
}
}
通过androidTest
env访问固定装置文件:this.getClass().getClassLoader().getResourceAsStream(filename);
Access fixture files from your androidTest
env this way: this.getClass().getClassLoader().getResourceAsStream(filename);
这篇关于如何在Android中的单元测试和仪器测试之间共享资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!