Pytest 停止运行并出现 AttributeError(模块 'html' 在 pytest-html 中没有属性 'td')

Pytest stop runnig with AttributeError (module 'html' has no attribute 'td' in pytest-html)

在我的 PyTest 中,我包含了 conftest.py 用于自定义 HTML 报告。 但是当测试脚本试图访问 HTML 报告时出现以下错误。

"C:\Users\gobiraaj.anandavel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pluggy\callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "C:\Projects\TripTickAT\conftest.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:\Users\gobiraaj.anandavel\AppData\Local\Programs\Python\Python37-32\Scripts\pytest-script.py", line 11, in <module>
    load_entry_point('pytest==5.2.2', 'console_scripts', 'pytest')()
  File "C:\Users\gobiraaj.anandavel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytest-5.2.2-py3.7.egg\_pytest\config\__init__.py", line 
  File "C:\Projects\TripTickAT\conftest.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 设置以忽略此错误