【问题标题】:pytest-html Customizing Result table with output from test resultspytest-html 使用测试结果的输出自定义结果表
【发布时间】:2019-08-05 07:25:24
【问题描述】:

我正在尝试使用 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 单元测试中调用 status_code 的值,最后一行的代码应该是什么。

我指的是下面的堆栈,但第二个选项不清楚要调用哪个函数。 How to add additional variable to pytest html report

【问题讨论】:

  • 刚刚阅读您的问题后,我得到了“如何从该结果表中删除列?”的问题的答案。谢谢! :)

标签: python pytest python-unittest pytest-html


【解决方案1】:

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

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))

结论

conftest.py 文件中report._status_code 中包含的值与测试文件中request.node._status_code 相同

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 2021-11-22
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 2022-10-24
    相关资源
    最近更新 更多