0

在我的 PyTest 中,我包含了用于自定义 HTML 报告的 conftest.py。但是当测试脚本尝试访问 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()
4

1 回答 1

0

使用以下 html 导入代替 from py.xml import html

最初 pycharm 不会识别此导入,但这不会影响执行。如果您愿意,可以更改 pycharm 设置以忽略此错误

于 2019-11-13T15:35:52.487 回答