做接口測試,使用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)
?
?修改完成之后,再次運行接口測試,就可以在測試報告當中看到對應的用例名字啦。。
?
?