字号: 小 中 大 |
推荐给好友
上一篇 |
下一篇
软件单元测试的测试用例编写方法
发布: 2010-9-10 08:28 |
作者: 网络转载 |
来源:
领测软件测试网采编 |
查看: 298次 | 进入软件测试论坛讨论
领测软件测试网
三、如何为每个测试方法单独提供环境方法呢?
通过前面的讲解,相信大家对模板方法和回调函数模式都已经掌握了,这里直接给出相关代码:
/**
* DAL层测试支持类.
*
*
* 除非特殊情况,所有DAO都要继承此类.
*
* @author tao.youzt
*/
public abstract class GodzillaDalTestSupport extends AbstractDependencyInjectionSpringContextTests {
/*
* @see org.springframework.test.AbstractDependencyInjectionSpringContextTests#getConfigLocations()
*/
@Override
protected final String[] getConfigLocations() {
String[] configLocations = null;
String[] customConfigLocations = getCustomConfigLocations();
if (customConfigLocations != null && customConfigLocations.length > 0) {
configLocations = new String[customConfigLocations.length + 2];
configLocations[0] = "classpath:godzilla/dal/godzilla-db-test.xml";
configLocations[1] = "classpath:godzilla/dal/godzilla-dao.xml";
for (int i = 2; i < configLocations.length; i++) {
configLocations[i] = customConfigLocations[i - 2];
}
return configLocations;
} else {
return new String[] { "classpath:godzilla/dal/godzilla-db-test.xml",
"classpath:godzilla/dal/godzilla-dao.xml" };
}
}
/**
* 子类可以覆盖该方法加载个性化配置.
*
* @return
*/
protected String[] getCustomConfigLocations() {
return null;
}
/**
* 准备测试环境.
*/
protected void setupEnv() {
}
/**
* 清除测试数据.
*/
protected void cleanEvn() {
}
/**
* 测试用例执行器.
*/
protected abstract class TestExecutor {
/**
* 准备测试环境
*/
public void setupEnv() {
}
/**
* 执行测试用例.
*/
public abstract void execute();
/**
* 清除测试数据.
*/
public void cleanEnv() {
}
}
/**
* 执行一个测试用例.
*
* @param executor
*/
protected final void execute(final TestExecutor executor) {
execute(IgnoralType.NONE, executor);
}
/**
* 执行一个测试用例.
*
* @param executor
*/
protected final void execute(final IgnoralType ignoral, final TestExecutor executor) {
switch (ignoral) {
case NONE: {
setupEnv();
executor.setupEnv();
executor.execute();
executor.cleanEnv();
cleanEvn();
break;
}
case BOTH: {
executor.execute();
break;
}
case GLOBAL: {
executor.setupEnv();
executor.execute();
executor.cleanEnv();
break;
}
case LOCAL: {
setupEnv();
executor.execute();
cleanEvn();
break;
}
case GLOBAL_S: {
executor.setupEnv();
executor.execute();
executor.cleanEnv();
cleanEvn();
break;
}
case GLOBAL_C: {
setupEnv();
executor.setupEnv();
executor.execute();
executor.cleanEnv();
break;
}
case LOCAL_S: {
setupEnv();
executor.execute();
executor.cleanEnv();
cleanEvn();
break;
}
case LOCAL_C: {
setupEnv();
executor.setupEnv();
executor.execute();
cleanEvn();
break;
}
case BOTH_SETUP: {
executor.execute();
executor.cleanEnv();
cleanEvn();
break;
}
case BOTH_CLEAN: {
setupEnv();
executor.setupEnv();
executor.execute();
break;
}
case GLOBAL_S_LOCAL_C: {
executor.setupEnv();
executor.execute();
cleanEvn();
break;
}
case GLOBAL_C_LOCAL_S: {
setupEnv();
executor.execute();
executor.cleanEnv();
break;
}
}
}
/**
* 忽略类型Enum.
*/
public enum IgnoralType {
/** 不忽略任何环境相关方法 */
NONE,
/** 忽略全局环境相关方法 */
GLOBAL,
/** 忽略局部环境相关方法 */
LOCAL,
/** 忽略所有环境相关方法 */
BOTH,
/** 忽略全局准备测试环境方法 */
GLOBAL_S,
/** 忽略全局清除测试数据方法 */
GLOBAL_C,
/** 忽略局部准备测试环境方法 */
LOCAL_S,
/** 忽略局部清除测试数据方法 */
LOCAL_C,
/** 忽略全部准备测试环境方法 */
BOTH_SETUP,
/** 忽略全部清楚测试数据方法 */
BOTH_CLEAN,
/** 忽略全局准备测试环境和局部清除测试数据方法 */
GLOBAL_S_LOCAL_C,
/** 忽略全局清除测试数据和局部准备测试环境方法 */
GLOBAL_C_LOCAL_S
}
}
/**
* URL DAO测试类.
*
* @author tao.youzt
*/
public class TestBizUrlDAO extends GodzillaDalTestSupport {
private BizUrlDAO bizUrlDAO;
@Override
protected void setupEnv() {
bizUrlDAO.delete("www.easyjf.com");
}
@Override
protected void cleanEvn() {
bizUrlDAO.delete("www.easyjf.com");
}
/**
* 测试插入一条新数据.
*/
public void testInsert() {
execute(new TestExecutor() {
@Override
public void execute() {
bizUrlDAO.insert(generateDO());
assertNotNull(bizUrlDAO.getByUrl("www.easyjf.com"));
}
});
}
/**
* 测试重复插入数据的情况.
*/
public void testDuplicateInsert() {
execute(new TestExecutor() {
@Override
public void setupEnv() {
bizUrlDAO.insert(generateDO());
}
@Override
public void execute() {
try {
bizUrlDAO.insert(generateDO());
assertTrue("Must throw an exception!", false);
} catch (Exception e) {
assertTrue("Expect this exception.", true);
}
}
});
}
/**
* 测试删除一条已经存在的数据.
*/
public void testDelete() {
execute(IgnoralType.GLOBAL_C, new TestExecutor() {
@Override
public void execute() {
assertNotNull(bizUrlDAO.getByUrl("www.easyjf.com"));
bizUrlDAO.delete("www.easyjf.com");
assertNull(bizUrlDAO.getByUrl("www.easyjf.com"));
}
@Override
public void setupEnv() {
bizUrlDAO.insert(generateDO());
}
});
}
/**
* 生成一个用于测试的DO.
*
* @return
*/
private BizUrlSynchronizeDO generateDO() {
BizUrlDO bizUrlDO = new BizUrlDO();
bizUrlDO.setUrl("www.easyjf.com");
bizUrlDO.setName("EasyJWeb");
bizUrlDO.setEmail("webmaster@easyjf.com");
return bizUrlDO;
}
public void setBizUrlDAO(BizUrlSynchronzieDAO bizUrlDAO) {
this.bizUrlDAO = bizUrlDAO;
}
}
注意testDeleate()方法,我们传入了两个参数,第一个参数IgnoralType.GLOBAL_C 代表忽略哪个方法,有12种类型可以设置。GLOBAL_C代表忽略全局的清除测试数据方法,其他见代码注释。
文章来源于领测软件测试网 https://www.ltesting.net/