java - 在android studio中使用espresso在每个测试用例之前清除/删除缓存

标签 java android android-studio caching android-espresso

我使用 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/

相关文章:

android - 如何在 Espresso 中打开标签页

java - Parse.com : with parseUser, 如何将数据保存在我在类解析中创建的列中?

java - 错误 : Out of memory: Java heap space. 使用 '-Xmx' JVM 选项配置 Gradle 内存设置

java - Akka远程路由主机名配置问题

java - 如何构建正则表达式的最佳 AST?

java - 将适配器应用于 Spinner 会导致 NullPointerException?

android - FloatBuffer 和 CharBuffer 的元素个数 : capacity() vs limit()

android - 如何解决java.util.zip.ZipException duplicate entry : com/google/gson/annotations/Expose.类?

sizeToScene 调用后的 JavaFX 舞台宽度/高度

java - Travis 使用 ant 代替 Maven