做接口测试,使用unittest+ddt+excel ,使用HtmlTetstRunner来生成测试用例。
查看报告的时候 用例名称都是 test_api_1 、test_api_2 、test_api_3 的显示 ,看的不爽,也不明确,如果是test_api_登陆成功 、 test_api_密码错误
这样是不是就很好去看这个报告了。不懂代码的也知道哪条通过,哪条不通过,可以对应去处理 ,而不是去excel数行数
======================================分割线==================================
查看了ddt源码,代表用例名称的函数叫:mk_test_name
源码啥样的就不讲了,简单粗暴直接复制粘贴
ddt源码如下(红色粗体部分标识):
1 def mk_test_name(name, value, index=0):2 """3 Generate a new name for a test case.4 5 It will take the original test name and append an ordinal index and a6 string representation of the value, and convert the result into a valid7 python identifier by replacing extraneous characters with ``_``.8 9 We avoid doing str(value) if dealing with non-trivial values.
10 The problem is possible different names with different runs, e.g.
11 different order of dictionary keys (see PYTHONHASHSEED) or dealing
12 with mock objects.
13 Trivial scalar values are passed as is.
14
15 A "trivial" value is a plain scalar, or a tuple or list consisting
16 only of trivial values.
17 """
18
19 # Add zeros before index to keep order
20 index = "{0:0{1}}".format(index + 1, index_len)
21 if not is_trivial(value):
22 return "{0}_{1}".format(name, index)
23 try:
24 value = str(value)
25 except UnicodeEncodeError:
26 # fallback for python2
27 value = value.encode('ascii', 'backslashreplace')
28 test_name = "{0}_{1}_{2}".format(name, index, value)
29 return re.sub(r'\W|^(?=\d)', '_', test_name)
s上面是我的用例模板 ,想显示哪个就填哪个,看下用例应该懂了吧,不懂的加群问
2、修改ddt源码,显示测试用例名字 ,如果传入数据为元组或字典。把下列代码替换源码红色区
if not is_trivial(value) and type(value) is not dict:
return "{0}_{1}_{2}".format(name, index,value.title)
if type(value) is dict:
try:
value = value["title"]
except:
return "{0}_{1}".format(name.index)
修改完成之后,再次运行接口测试,就可以在测试报告当中看到对应的用例名字啦。。