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

问题描述 投票:0回答:3

我使用 espresso 在 Android Studio 中编写测试。 现在有许多测试,在我运行之前我必须删除应用程序的缓存。 我尝试了很多我知道的选项,但没有成功。 我在网站上搜索了问题并尝试了结果,但它们都没有用。

例如,应用程序中有一个阶段会导致更改性别地址(我的应用程序是外语),我在这部分测试了很多东西,我从 3 个不同的测试用户登录,每个用户都有不同的除非删除缓存并且不删除缓存,否则无法更改的视图我不能一起运行它们,但我可以单独运行它们中的每一个。 该应用程序在用户登录的 momnet 中定义自己,以便切换用户我需要删除应用程序缓存。

我在这里附上了一些我尝试过的链接,这些链接应该有效但没有。 他们或许可以提供帮助和解释

在测试用例浓缩咖啡之前清除数据库

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

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

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

java android android-studio caching android-espresso
3个回答
3
投票

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

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

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

每次测试前清空数据库

在每次测试运行之前清除数据库的另一种方法是在使用 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'
}

0
投票

对我来说,我需要在脚本之前添加

adb shell
以使其工作,我还添加了
BuildConfig
来为您找到包裹(您稍后可能会更改它)

  @Before
    fun setup() {
        InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("adb shell pm clear ${BuildConfig.APPLICATION_ID}").close()
    }

-1
投票

科特林

@Test
fun clearStorage() {
    InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("pm clear PACKAGE NAME").close()
}
© www.soinside.com 2019 - 2024. All rights reserved.