YAML里的值用参数化处理,避免测试数据量大时,整个YAML文件也很大,测试数据写在CSV文件里。也算是自动化测试框架中的一部分吧。
整个思路是这样的:
1、准备CSV文件,写好测试数据
name,appid,secret,grant_type,assert_str
first_case,id1,secret1,grant_type1,errcode1
second_case,"",secret2,grant_type2,errcode2
third_case,id3,"",grant_type3,errcode3
forth_case,id4,secret4,"",errcode4
fifth_case,id5,secret5,"",errcode5
sixth_case,id6,secret6,"",errcode6
2、解析CSV文件,转化成JSON格式
import csv
import json
from contextlib import ExitStack
"""
将csv文件转换成json
"""
profileList = []
def FromCsvToJson(csv_path):
with open(csv_path, 'r', encoding='utf-8') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
profileList.append(dict(row))
return profileList
可以看出就是个字典列表
[{'name': 'first_case', 'appid': 'id1', 'secret': 'secret1', 'grant_type': 'grant_type1', 'assert_str': 'errcode1'}, {'name': 'second_case', 'appid': '', 'secret': 'secret2', 'grant_type': 'grant_type2', 'assert_str': 'errcode2'}, {'name': 'third_case', 'appid': 'id3', 'secret': '', 'grant_type': 'grant_type3', 'assert_str': 'errcode3'}, {'name': 'forth_case', 'appid': 'id4', 'secret': 'secret4', 'grant_type': '', 'assert_str': 'errcode4'}, {'name': 'fifth_case', 'appid': 'id5', 'secret': 'secret5', 'grant_type': '', 'assert_str': 'errcode5'}, {'name': 'sixth_case', 'appid': 'id6', 'secret': 'secret6', 'grant_type': '', 'assert_str': 'errcode6'}]
3、编写YAML文件,其中动态变化的值以固定格式编写(这个可以自己定义,比如我是这样写的$csv{变量名}),看作是一个模板
- name: $csv{name}
request:
method: get
url: /cgi-bin/token
params:
appid: $csv{appid}
secret: $csv{secret}
grant_type: $csv{grant_type}
4、用yaml包提供的方法对YAML文件进行操作,找到固定格式的变量,取出该变量与JSON文件里的键匹配,如果匹配到了,用该键对应的值替换掉动态变化的值,接着写入到新的文件里
解析代码:
from contextlib import ExitStack
# yaml_file为YAML模板文件
# new_yaml_file为新生成的带有测试数据的YAML文件
def EnvReplaceYaml(yaml_file, new_yaml_file):
try:
with ExitStack() as stack:
yml_file = stack.enter_context(open(yaml_file, 'r+'))
yml_output = stack.enter_context(open(new_yaml_file, 'w'))
# 先读取YAML模板文件,返回值为字符串列表
yml_file_lines = yml_file.readlines()
# profileList的长度即为测试用例的数量
for i in range(0, len(profileList)):
# 循环遍历列表
for line in yml_file_lines:
new_line = line
# 如果找到以“$csv{”开头的字符串
if new_line.find('$csv{') > 0:
# 对字符串以“:”切割
env_list = new_line.split(':')
# 取“:”后面的部分,去掉首尾空格,再以“{”切割,再以“}”切割取出变量名称,比如“name”
env_name = env_list[1].strip().split('{', 1)[1].split('}')[0]
replacement = ""
# 如果name在字典列表的key里
if env_name in profileList[i].keys():
# 取出name对应的值赋给replacement
replacement = profileList[i][env_name]
# 用replacement替换掉YAML模板中的“$csv{name}”
for j in range(0, len(profileList)):
new_line = new_line.replace(env_list[1].strip(), replacement)
# 将new_line写入到yml_output文件里
yml_output.write(new_line)
yml_output.write("\n\n")
except IOError as e:
print("Error: " + format(str(e)))
raise
运行得到以下YAML文件:
- name: first_case
request:
method: get
url: /cgi-bin/token
params:
appid: id1
secret: secret1
grant_type: grant_type1
- name: second_case
request:
method: get
url: /cgi-bin/token
params:
appid:
secret: secret2
grant_type: grant_type2
- name: third_case
request:
method: get
url: /cgi-bin/token
params:
appid: id3
secret:
grant_type: grant_type3
- name: forth_case
request:
method: get
url: /cgi-bin/token
params:
appid: id4
secret: secret4
grant_type:
- name: fifth_case
request:
method: get
url: /cgi-bin/token
params:
appid: id5
secret: secret5
grant_type:
- name: sixth_case
request:
method: get
url: /cgi-bin/token
params:
appid: id6
secret: secret6
grant_type:
可以看到,CSV文件里有6条测试数据,在YAML文件里生成了6条测试用例。这样,无论CSV文件里有多少条数据,都可以生成相应数量的测试用例。对于做手工测试的同学来说,直接将测试数据写入CSV文件即可。