【问题标题】:Clear database before testcase espresso在测试用例浓缩咖啡之前清除数据库
【发布时间】:2022-03-07 23:11:29
【问题描述】:

我正在使用 espresso 清除我的应用程序中的数据库 我像这样设置活动

@Rule
@JvmField
val activity = ActivityTestRule<PhotoPrinterActivity>(PhotoPrinterActivity::class.java,false,false)

这是我之前的功能

@Before
open fun setup() {
    clearDatabase()
    activity.launchActivity(null)
    // Waiting for start app success fully

}

这是我清晰的数据库代码

fun clearDatabase() {
    val databaseList = InstrumentationRegistry.getInstrumentation().targetContext.databaseList()
    for (database in databaseList) {

        // when transaction rollback files exists they are always locked so we can't delete them
        if (database.contains(".db-journal")) {
            InstrumentationRegistry.getTargetContext().deleteDatabase(database)
            continue
        }

        // when using transaction write ahead logging then this db files are listed but often they don't exist
        if (database.contains(".db-wal") || database.contains(".db-shm")) {
            InstrumentationRegistry.getTargetContext().deleteDatabase(database)
            continue
        }
        Log.v("EspressoMacchiato", "deleting " + database)
        var databasePath = InstrumentationRegistry.getInstrumentation().targetContext.getDatabasePath(database)
        if (databasePath.exists()) {
            InstrumentationRegistry.getInstrumentation().targetContext.deleteDatabase(database)
        }
    }

}

问题是当清除数据库成功并执行向数据库中添加一些数据时,

android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:786)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)

任何人请帮助我!非常感谢!

【问题讨论】:

  • 删除操作可能会锁定数据库文件,这就是您会看到此错误的原因。尝试在您的活动启动时发布延迟以测试是否是这种情况。
  • 你能把你的数据库代码贴出来

标签: android kotlin ui-automation android-espresso


【解决方案1】:

在您的 espresso 测试中使用 @BeforeClassInstrumentationRegistry 删除数据库:

@BeforeClass
public static void beforeClass() {
    InstrumentationRegistry.getTargetContext().deleteDatabase("database_name");
}

为了防止同时执行多个 espresso 测试时出现错误,请使用 Android Test Orchestrator。它将分别执行所有这些。

【讨论】:

    【解决方案2】:

    如果由于弃用而无法使接受的答案起作用,我相信 Instrumentation api 已更新为: InstrumentationRegistry .getInstrumentation() .getTargetContext()

    因此:

    InstrumentationRegistry.getInstrumentation().getTargetContext().deleteDatabase("database_name")
    

    或者,您可以获取您可以执行的实际应用程序上下文(“目标”):

    ApplicationProvider.getApplicationContext<YOUR-APPLICATION>().deleteDatabase("database_name")
    

    【讨论】:

      【解决方案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'
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多