简体   繁体   English

在为 Android 运行仪器测试时如何访问测试设备的文件

[英]How do you get access to a testing device's files when running instrumentation tests for Android

I have set up a physical device with files that can be retrieved through the MediaStore.我已经设置了一个物理设备,其中包含可以通过 MediaStore 检索的文件。 My goal is to retrieve these files through the MediaStore in an instrumented test.我的目标是在仪器测试中通过 MediaStore 检索这些文件。

Currently, when I push the debug app to the device and test it manually, the device files are found.目前,当我将调试应用程序推送到设备并手动测试时,会找到设备文件。 The problem is when I am running instrumentation tests, and I get nothing from the MediaStore.问题是当我运行仪器测试时,我从 MediaStore 中一无所获。

Is there a way to get the files through the MediaStore in an instrumentation test?有没有办法在仪器测试中通过 MediaStore 获取文件?

Possible relevant: I define and use a dummy Activity in my instrumentation test class可能相关:我在我的仪器测试 class 中定义并使用了一个虚拟活动

        Looper.prepare()
        dummyActivity = DummyActivity()

I then tried to define the dummy Activity in the debug folder, declare it in the debug manifest, and instantiate it via an ActivityScenario.然后我尝试在调试文件夹中定义虚拟 Activity,在调试清单中声明它,并通过 ActivityScenario 实例化它。 Didn't help.没有帮助。

I made the assumption that manifest permissions carried over to test package from the debug package.我假设清单权限从调试 package 转移到测试 package。 Not so.不是这样。

Adding the following line to my test class made the MediaStore query return the files on the device!将以下行添加到我的测试 class 使 MediaStore 查询返回设备上的文件!

    @get:Rule
    var mRuntimePermissionRule = GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何将Android Instrumentation测试推送到仿真器/设备? - How do you push Android Instrumentation Tests to Emulator/Device? 你如何从Eclipse运行Android检测测试? - How do you run Android instrumentation tests from Eclipse? 运行 Android 仪器测试时出现错误“未找到测试” - Error "No tests found" when running Android instrumentation tests 如何在Android中进行工具测试的代码覆盖率 - How to get code coverage for instrumentation tests in Android IllegalAccessError运行android工具测试 - IllegalAccessError running android instrumentation tests 运行Android Instrumentation测试时“无法确定扩展文件夹” - “Cannot determine expansion folder” when running android Instrumentation tests Android:如何在Instrumentation测试中进行大量测试之前确保sharedPreference变得清晰 - Android : How to make sure that sharedPreference get clear before running a large nos of test in Instrumentation testing 如何从 Android 仪器测试中的测试资产访问 File 对象? - How to access File object from test assets in Android instrumentation tests? 如何编写Firebase Android Instrumentation测试? - How do I write Firebase Android Instrumentation Tests? 运行Android测试时为什么会出现非法访问错误? - Why do I get a Illegal Access Error when running my Android tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM