【问题标题】:Clear/delete cache before evrey test case using espresso in android studio在 evrey 测试用例之前在 android studio 中使用 espresso 清除/删除缓存
【发布时间】:2020-08-12 06:39:47
【问题描述】:

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

例如,应用程序中有一个阶段导致性别地址发生变化(我的应用程序是外语的),我在这一部分测试了很多东西,我从 3 个不同的测试用户登录,每个用户都有除非删除缓存并且不删除缓存,否则无法更改的不同视图我不能一起运行它们,但我可以分别运行它们中的每一个。 该应用程序在用户登录的 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

【问题讨论】:

    标签: java android android-studio caching android-espresso


    【解决方案1】:

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

    将以下代码添加到您的 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'
    }
    

    【讨论】:

    • 您的第一个代码块似乎不起作用,因为我的应用程序在运行后无法启动,后续测试也没有。它几乎就像它删除应用程序一样?
    猜你喜欢
    • 2021-12-01
    • 2016-01-03
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    相关资源
    最近更新 更多