设为首页 加入收藏

TOP

Jtester+unitils+testng:DAO单元测试文件模板自动生成(二)
2014-11-24 03:31:13 来源: 作者: 【 】 浏览:8
Tags:Jtester unitils testng DAO 单元 测试 文件 模板 自动生成
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
	
	#
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇数据库基础知识复习 下一篇Nosql入门知识

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)