I’m trying to add environment metadata to my pytest HTML report using pytest_configure, but accessing config.metadata throws an AttributeError.
How can I properly add metadata in pytest-html without causing this error?
I’m trying to add environment metadata to my pytest HTML report using pytest_configure, but accessing config.metadata throws an AttributeError.
How can I properly add metadata in pytest-html without causing this error?
I ran into this issue before. The AttributeError: ‘Config’ object has no attribute ‘metadata’ happens because config.metadata doesn’t exist until the pytest-html plugin has initialized it.
So if you try to access it too early in pytest_configure, it’s not yet available.
The correct way is to check if the metadata attribute exists first and then add your environment info:
def pytest_configure(config):
if hasattr(config, "metadata"):
config.metadata["Environment"] = "QA"
config.metadata["Browser"] = "Chrome"
This ensures you don’t access a missing attribute.
I also got the same error when I assumed config.metadata always exists.
pytest-html only adds the metadata attribute once it’s loaded, so adding a hasattr check in pytest_configure is the safest approach.
If you want to guarantee the metadata is available, you can use the pytest_html_report_title hook or pytest_html_results_summary hook instead.
These hooks run after pytest-html has initialized, so you can safely modify config.metadata there without worrying about AttributeError:
def pytest_html_results_summary(prefix, summary, config):
config.metadata["Environment"] = "QA"
This can be cleaner, especially if you have multiple plugins interacting with metadata.