python -ddt重写

Posted 1376402226-yuyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
 

















以上是关于python -ddt重写的主要内容,如果未能解决你的问题,请参考以下文章

Python开发

Python 介绍

Python学习之认识python

python初识

Python 线程池模块,Python 多线程

如何切换python版本