在我的PyTest中,我包含了用于自定义HTML报告的conftest.py。但是,当测试脚本尝试访问HTML报告时,出现了以下错误。
"C:Usersgobiraaj.anandavelAppDataLocalProgramsPythonPython37-32libsite-packagespluggycallers.py", line 187, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "C:ProjectsTripTickATconftest.py", line 14, in pytest_html_results_table_row
INTERNALERROR> cells.insert(2, html.td(report.status_code))
INTERNALERROR> AttributeError: module 'html' has no attribute 'td'
Traceback (most recent call last):
File "C:Usersgobiraaj.anandavelAppDataLocalProgramsPythonPython37-32Scriptspytest-script.py", line 11, in <module>
load_entry_point('pytest==5.2.2', 'console_scripts', 'pytest')()
File "C:Usersgobiraaj.anandavelAppDataLocalProgramsPythonPython37-32libsite-packagespytest-5.2.2-py3.7.egg_pytestconfig__init__.py", line
File "C:ProjectsTripTickATconftest.py", line 8, in pytest_html_results_table_header
cells.insert(2, html.th('Status_code'))
AttributeError: module 'html' has no attribute 'th'
conftest.py
from datetime import datetime
import html.parser
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()
使用以下html导入从py.xml导入html
最初pycharm不会识别此导入,但这不会影响执行。如果你愿意,你可以更改pycharm设置以忽略此错误