PYTEST-HTML自定义结果表具有从测试结果中输出的



我正在尝试使用pytest进行API自动化。我想将status_code作为使用pytest-html生成的报告HTML中的列之一。我在测试功能的一个变量中收集了status_code。但是如何将其传递到conftest中。

我的单元测试文件的代码低于代码。

class Test1(unittest.TestCase):
    def test1_cust_list_page(self):
        cust_list_resp = requests.post(BASE_URL+customer_list_ep,json=cust_page_payload,headers=headers,params=cust_list_params)
        print(cust_list_resp.status_code)
        status_code = cust_list_resp.status_code
        assert cust_list_resp.status_code==200

我的conftest文件具有以下代码:

from datetime import datetime
from py.xml import html
import pytest
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Status_code'))
    cells.insert(1, html.th('Time', class_='sortable time', col='time'))
    cells.pop()
@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.status_code))
    cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
    cells.pop()
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.status_code = str(item.function.)

如果我想从test1_cust_list_page单位测试中调用状态_code的值

我是参考以下堆栈,但是第二个选项尚不清楚要调用哪个函数。如何向Pytest HTML报告添加其他变量

在您的测试用例中使用额外参数,例如

def test1_cust_list_page(self, request)
     #---other piece of code written here---
     #assign the variable the value within the test as
     request.node._status_code=cust_list_resp.status_code
     #in the above statement you created a variable _status_code

现在在HTML结果表钩中

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    #access your variable like below
    cells.insert(2, html.td(report._status_code))

结论

report._status_code中包含的值。

最新更新