我正在使用Espresso和UIAutomator来编写我的测试用例.我正在测试外部存储权限,当它被拒绝时以及何时被允许.我有不同的测试用例,都需要在测试用例开始时撤销权限.但是,某些测试用例应该并且确实会导致授予权限,因此我需要在执行下一个测试时撤消权限.我一直在搜索,我遇到的最接近的事情是使用pm管理器执行adb shell命令来撤销权限.但是,通过这样做,我将收到以下错误,由于"进程崩溃",Instrumentation运行失败.有没有什么办法可以确保在每个测试用例开始时撤销权限?如果没有,如何解决有关测试权限的问题?提前谢谢你的帮助!
这是我目前在每个测试用例之前撤消权限的代码片段(不起作用):
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
尝试使用上面的代码段撤消权限时出现相应的错误消息:
Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''.
Run Code Online (Sandbox Code Playgroud)
android android-testing android-permissions android-espresso android-uiautomator
我是Android仪表单元测试的新手.我有一个看起来像这样的简单测试.这是仪器化测试; 在我真正编写一个实际的仪器测试之前,我只想测试一个简单的测试:
@RunWith(AndroidJUnit4.class)
@SmallTest
public class AddressBookKeepingInstrumentedTest {
public static final String TEST_STRING = "This is a string";
public static final long TEST_LONG = 12345678L;
@Test
public void test_simple() {
assertEquals(2,1+1);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到以下错误:
junit.framework.AssertionFailedError: No tests found in com.example.myfirstapp.AddressBookKeepingInstrumentedTest
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)
Tests ran to completion.
Run Code Online (Sandbox Code Playgroud)
Gradle构建在此之前成功通过.任何想法为什么会这样?