码迷,mamicode.com
首页 > 编程语言 > 详细

python -ddt重写

时间:2018-06-23 20:55:00      阅读:733      评论:0      收藏:0      [点我收藏+]

标签:nsis   only   lis   ros   ror   mat   pen   not   ons   

 

ddt原始框架,生成的用例的名称比较冗长,我们可以对ddt框架该类方法进行重写

#对此方法进行重写

#raw code

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)
if not is_trivial(value):
return "{0}_{1}".format(name, index)
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)
重写后:
  

   # Add zeros before index to keep order
    index = "{0:0{1}}".format(index + 1, index_len)
    if not is_trivial(value):
        if type(value) is dic and "api_name" in value.keys():
            value=value["api_name"]
        else:
            return "{0}_{1}".format(name, index)
    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)
 

python -ddt重写

标签:nsis   only   lis   ros   ror   mat   pen   not   ons   

原文地址:https://www.cnblogs.com/1376402226-yuyu/p/9218227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!