Android的测试原始资源 [英] Android test raw resource

查看:131
本文介绍了Android的测试原始资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件夹结构Android的工作室:

 ├──androidTest
│├──的java
│└──水库
│└──生
│└──test_file里面
└──主
    ├──的java
    └──水库
        └──生
 └──app_file

我试图访​​问它存在于androidTest元素的原始文件夹中的 test_file里面资源。下面是从继承的Robotium测试用例里面的code ActivityInstrumentationTestCase2

  InputStream为= this.getInstrumentation()
                 .getContext()
                 .getResources()
                 .openRawResource(R.raw.test_file);

Android的工作室抛出一个参考错误,因为资源不能被发现。确切的错误是无法解析符号test_file里面。

如何引用该资源形式的测试案例,它存在于androidTest资源包?


解决方案

在默认情况下你的androidTest项目将包括您的应用程序的R舱,但androidTest的资源将生成到一个单独的文件。请确保您从测试项目导入R类:

 进口com.your.package.test.R;[..]。getInstrumentation()的getContext()getResources()openRawResource(R.raw.test_file)。

您也可以直接引用测试项目的R舱:

<$p$p><$c$c>getInstrumentation().getContext().getResources().openRawResource(com.your.package.test.R.raw.test_file);

I have the following folder structure in Android Studio:

├── androidTest
│   ├── java
│   └── res
│       └── raw
│           └── test_file
└── main
    ├── java
    └── res
        └── raw
            └── app_file

I'm trying to access the test_file resource which exists in the raw folder of the androidTest elements. Here's the code inside a Robotium test case that inherits from ActivityInstrumentationTestCase2:

InputStream is = this.getInstrumentation()
                 .getContext()
                 .getResources()
                 .openRawResource(R.raw.test_file);

Android Studio throws a reference error since the resource cannot be found. The exact error is "Cannot resolve symbol test_file".

How can I reference this resource form a test case, which exists on the androidTest resources bundle?

解决方案

By default your androidTest project will include your app's R class, but androidTest's resources will be generated into a separate file. Make sure you import the R class from your test project:

import com.your.package.test.R;

[..]

getInstrumentation().getContext().getResources().openRawResource(R.raw.test_file);

You can also directly reference the test project's R class:

getInstrumentation().getContext().getResources().openRawResource(com.your.package.test.R.raw.test_file);

这篇关于Android的测试原始资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆