在测试项目中存储测试文件

发布于 2024-11-18 13:27:04 字数 132 浏览 6 评论 0原文

我编写了一些使用 XML 文件的测试。我有两个项目,第一个是代码,第二个是测试。我想在测试项目中存储这个 XML 文件(它们包含测试期间使用的一些数据)。但这是不可能的,因为似乎只有 src 项目中的文件才会加载到设备中。有谁知道解决这个问题的方法?

I wrote some tests that use XML files. I have two project one with code, the second with tests. I would like to store this XML files (they contain some data used during the tests) in the test project. But it is not possible because it seems that only files from src project are loaded to a device. Does anyone know the way to solve this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2024-11-25 13:27:04

假设你在android项目和测试项目中都有assets文件夹,并且你将XML文件放在assets文件夹中。在测试项目下的测试代码中,这将从 android 项目资产文件夹中加载 xml:

getInstrumentation().getTargetContext().getResources().getAssets().open(testFile);

这将从测试项目资产文件夹中加载 xml:

getInstrumentation().getContext().getResources().getAssets().open(testFile);

确保您的 TestCase 类扩展了 android.test.InstrumentationTestCase 而不是android.test.AndroidTestCase 访问 this.getInstrumentation() 方法。

Suppose you got assets folder in both android project and test project, and you put the XML file in the assets folder. in your test code under test project, this will load xml from the android project assets folder:

getInstrumentation().getTargetContext().getResources().getAssets().open(testFile);

This will load xml from the test project assets folder:

getInstrumentation().getContext().getResources().getAssets().open(testFile);

Make sure your TestCase class extends android.test.InstrumentationTestCase instead of android.test.AndroidTestCase to access this.getInstrumentation() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文