在测试用例浓缩咖啡之前清除数据库
Clear database before testcase espresso
我正在使用浓缩咖啡清除我应用程序中的数据库
我这样设置 activity
@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)
任何人都请帮助我!非常感谢!
在您的浓缩咖啡测试中使用 @BeforeClass and InstrumentationRegistry 删除数据库:
@BeforeClass
public static void beforeClass() {
InstrumentationRegistry.getTargetContext().deleteDatabase("database_name");
}
为了防止一次执行多个浓缩咖啡测试时出现错误,请使用 Android Test Orchestrator。它将单独执行所有这些。
每次测试清除一次数据库class
将以下代码添加到您的 Android 测试 class:
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'
}
如果您因弃用而无法使已接受的答案起作用,我相信 Instrumentation api 已更新为:
InstrumentationRegistry .getInstrumentation() .getTargetContext()
因此:
InstrumentationRegistry.getInstrumentation().getTargetContext().deleteDatabase("database_name")
或者,您可以获得实际的应用程序上下文(“目标”),您可以这样做:
ApplicationProvider.getApplicationContext<YOUR-APPLICATION>().deleteDatabase("database_name")
我正在使用浓缩咖啡清除我应用程序中的数据库 我这样设置 activity
@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)
任何人都请帮助我!非常感谢!
在您的浓缩咖啡测试中使用 @BeforeClass and InstrumentationRegistry 删除数据库:
@BeforeClass
public static void beforeClass() {
InstrumentationRegistry.getTargetContext().deleteDatabase("database_name");
}
为了防止一次执行多个浓缩咖啡测试时出现错误,请使用 Android Test Orchestrator。它将单独执行所有这些。
每次测试清除一次数据库class
将以下代码添加到您的 Android 测试 class:
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' }
如果您因弃用而无法使已接受的答案起作用,我相信 Instrumentation api 已更新为: InstrumentationRegistry .getInstrumentation() .getTargetContext()
因此:
InstrumentationRegistry.getInstrumentation().getTargetContext().deleteDatabase("database_name")
或者,您可以获得实际的应用程序上下文(“目标”),您可以这样做:
ApplicationProvider.getApplicationContext<YOUR-APPLICATION>().deleteDatabase("database_name")