为Robolectric测试用例设置resourceDir

时间:2014-10-10 20:59:08

标签: android testing android-resources robolectric

我想为我的一个Robolectric测试案例使用不同的资源目录。在这种情况下,我想使用release-variant资源。 问题是我无法使用此功能,而且我没有在网上看到使用此配置的任何人的在线示例。 它仍然使用调试资源(运行gradle testDebug,其中' Debug'是我在Gradle中的buildType之一)。目录i设置相对于清单所在的目录是正确的。我已尝试在" build / intermediates /.."中引用ref文件;太。配置似乎完全没有效果。

@RunWith(RobolectricTestRunner.class)
@Config(resourceDir = "../../variant_resources/release/res")

..

@Test
public void testHowThingsAreUsingReleaseResources() {
  ..
}

我正在使用Robolectric 2.4-SNAPSHOT 有没有人用这个配置好运?有明显的错误吗?

1 个答案:

答案 0 :(得分:0)

Qualified Resources

If the resource file is to vary based on the sub-folder, then it's possible to specify a qualifier for the resource. See the RoboElectric page on Qualified Resources.

The @Config attribute with the qualifiers parameter can be used to specify the qualifier for the resource.

For example, if there are resources:

 values/strings.xml
 values-en/strings.xml
 values-en-port/strings.xml

... then the attribute @Config(qualifiers="en-port") would ensure that the values-en-port/strings.xml resource would be used.

Changing resource paths using a different RobolectricTestRunner

Another approach to load different resources (if this is only to apply to some tests) is to implement a different implementation of the RobolectricTestRunner class, that loads different resource paths. There's a good example of how to do this here:

https://stackoverflow.com/a/29223625/3063884