Jef*_*rod 10 java junit android junit4 junit3
我想在用测试夹具替换它之前备份我的应用程序的数据库.由于Android限制,我被迫使用Junit3,我想实现@BeforeClass和@AfterClass的等效行为.
更新:现在有一个工具(Junit4Android)可以在Android上获得对Junit4的支持.这有点像kludge,但应该工作.
为了实现@BeforeClass等价物,我一直在使用静态变量并在第一次运行期间初始化它,但我需要能够在运行所有测试后恢复数据库.我想不出有什么方法可以检测上次测试的运行时间(因为我认为测试执行顺序无法保证.)
public class MyTest extends ActivityInstrumentationTestCase2<MainActivity> {
private static boolean firstRun = true;
@Override
protected void setUp() {
if(firstRun) {
firstRun = false;
setUpDatabaseFixture();
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
来自junit网站:
在套件中包装了setUp和tearDown方法.如果你想运行一个YourTestClass测试用例,那就是这种情况.
public static Test suite() {
return new TestSetup(new TestSuite(YourTestClass.class)) {
protected void setUp() throws Exception {
System.out.println(" Global setUp ");
}
protected void tearDown() throws Exception {
System.out.println(" Global tearDown ");
}
};
}
Run Code Online (Sandbox Code Playgroud)
如果您只想为所有测试用例运行一个setUp和tearDown,请创建一个套件并将testClass添加到它并在TestSetup构造函数中传递套件对象.但我认为这没有多少用处,并且在某种程度上它是违反JUnit哲学.
| 归档时间: |
|
| 查看次数: |
7301 次 |
| 最近记录: |