上一小节,我们对测试的关键字做了梳理,并将关键字进行了方法化(呃呃呃,这是什么话)。接下来就要根据关键字的测试用例excel进行测试用例编写,首先不用说的是在excel中的测试用例必然会用到读取excel的方法,然后就是将测试用例整合起来执行它就OK了。
循环行数,去执行每一行的case
# 拿到操作值
# 是否执行
#拿到执行方法
#拿到输入数据
#是否有输入数据
# 执行方法(输入数据,操作元素)
# 没有输入数据
# 执行方法(操作元素)
#!/bin/usr/env python3
# -*- coding: utf-8 -*-
# --------------------------------
# ProjectName:
# Author: Crisimple
# CreateTime: 2019/6/24 9:47
# Description: 使用ActionMethod的方法用于测试用例中
# FileName: keyword_case.py
# Question:
# --------------------------------
import sys
import os
# 找到当前文件所在的目录
# curPath = os.path.abspath(os.path.dirname(__file__))
# print("curPath: ", curPath)
# # 然后呢
# rootPath = os.path.split(curPath)[0]
# print(rootPath)
sys.path.append("D://MySpace//Python//WebTest//")
from code_package.util.read_excel import ReadExcel
from code_package.action_method.action_method import ActionMethod
class KeywordCase(object):
def run_main(self):
self.action_method = ActionMethod()
handle_excel = ReadExcel("../config/key_word.xls")
# 拿到行数
case_lines = handle_excel.get_lines()
print(case_lines)
if case_lines:
for i in range(1, case_lines):
# 拿到单元格的数据
is_run = handle_excel.get_cell(i, 3)
if is_run == "yes":
method = handle_excel.get_cell(i, 4)
send_value = handle_excel.get_cell(i, 5)
hand_value = handle_excel.get_cell(i, 6)
except_result_method = handle_excel.get_cell(i, 7)
except_result = handle_excel.get_cell(i, 8)
# 反射
self.run_method(method, send_value, hand_value)
if except_result != '':
except_value = self.run_method(except_result_method)
if except_value[1] == "text":
result = self.run_method(except_result_method)
if except_value[1] in result:
handle_excel.write_value(i, "pass")
else:
handle_excel.write_value(i, "fail")
elif except_value[0] == "element":
result = self.run_method(except_result_method, except_value[1])
if result:
handle_excel.write_value(i, "pass")
else:
handle_excel.write_value(i, "fail")
else:
print("没有了else...")
else:
print("预期结果为空")
# 循环行数,去执行每一行的case
# 拿到操作值
# 是否执行
#拿到执行方法
#拿到输入数据
#是否有输入数据
# 执行方法(输入数据,操作元素)
# 没有输入数据
# 执行方法(操作元素)
def get_except_result_method(self, data):
return data.split("=")
def run_method(self, method, send_value='', hand_value=''):
print("send_value:------>", send_value)
print("hand_value:------>", hand_value)
method_value = getattr(self.action_method, method)
if send_value == '' and hand_value != '':
result = method_value(hand_value)
# 容错处理
elif send_value == '' and hand_value == '':
result = method_value()
elif send_value != '' and hand_value == '':
result = method_value(send_value, hand_value)
else:
result = method_value(send_value, hand_value)
return result
if __name__ == "__main__":
keyword_case = KeywordCase()
keyword_case.run_main()