我使用 espresso 在 Android Studio 中编写了测试。
现在有许多测试在我运行之前我必须删除应用程序的缓存。
我尝试了很多我知道的选项,但没有任何效果。
我在网站上搜索了问题并尝试了结果,但它们都没有奏效。
例如,应用中有一个阶段会导致性别寻址发生变化(我的应用是外语),我在这部分测试了很多东西,我从 3 个不同的测试用户登录,每个用户都有不同的 View 除非删除缓存并且不删除缓存,否则无法更改我无法同时运行它们,但我可以单独运行它们中的每一个。
该应用程序在用户登录的 momnet 中定义自己,以便切换用户我需要删除应用程序缓存。
我在此处附上了一些我尝试过的、应该有效但没有成功的链接。
他们也许能够帮助和解释
Clear database before testcase espresso
Reset app state between InstrumentationTestCase runs
https://github.com/chiuki/espresso-samples/issues/3
https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/10
最佳答案
每个测试类清除一次数据库
将以下代码添加到您的 Android 测试类:
companion object {
@BeforeClass
fun clearDatabase() {
InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("pm clear PACKAGE_NAME").close()
}
}
每次测试前清除数据库 在每次测试运行之前清除数据库的另一种方法是在使用 Android Test Orchestrator 时设置 clearPackageData 标志。 .这将“在每次测试后从设备的 CPU 和内存中删除所有共享状态:”
Add the following statements to your project's build.gradle file:
android { defaultConfig { ... testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" // The following argument makes the Android Test Orchestrator run its // "pm clear" command after each test invocation. This command ensures // that the app's state is completely cleared between tests. testInstrumentationRunnerArguments clearPackageData: 'true' } testOptions { execution 'ANDROIDX_TEST_ORCHESTRATOR' } } dependencies { androidTestImplementation 'androidx.test:runner:1.1.0' androidTestUtil 'androidx.test:orchestrator:1.1.0' }
关于java - 在android studio中使用espresso在每个测试用例之前清除/删除缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61480388/