【发布时间】: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