在测试用例浓缩咖啡之前清除数据库 [英] Clear database before testcase espresso
本文介绍了在测试用例浓缩咖啡之前清除数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
im使用espresso清除我的应用程序中的数据库 我设置这样的活动
im using espresso to clear database in my app Im setting activity like this
@Rule
@JvmField
val activity = ActivityTestRule<PhotoPrinterActivity>(PhotoPrinterActivity::class.java,false,false)
这是我的before函数
And this is my before function
@Before
open fun setup() {
clearDatabase()
activity.launchActivity(null)
// Waiting for start app success fully
}
这是我清晰的数据库代码
And this is my clear database code
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)
}
}
}
问题是当清除数据库成功并执行向数据库中添加一些数据时,
Issue is when clear database success and perform add some data into 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)
任何人都请帮助我!非常感谢!
Anyone please help me !Thanks so much!
推荐答案
使用 @在您的意式浓缩咖啡测试中将BeforeClass 和 InstrumentationRegistry 删除数据库:
@BeforeClass
public static void beforeClass() {
InstrumentationRegistry.getTargetContext().deleteDatabase("database_name");
}
为防止一次执行多个意式浓缩咖啡测试时出现错误,请使用 Android测试协调器.它将分别执行所有这些操作.
And to prevent bugs when executing multiple espresso tests at once, use Android Test Orchestrator. It will execute all of them separately.
这篇关于在测试用例浓缩咖啡之前清除数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文