Espresso:如何使用androidTest文件夹的R.string资源

Espresso:如何使用androidTest文件夹的R.string资源,android,testing,android-resources,android-testing,android-espresso,Android,Testing,Android Resources,Android Testing,Android Espresso,我想把数据放在androidTest/res/values/string.xml文件夹的xml文件中。我已经创建了文件say test.xml,包含以下内容 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="test">hello</string> </resources> 你好 当我试图通过R.string.test访问现场测试时。它不可访问

我想把数据放在androidTest/res/values/string.xml文件夹的xml文件中。我已经创建了文件say test.xml,包含以下内容

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="test">hello</string>
</resources>

你好
当我试图通过R.string.test访问现场测试时。它不可访问,并显示“无法解析符号测试”。有人能在这里给我一些建议吗。

这就是你的做法:

导入com.my.example.test.R

Resources resources = InstrumentationRegistry.getContext().getResources();
String terms = resources.getString(R.string.test);

androidx.test.platform.app.InstrumentationRegistry
不推荐使用
androidx.test.core.app.ApplicationProvider

val targetContext = ApplicationProvider.getApplicationContext<Context>()
val test: String =  targetContext.resources.getString(R.string.test)
val targetContext=ApplicationProvider.getApplicationContext()
val test:String=targetContext.resources.getString(R.String.test)

@sabersafavi,事实上我们有信用卡等信息,我们想存储这些信息,并在浓缩咖啡测试中使用它们。作为它的关键信息,我们既不希望它存储在主项目文件中,也不希望每次构建发布版本apk时都删除它。这里可能重复的技巧是正确的导入,因为默认情况下作为导入
com.yourpack.test.R
而不是
com.yourpackage.test.R
。所以你需要手动修复它。@MyDogTom谢谢,我现在可以用了。缺少的部分包括在gradle文件中{debug{res.srcDirs=['src/main/res','src/test/res','src/androidTest/res','src/debug/res']}@user3400729
getTargetContext()
将从应用程序的APK读取值;我认为
androidTest
文件夹的要点是,插装测试是一个不同的APK,因此您需要使用
getContext()
从中读取资源。