替換ddt.py文件中的mk_test_name方法,注意 使用ddt的時(shí)候需要包含字段
case_name
,否則名稱還是下標(biāo)遞增,如下
def mk_test_name(name, value, index=0):
"""
Generate a new name for a test case.
It will take the original test name and append an ordinal index and a
string representation of the value, and convert the result into a valid
python identifier by replacing extraneous characters with ``_``.
We avoid doing str(value) if dealing with non-trivial values.
The problem is possible different names with different runs, e.g.
different order of dictionary keys (see PYTHONHASHSEED) or dealing
with mock objects.
Trivial scalar values are passed as is.
A "trivial" value is a plain scalar, or a tuple or list consisting
only of trivial values.
"""
# Add zeros before index to keep order
index = "{0:0{1}}".format(index + 1, index_len)
# 測(cè)試用例名稱讀取 case_name 字段的值 start --Victor
# 添加了對(duì)字典數(shù)據(jù)的處理冒冬。
if not is_trivial(value) and type(value) is not dict:
return "{0}_{1}".format(name, index)
# 如果數(shù)據(jù)是字典伸蚯,則獲取字典當(dāng)中的api_name對(duì)應(yīng)的值,加到測(cè)試用例名稱中简烤。
if type(value) is dict:
try:
value = value["case_name"] # case_name作為value值
except:
return "{0}_{1}".format(name, index)
# if not is_trivial(value): # 注釋原有方法
# return "{0}_{1}".format(name, index)
# 測(cè)試用例名稱讀取 case_name 字段的值 end --Victor
try:
value = str(value)
except UnicodeEncodeError:
# fallback for python2
value = value.encode('ascii', 'backslashreplace')
test_name = "{0}_{1}_{2}".format(name, index, value)
return re.sub(r'\W|^(?=\d)', '_', test_name)
# 獲取測(cè)試數(shù)據(jù)剂邮,
def get_testcase(filepath, index, module):
'''
參考:https://www.2cto.com/kf/201805/745595.html
:param filepath: 測(cè)試數(shù)據(jù)存放路徑
:param index: Execl 里面sheet(工作表)的下標(biāo) 從0開始
:param module: 指定sheet中用例模塊
:return:
'''
try:
file = xlrd.open_workbook(filepath)
sheet = file.sheets()[index - 1] # index 表單 默認(rèn)一個(gè)Excel表中只存儲(chǔ)一個(gè)表單 從0開始
nrows = sheet.nrows # 獲取表單行數(shù)
listdata = []
for i in range(1, nrows):
dict_canshu = {}
dict_canshu['module'] = sheet.cell(i, 0).value # 獲取每行第一列內(nèi)容
if dict_canshu['module'] == module: #
dict_canshu['case_name'] = sheet.cell(i, 1).value # 獲取每行第二列內(nèi)容,用做case的名字
dict_canshu.update(eval(sheet.cell(i, 2).value))
dict_canshu.update(eval(sheet.cell(i, 3).value))
listdata.append(dict_canshu)
logs.logger.info('獲取%s內(nèi)第%s個(gè)sheet(工作表)用例模塊為"%s"的測(cè)試數(shù)據(jù)' % (filepath, index,module))
logs.logger.info('測(cè)試數(shù)據(jù)listdata:%s' % listdata)
return listdata
except Exception as e:
logs.logger.error('獲取測(cè)試用例數(shù)據(jù)失敗横侦,原因:%s' % e)