我正在使用Android Instrumentation测试来测试我的应用程序.
所以我有一个ActivityInstrumentationTestCase2包含多个测试的测试类扩展.代码如下所示:
public class ManageProjectsActivityTestextends ActivityInstrumentationTestCase2<ManageProjectsActivity> {
public ManageProjectsActivityTest() {
super("eu.vranckaert.worktime", ManageProjectsActivity.class);
}
@Override
protected void setUp() throws Exception {
getInstrumentation().getTargetContext().deleteDatabase(DaoConstants.DATABASE);
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
@Override
protected void runTest() throws Throwable {
super.runTest();
getActivity().finish();
}
public void testDefaults() {
// My test stuff
}
public void testAddProject() {
// My test stuff
}
}
Run Code Online (Sandbox Code Playgroud)
因此,正在测试的活动有一个项目列表.从数据库中检索项目列表.当没有可用的数据库时,所以在创建数据库时,我插入一个默认项目.
这意味着当测试运行时,这就是我所说的:
但这不是这个测试套件的作用......这是我的测试套件的结果:
在开始时我没有覆盖runTest()方法,但我认为也许我应该自己结束活动以强制重新创建,但它没有任何区别.
所以似乎DB保存在内存中(因为当我明确删除它时,甚至没有在设备上创建新的DB文件).甚至是活动,因为当在活动的onCreate中放置一个断点a时,我只会在那里进行两次测试.
为了维护DB我使用ORMLite.您可以在此处查看我的帮助程序类:http://code.google.com/p/worktime/source/browse/trunk/android-app/src/eu/vranckaert/worktime/dao/utils/DatabaseHelper.java
所以我的问题是如何强制测试始终使用不同的数据库......?
mDb.delete(DATABASE_TABLE_NAME, null, null);
Run Code Online (Sandbox Code Playgroud)
这确实是解决方案/待办事项......
我将setUp(..)方法中的第一行更改为:
cleanUpDatabase(tableList);
Run Code Online (Sandbox Code Playgroud)
然后我添加了方法cleanUpDatabse(..)liek this:
private void cleanUpDatabase(List<String> dbTables) {
Log.i(LOG_TAG, "Preparing to clean up database...");
DatabaseHelper dbHelper = new DatabaseHelper(getInstrumentation().getTargetContext());
ConnectionSource cs = dbHelper.getConnectionSource();
SQLiteDatabase db = dbHelper.getWritableDatabase();
Log.i(LOG_TAG, "Dropping all tables");
for (String table : dbTables) {
db.execSQL("DROP TABLE IF EXISTS " + table);
}
Log.i(LOG_TAG, "Executing the onCreate(..)");
dbHelper.onCreate(db, cs);
Log.i(LOG_TAG, "Verifying the data...");
for (String table : dbTables) {
Cursor c = db.query(table, new String[]{"id"}, null, null, null, null, null);
int count = c.getCount();
if (count != 1 && (table.equals("project") || table.equals("task"))) {
dbHelper.close();
Log.e(LOG_TAG, "We should have 1 record for table " + table + " after cleanup but we found " + count + " record(s)");
throw new RuntimeException("Error during cleanup of DB, exactly one record should be present for table " + table + " but we found " + count + " record(s)");
} else if (count != 0 && !(table.equals("project") || table.equals("task"))) {
dbHelper.close();
Log.e(LOG_TAG, "We should have 0 records for table " + table + " after cleanup but we found " + count + " record(s)");
throw new RuntimeException("Error during cleanup of DB, no records should be present for table " + table + " but we found " + count + " record(s)");
}
}
Log.i(LOG_TAG, "The database has been cleaned!");
dbHelper.close();
}
Run Code Online (Sandbox Code Playgroud)
这段代码在每次测试之前执行,这使得我的所有测试都相互独立.
警告:为了检索对DatabaseHelper的引用(当然是你自己的实现;))你无法调用,getActivity()因为这将启动你的活动(从而完成你所有的初始数据库加载(如果有的话......)
与这个问题有点相关,但是当我寻求帮助时,我就落到了这里.可能对一些人有所帮助.如果使用RenamingDelegatingContext初始化数据库,它将在运行之间清除数据库.
public class DataManagerTest extends InstrumentationTestCase {
private DataManager subject;
@Before
public void setUp() {
super.setUp();
RenamingDelegatingContext newContext = new RenamingDelegatingContext(getInstrumentation().getContext(), "test_");
subject = new DataManager(newContext);
}
// tests...
}
Run Code Online (Sandbox Code Playgroud)
以及关联的DataManagerClass.
public class DataManager {
private SQLiteDatabase mDatabase;
private SQLiteOpenHelper mHelper;
private final String mDatabaseName = "table";
private final int mDatabaseVersion = 1;
protected DataManager(Context context) {
this.mContext = context;
createHelper();
}
private void createHelper() {
mHelper = new SQLiteOpenHelper(mContext, mDatabaseName, null, mDatabaseVersion) {
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// createTable...
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
// upgrade table
}
};
}
...
}
Run Code Online (Sandbox Code Playgroud)
android.support.test.InstrumentationRegistry的getTargetContext,也许违反直觉,getContext应该可以解决问题:
使用getContext删除数据库(不是 getTargetContext)。
getContext().deleteDatabase(DbHelper.DATABASE_NAME);
public class DbHelperTest {
private DbHelper mDb;
@Before
public void setUp() throws Exception {
getContext().deleteDatabase(DbHelper.DATABASE_NAME);
mDb = new DbHelper(getTargetContext());
}
@After
public void tearDown() throws Exception {
mDb.close();
}
@Test
public void onCreate() throws Exception {
mDb.onCreate(mDb.getWritableDatabase());
}
@Test
public void onUpgrade() throws Exception {
mDb.onUpgrade(mDb.getWritableDatabase(), 1, 2);
}
@Test
public void dropTable() throws Exception {
String tableName = "mesa";
mDb.getReadableDatabase().execSQL("CREATE TABLE "
+ tableName + "(_id INTEGER PRIMARY KEY AUTOINCREMENT)");
mDb.dropTable(mDb.getWritableDatabase(), tableName);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6626 次 |
| 最近记录: |