python -ddt重写

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)
 

猜你喜欢

转载自www.cnblogs.com/1376402226-yuyu/p/9218227.html
ddt