这不是(优雅地处理数据,因此您不必担心恢复它)使用模拟对象进行测试的目的是什么?Android支持模拟。
我问一个问题,因为我从来没有嘲笑过 Android。
根据我的经验和这篇博客文章,当 Android 测试被制成一个套件并由InstrumentationTestRunner运行时- ActivityInstrumentationTestCase2是 ActivityTestCase 的扩展,它是InstrumentationTestCase的扩展- 它们按字母顺序使用android.test.suitebuilder.TestGrouping.SORT_BY_FULLY_QUALIFIED_NAME
,所以你可以恢复你DB 的方法是测试名称中字母表中的最低值,例如:
// underscore is low in the alphabet
public void test___________Restore() {
...
}
笔记:
您必须注意继承的测试,因为它们不会按此顺序运行。解决方案是覆盖所有继承的测试并简单地从覆盖中调用 super()。这将再次让所有内容按字母顺序执行。
例子:
// Reusable class w only one time setup and finish.
// Abstract so it is not run by itself.
public abstract class Parent extends InstrumentationTestCase {
@LargeTest
public void test_001_Setup() { ... }
@LargeTest
public void test_____Finish() { ... }
}
/*-----------------------------------------------------------------------------*/
// These will run in order shown due to naming.
// Inherited tests would not run in order shown w/o the use of overrides & supers
public class Child extends Parent {
@LargeTest
public void test_001_Setup() { super.test_001_Setup(); }
@SmallTest
public void test_002_MainViewIsVisible() { ... }
...
@LargeTest
public void test_____Finish() { super.test_____Finish(); }
}