每次测试开始时Android都撤消许可 [英] Android revoke permission at start of each test

查看:95
本文介绍了每次测试开始时Android都撤消许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Espresso和UIAutomator编写测试用例.拒绝和允许时,我正在测试外部存储权限. 我有不同的测试用例,所有这些用例都需要在测试用例开始时撤销权限. 但是,某些测试用例应该并且确实会导致授予许可,因此在执行下一个测试时,我需要撤消许可. 我到处搜索了,发现的最接近的东西是使用pm manager执行adb shell命令来撤消权限.但是,这样做,我将收到以下错误,由于进程崩溃",使工具运行失败.有什么方法可以确保在每个测试用例开始时都撤消权限?如果没有,如何解决有关测试权限的问题?谢谢您的提前帮助!

I'm using Espresso and UIAutomator to write my test cases. I'm testing external storage permissions when it's denied and when it's allowed. I have different test cases which all require the permission to be revoked at the start of the test case. However, some of the test cases should and do result in the permission being granted, so I need to revoke the permission when the next test is being executed. I have searched around and the closest thing I came across is using the pm manager to execute adb shell commands to revoke the permission. However by doing so, I'll get the following error, Instrumentation run failed due to 'process crash'. Is there any way I can ensure permissions are revoked at the beginning of every test case? If not, how can this issue be resolved regarding testing permissions? Thank you for your help in advance!

这是我当前在每个测试用例之前都必须撤消权限的代码片段(无效):

This is the code snippet I currently have to revoke permission before each test case (which doesn't work):

@Before
public void setUp() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getInstrumentation().getUiAutomation().executeShellCommand(
                "pm revoke " + getTargetContext().getPackageName()
                        + " android.permission.WRITE_EXTERNAL_STORAGE");
    }
}

尝试使用上述代码片段撤消权限时出现相应的错误消息:

Corresponding error message when trying to revoke permission with above code snippet:

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''.

我也遇到过这两篇文章:

I have also come across these two posts: this and this.

推荐答案

在测试开始之前,您不应撤消任何许可.这将重新启动整个测试应用程序过程,并且该测试将标记为失败.

You should not revoke any permission before the tests start. This will restart the entire test app process and the test will be marked as failure.

相反,您可以在测试执行完成后撤消权限,即在@After方法中如下:

Instead you can revoke the permission after test execution is completed i.e. in @After method as follows:

@After
fun tearDown(){
    InstrumentationRegistry.getInstrumentation().uiAutomation.
            executeShellCommand("pm revoke ${getTargetContext().packageName} android.permission.WRITE_EXTERNAL_STORAGE")
}

或者,您可以使用 Android测试协调器版本1.0.2并设置

Alternatively, you can use Android Test Orchestrator version 1.0.2 and set

 testInstrumentationRunnerArguments clearPackageData: 'true'

这将在每次测试后清除包装数据.

this will clear the package data after each test.

注意:在通过Jacoco管理代码覆盖率报告时,Android Test Orchestrator v1.0.2出现问题.

Note: Android Test Orchestrator v1.0.2 has issues when it comes to managing code coverage reports via Jacoco.

这篇关于每次测试开始时Android都撤消许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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