在 android studio 中使用 espresso 清除/删除 evrey 测试用例之前的缓存

Gal*_* El 2 java android caching android-studio android-espresso

我使用 espresso 在 Android Studio 中编写了测试。现在有很多测试,在运行之前我必须删除应用程序的缓存。我尝试了很多我知道的选择,但没有任何结果。我在网站上搜索了该问题并尝试了结果,但都不起作用。

例如,应用程序中有一个阶段会导致性别寻址发生变化(我的应用程序是外语),我在本节中测试了很多内容,我从 3 个不同的测试用户登录,每个用户都有不同的视图除非删除缓存,否则无法更改,并且在不删除缓存的情况下,我无法一起运行它们,但我可以单独运行它们中的每一个。该应用程序在用户登录的 Momnet 中定义自身,因此要切换用户,我需要删除应用程序缓存。

我在此处附加了一些我尝试过的链接,这些链接应该有效,但没有。他们也许能够提供帮助和解释

在测试用例 espresso 之前清除数据库

在 InstrumentationTestCase 运行之间重置应用程序状态

https://github.com/chiuki/espresso-samples/issues/3

https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/10

Tho*_*les 5

每个测试类清除数据库一次

将以下代码添加到您的 Android 测试类中:

companion object {
    @BeforeClass
    fun clearDatabase() {
        InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("pm clear PACKAGE_NAME").close()
    }
}
Run Code Online (Sandbox Code Playgroud)

每次测试前清空数据库

在每次测试运行之前清除数据库的另一种方法是在使用Android Test Orchestrator时设置clearPackageData 标志。这将“在每次测试后从设备的 CPU 和内存中删除所有共享状态:”

将以下语句添加到项目的 build.gradle 文件中:

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'
}
Run Code Online (Sandbox Code Playgroud)