ntValue(), 1);
Integer nodelete = XXXDefaultDAO.deleteXXXDefaultDOByPrimaryKey(6L);
Assert.assertEquals(nodelete.intValue(), 0);
}
}
其中, 数据准备文件在 *.when.wiki 中, 数据验证文件在 *.then.wiki 中, 数据库中只需要保证正确的表结构即可。 每次单元测试都是自动化可重复的。
XXXDefaultDAOTest.initBlank.when.wiki
|connect|
|clean table|xxx_default|
XXXDefaultDAOTest.initRecords.when.wiki
|connect|
|clean table|xxx_default|
|clean table|xxx|
|insert|xxx_default|
| id | gmt_create | gmt_modify | cidr_block | ip_protocol | port_range | policy | nic | priority | type | is_deleted | description |
| 6 | 2014-04-08 20:18:04 | 2014-04-08 20:18:04 | 10.152.126.83 | all | | accept | 3 | 1 | 1 | 0 | bie dong |
XXXDefaultDAOTest.queryOneRecord.then.wiki
|connect|
|query|select cidr_block, ip_protocol, port_range, policy, nic, priority, type, is_deleted, description from xxx_default |
|cidr_block | ip_protocol | port_range | policy | nic | priority | type | is_deleted | description |
|192.168.10.10 | tcp | 3000:4000 | accept | 2 | 65533 | 1 | 0 | test1 |
XXXDefaultDAOTest.testUpdate.then.wiki
|connect|
|query|select cidr_block, ip_protocol, port_range, policy, nic, priority, type, is_deleted, description from xxx_default|
| cidr_block | ip_protocol | port_range | policy | nic | priority | type | is_deleted | description |
| 10.152.126.83 | udp | | accept | 1 | 1 | 1 | 0 | desc |
显然, 如果每个 DAO 测试类都写这些 WIKI 及 DAO 类(set/get 字段很耗体力), 那会是比较大的工作量。 这时候, 最好能够自动生成这些文件或文件模板, 减少手工的劳动量。
因此, 我编写了一个 python 程序, 在指定配置下, 可以自动生成相关的测试文件模板文件。
readcfg.py : 读取DAO测试类信息的配置文件
from ConfigParser import ConfigParser
config = ConfigParser()
config.read("daotest.conf")
def getAllDAOTestInfo():
allDAOTest = {}
secs = config.sections()
for sec in secs:
allDAOTest[sec] = getDAOTestInfo(sec)
print 'all DAO Test: \n', allDAOTest
return allDAOTest
def getDAOTestInfo(daoTestName) :
daoTestInfo = {
'DaoTestName': config.get(daoTestName, 'DaoTestName') ,
'TableName': config.get(daoTestName, 'TableName'),
'FieldArray': config.get(daoTestName, 'FieldArray'),
}
return daoTestInfo
create_daotest_wiki.py : 生成 dao 测试的测试文件模板:
import readcfg
def gene_daotest_wiki(daoTestInfo):
daoTestName = daoTestInfo['DaoTestName']
tableName = daoTestInfo['TableName']
fieldArray = daoTestInfo['FieldArray'].split(',')
gene_daotest_wiki_really(daoTestName, tableName, fieldArray)
gene_daotest_java(daoTestName, tableName, fieldArray)
def gene_daotest_wiki_really(DaoTestName, tableName, fieldArray):
conn = '|connect|'
clean_table = '|clean table|' + tableName + '|'
insert_table = '|insert|' + tableName + '|'
all_fields = '|' + getfieldsWithSep(fieldArray, 0, '|') + '|'
query_stmt = '|query|' + 'select ' + getfieldsWithSep(fieldArray, 0, ', ', filterTimeFieldFunc) + ' from ' + tableName + '|'
query_fields = '|' + getfieldsWithSep(fieldArray, 0, '|', filterTimeFieldFunc) + '|'
# create DaoTestName.initBlank.when.wiki
f_initBlank = open(DaoTestName+".initBlank.when.wiki", 'w')
f_initBlank.write('\n'.join([conn, clean_table]));
f_initBlank.close
# create DaoTestName.initRecords.when.wiki
f_initRecs = open(DaoTestName+".initRecords.when.wiki", 'w')
f_initRecs.write('\n'.join([conn, clean_table, insert_table, all_fields]))
f_initRecs.close
#