简介:Python中的 itertools 模块在某些存在多条件的用例场景,可以通过该模块进行用例编写。
最终效果:
案例:多条件下提现功能用例编写。
# -*-coding:utf-8-*-
import itertools
import xlwt
import datetime
f = xlwt.Workbook() # 创建工作簿
sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True) # 创建sheet1
case = ["用例名称"]
case_id = ["ID"]
case_type = ["用例类型"]
case_user = ["创建人"]
case_create_time = ["创建时间"]
user = ["个人用户", "对公用户", "特殊用户"]
scene = ["正常时间", "系统维护不可用时间"]
card = ["有效卡", "未绑卡", "卡挂失", "冻结卡"]
count = ["超出当日次数", "正常次数范围"]
withdraw = ["提现金额大于余额", "余额为零", "提现金额大于单笔最大", "提现金额小于单笔最小", "提现金额小于最小提现金额"]
conditions = [user, scene, card, count, withdraw]
r = itertools.product(*conditions) # 拆包
number = 0
for i in r:
case_name = " - ".join(i)
case.append(case_name)
case_id.append("{}".format(str(1000000 + number)))
case_type.append("功能用例")
case_user.append("Tom")
time = str(datetime.datetime.now())[:19]
case_create_time.append("{}".format(time))
number += 1
for i, v in enumerate(case):
sheet1.write(i, 0, case_id[i])
sheet1.write(i, 1, v) # i表示行数, 1表示第2列。v是插入的值
sheet1.write(i, 2, case_type[i])
sheet1.write(i, 3, case_user[i])
sheet1.write(i, 4, case_create_time[i])
f.save('text.xls') # 保存文件
微信公众号:玩转测试开发
欢迎关注,共同进步,谢谢!
网友评论