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

Bin*_*ngo 7 testing android android-resources android-testing android-espresso

我想将数据放在androidTest/res/values/string.xml文件夹的xml文件中.我创建了文件说test.xml,内容如下.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="test">hello</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

当我试图通过R.string.test访问现场测试时.它无法访问,并说"无法解决符号测试".有人可以在这里建议我.

Igo*_*sky 9

这是您的操作方式:

import com.my.example.test.R;

Resources resources = InstrumentationRegistry.getContext().getResources();
String terms = resources.getString(R.string.test);
Run Code Online (Sandbox Code Playgroud)


jit*_*555 5

androidx.test.platform.app.InstrumentationRegistry 已弃用 androidx.test.core.app.ApplicationProvider

val targetContext = ApplicationProvider.getApplicationContext<Context>()
val test: String =  targetContext.resources.getString(R.string.test)
Run Code Online (Sandbox Code Playgroud)