zoukankan      html  css  js  c++  java
  • ddt源码修改:HtmlTestRunner报告依据接口名显示用例名字

    做接口测试,使用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 a
     6     string representation of the value, and convert the result into a valid
     7     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)
    复制代码

     

     修改完成之后,再次运行接口测试,就可以在测试报告当中看到对应的用例名字啦。。

     

     

    作者:Mr、北乐

    出处:https://www.cnblogs.com/beile/

    随便记录的,如果写的哪里不懂可加群交流,谢谢

  • 相关阅读:
    任意钝角三角形都可以分割为7个锐角三角形
    科场的胜者
    VBA按区导出电脑字库区位码
    A macro to get all interior colorindex has been used in thisworkbook
    和菜鸟一起学linux总线驱动之初识spi总线协议
    Android应用程序与SurfaceFlinger服务之间的共享UI元数据(SharedClient)的创建过程分析
    和菜鸟一起学android4.0.3源码之vibrator振动器移植心得
    opengl_NeNe 第九课,移动图像代码.vs 2010_express OPENGL 2.0
    和菜鸟一起学linux总线驱动之初识USB设备描述符
    和菜鸟一起学OK6410之熟悉内核源码
  • 原文地址:https://www.cnblogs.com/beile/p/10756827.html
Copyright © 2011-2022 走看看