Search code examples
xmlpytestconftest

Adding multiple XML attributes to a pytest xml in conftest.py


I have multiple pytest scripts that run independently, I want to build a mechanism to add xml attributes to the tests in the script to run in the conftest.py script on collection. So far i've found online a call :

request.config._xml.node_reporter(item.nodeid).add_attribute('spam', 'eggs')

that can be run in the pytest_collection_modifyitems hook . but when i try to run this i get:

NameError: name 'request' is not defined

Error Any idea what am i missing?


Solution

  • Dug some more in the documentation and found that if you add this to the conftest.py:

    import pytest
    
    @pytest.fixture(autouse=True)
    def record_index(record_xml_attribute):
        record_xml_attribute('index', '15')
    

    it does exactly what i need