访问仪器测试资源
我正在尝试找到一种方法让我的仪器测试能够访问测试包中生成的字符串资源。
更多详细信息:
我有一个继承自 ActivityInstrumentationTestCase2 的 Android 测试用例。我首先使用 Eclipse 的新 Android 测试项目来创建测试。测试项目有资源(类似于常规 Android 项目的资源)。我试图找到一种方法在各个单独的测试中以编程方式访问测试项目中的字符串资源。我已经尝试过:
String s = getInstrumentation().getContext().getString(R.string.blah);
并且
String s = mActivity.getApplicationContext().getString(R.string.blah);
两种方法都会抛出 NotFoundException。我在 strings.xml 中定义了字符串“blah”。上面代码中的 R 是从我的测试包导入的,而不是被测试应用程序的包。我可以通过后一个调用访问应用程序包中定义的资源。
在我的测试中找到一种访问 XML 定义的字符串资源的方法会很有用(因为我想避免在代码中输入字符串)。我做错了什么?
I am trying to find a way for my instrumentation tests to have access to string resources generated within the test package.
More details:
I have an Android test case which inherits from ActivityInstrumentationTestCase2. I used Eclipse's New Android Test Project to create the test in the first place. The test project has resources (similar to the resources for a regular Android project). I am trying to find a way to programmatically access String resources in the test project in the various individual tests. I have tried:
String s = getInstrumentation().getContext().getString(R.string.blah);
and
String s = mActivity.getApplicationContext().getString(R.string.blah);
Both methods throw a NotFoundException. I have the string "blah" defined in my strings.xml. R in code above is an import from my test package and not the package of the application under test. I am able to access resources defined in the application package with the latter call.
It would be useful to figure out a way to access XML defined string resources in my tests (as I want to avoid typing strings into code). What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我尝试过:
其中
com.myProject
是要测试的项目的包,com.myProject.test
是测试项目的包。如果我将代码放在
setup()
中,它对我有用。如果我将它放在构造函数中,它就不起作用。I tried:
where
com.myProject
is the package of the project to be tested andcom.myProject.test
is the package of the testing project.It worked for me if I place the code inside
setup()
. It doesn't work if I place it inside the constructor.我认为如果不将资源文件存储在其他地方,这是不可能的,但您可以使用类似 Robotium,您只需使用 Solo.getString() 即可获取资源。
I think this is not possible without storing the resource files somewhere else, but you can use something like Robotium where you just use Solo.getString() and you get your resource.
对于使用 Kotlin 的 Androidx Compose,这对我有用:
PS:我在关注 本关于 Android 仪器测试的教程
For Androidx Compose with Kotlin, this is what worked for me:
PS : I came across this while following this tutorial on Android instrumentation testing
即使这篇文章不再是最新的 - 这个添加可能会对某人有所帮助:
要理解它是如何工作的,您必须记住,您的仪器所在的上下文与您正在运行测试的应用程序的上下文不同。正如您所说的正确,您无法从仪表上下文访问目标的资源。您可以在测试项目内的单独 xml 文件中为您的检测上下文定义一些资源,或者 - 如果您想使用预定义的资源 - 您可以通过以下方式获取它们:
检测上下文(如 Wujun 所写)或目标上下文可用,直到测试用例构建完全完成。
Even if this post is no longer current - this addition might help someone:
To understand how this works, you have to keep in mind that the context your instrumentation lives in is another than the context of the application you're running your test against. As you stated correctly, you CAN'T access the resources of your target from the instumentation context. You can define some for your instrumentation context in a separate xml-file inside the test project, or - if you want to make use of the predefined resources - you can get them this way:
Neither the instrumentation context (as Wujun wrote), nor the target context is available, until the testcase construction has fully completed.