pytest自定义动态添加描述信息

先上效果图:

 

修改pytest-html报告,分三部分.

pytest执行目录新建conftest.py文件

import pytest
from py._xmlgen import html
from datetime import datetime
"""
Summary部分在此设置
"""
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
    #Get configure content.
    prefix.extend([html.p("测试人: 测试组")])



"""
Environment部分在此设置
"""
def pytest_configure(config):
    
    config._metadata['测试地址'] = xxxxxxxx#


"""
Results部分在此设置.
"""
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))
    cells.insert(3, html.th('Time', class_='sortable time', col='time'))
    # cells.insert(1,html.th("Test_nodeid"))
    cells.pop()

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))
    cells.insert(3, html.td(datetime.utcnow(), class_='col-time'))
    # cells.insert(1,html.td(report.nodeid))
    cells.pop()

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
    report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")   #设置编码显示中文

 

下面说一下怎么样动态更改描述部分:

pytest-html默认获取的是测试方法的__doc__属性,也就是,测试函数下的注释   如下的"""  """中的内容.

    def data(self, request):
        """
        fixture parameters.
        """

 

要动态传参__doc__内容也是可以的.可以通过__doc__动态修改描述.

普通方法:   方法名.__doc__='fixture parameters.'

实例方法:   self.方法名.__func__.__doc__='fixture parameters.'     实例方法必须加__func__否则是只读的.

class TestCaseExecution(object):
    """
    Use the python pytest framework.
    """
    def setup_class(self):
        pass
    

    def teardown_class(self):
        pass


    param_list = load_case_data()

    
    @pytest.fixture(scope='session', params=param_list)
    def data(self, request):
        """
        fixture parameters.
        """
        return request.param
    
    def testcase(self, data):
        self.testcase.__func__.__doc__ = data[0]['Desc']
        #Execution the YAML test case.
        exec_test_case(data)
        

 

使用此方法,动态传入描述.

self.testcase.__func__.__doc__ = data[0]['Desc']

 

转载于:https://www.cnblogs.com/yhleng/p/11225638.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值