Search code examples
python-3.xattributeerrorpytest-html

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


In my PyTest, I have included conftest.py for customizing the HTML report. But I have the following error comes up while the test script tries to access the HTML report.

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

Solution

  • Use the following html import instead from py.xml import html

    Initially pycharm will not identify this import but this will not impact the execution. If you want you can change the pycharm settings to ignore this error