Android revoke permission at start of each test

后端 未结 6 905
花落未央
花落未央 2021-01-17 11:49

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 w

6条回答
  •  甜味超标
    2021-01-17 12:07

    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.

    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")
    }
    

    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.

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

提交回复
热议问题