测试平台开发-动态生成测试用例

测试平台开发-动态生成测试用例,第1张

测试平台开发-动态生成测试用例 测试平台开发-动态生成测试用例

现在为了让手工做上自动化,关键字驱动越来越普遍,不管是使用Excel、yaml还是别的格式文件储存用例,都需要在读取到关键字后动态生成用例。这里就讲一下如何使用python的setattr方法读取文档用例后,动态生成测试用例

实战:
  1. 创建一个测试类Test
# 使用的unittest框架
class Test(unittest.TestCase):
	# 声明一个启动方法  这里有个坑  不能声明为run  之前方法名声明为run,一直检测不到用例
    def begin_test(self, case_date):
    	print(case_date)
        # todo :遍历步骤  根据关键字执行用例
  1. 获取excel文件数据
def read_excel(file_name):
    file = os.path.dirname(os.path.dirname(__file__)) + "/%s" % file_name
    # 打开excel表格
    excel_file = xlrd.open_workbook_xls(filename=file)
    # 获取到对应的sheet
    sheet = excel_file.sheet_by_index(0)
    # 获取行总行数
    rows = sheet.nrows
    # 遍历表格获取内容
    for i in range(0, rows):
        # todo: 遍历文件 将文件存储陈想要的格式进行返回
    return datas
  1. 动态生成Test类下的测试用例
def to_test(excel_data):
    """
    将读取到的excel内容 转换成测试用例
    """
    # 遍历表格内容
    for i in range(len(excel_data)):
        # 使用setattr方法 在Test类动态生成test方法 方法体内容:to_begin(excel_data[i])
        setattr(Test, 'test_%s' % str(i + 1), to_begin(excel_data[i]))

def to_begin(case_date):
    def demo(self):
    	# 调用Test类型下的begin_test  作为用例启动
        Test.begin_test(self, case_date)
    return demo
  1. 启动测试
if __name__ == '__main__':
    data = read_excel('关键字驱动.xls')
    to_test(data)
    unittest.main()

根据以上四步就可以完成读取文档用例并动态生成测试用例

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5680217.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存