簡體   English   中英

在 android 工作室中使用 espresso 在 evrey 測試用例之前清除/刪除緩存

[英]Clear/delete cache before evrey test case using espresso in android studio

我使用濃縮咖啡在 Android Studio 中編寫了測試。 現在有一些測試在我運行之前我必須刪除應用程序的緩存。 我嘗試了很多我知道的選項,但都沒有成功。 我在該網站上搜索了該問題並嘗試了結果,但也沒有一個有效。

例如,應用程序中有一個階段導致更改性別尋址(我的應用程序是外語的),我在這部分測試了很多東西,我從 3 個不同的測試用戶登錄,每個用戶都有不同的視圖除非刪除緩存並且不刪除緩存,否則無法更改,我不能一起運行它們,但我可以分別運行它們中的每一個。 該應用程序在用戶登錄的 momnet 中定義自己,因此要切換用戶,我需要刪除應用程序緩存。

我在此處附上了一些我嘗試過並且應該有效但沒有成功的鏈接。 他們可能會提供幫助和解釋

在測試用例濃縮咖啡之前清除數據庫

在 InstrumentationTestCase 運行之間重置應用程序 state

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

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

每次測試清除數據庫一次 class

將以下代碼添加到您的 Android 測試 class 中:

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

每次測試前清空數據庫

在每次測試運行之前清除數據庫的另一種方法是在使用Android Test Orchestrator時設置 clearPackageData 標志。 這將“在每次測試后從設備的 CPU 和 memory 中刪除所有共享的 state:”

將以下語句添加到項目的 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' }

Kotlin

@Test fun clearStorage(){ InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("pm clear PACKAGE NAME").close() }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM