业务逻辑
- 我们的系统查询有级联关系,必填输入框有 8 个(下拉选择式),选填另算,根据不同的维度会查到不同的数据
- 需要对查询条件做遍历,将查询到的结果记录下来(返回状态码、返回结果是否为空、返回不为空的结果判断是否正确)
具体实现
- 根据级联关系,将第一个下拉框的结果作为第二个下拉框的输入···以此类推,遍历了 8 个下拉框的内容,对内容进行全对偶的匹配,得到 8 个参数值作为一组测试用例,保存至文件,然后再读取文件获取用例做数据驱动
代码逻辑
# 获取参数
def get_data(host, file_name):
tru_file(file_name)
data = {}
factoryTypes = get_factoryType()
cutType = get_cutType()
data["cutType"] = cutType
for factotyType in factoryTypes:
data["factoryType"] = factotyType
ProductGroups = get_ProductGroup(host, factotyType)
stepGroups = get_StepGroup(host, factotyType)
for ProductGroup in ProductGroups:
data["productGroup"] = ProductGroup
productIDS = get_ProductID(host, factotyType, ProductGroup)
for productID in productIDS:
data["productId"] = productID
for stepGroup in stepGroups:
data["stepGroup"] = stepGroup
stepIds = get_StepId(host, factotyType, productID, stepGroup)
for stepID in stepIds:
data["stepId"] = stepID
ItemTypes = get_ItemType(host, factotyType, stepID)
for ItemType in ItemTypes:
data["itemType"] = ItemType
items = get_Item(host, factotyType, stepID, ItemType)
for item in items:
data["item"] = item["key"]
param = get_param(data)
with open(file_name, "a", encoding="utf-8") as f:
f.write(str(param) + "\n")
问题
- 代码属实不好看,想要优化却又不知道怎么合理优化,请教下有没有好的方法呢