Adding Xray metadata to Pytest
Some additional steps are required to get the required Xray metadata in the pytest output. This guide should help you get started.
Tagging tests with Xray test keys
Start by adding pytest markers to each test with its corresponding Xray test key. Only one Xray test key should be present for a single test.
For example, to tag a pytest test with the Xray test key XTP-123
, add the following pytest marker:
import pytest
@pytest.mark.test_key("XTP-123")
def test_example():
pass
Next, modify conftest.py
to override the pytest hook pytest_collection_modifyitems
to write these markers to the pytest output:
def pytest_collection_modifyitems(session, config, items):
for item in items:
for marker in item.iter_markers(name="test_key"):
test_key = marker.args[0]
item.user_properties.append(("test_key", test_key))
Now, when running pytest with the --junitxml=build/reports/pytest-junit.xml
flag,
the build/reports/pytest-junit.xml
file should contain the following:
<testcase classname="example_test" name="test_example" time="0.0009">
<properties>
<property name="test_key" value="XTP-123" />
</properties>
</testcase>
Tagging tests with Xray test plan keys
Start by adding pytest markers to each test with its corresponding Xray test plan key. Multiple Xray test plan keys can be present for a single test.
For example, to tag a pytest test with the Xray test plan keys XTP-456
and XTP-789
,
add the following pytest markers:
import pytest
@pytest.mark.test_key("XTP-123")
@pytest.mark.test_plan("XTP-456")
@pytest.mark.test_plan("XTP-789")
def test_example():
pass
Next, modify conftest.py
to override the pytest hook pytest_collection_modifyitems
to write these markers to the pytest output:
def pytest_collection_modifyitems(session, config, items):
for item in items:
for marker in item.iter_markers(name="test_key"):
test_key = marker.args[0]
item.user_properties.append(("test_key", test_key))
test_plan_keys = [marker.args[0] for marker in item.iter_markers(name="test_plan")]
if test_plan_keys:
item.user_properties.append(("test_plans", ",".join(test_plan_keys)))
Now, when running pytest with the --junitxml=build/reports/pytest-junit.xml
flag,
the build/reports/pytest-junit.xml
file should contain the following:
<testcase classname="example_test" name="test_example" time="0.0009">
<properties>
<property name="test_key" value="XTP-123" />
<property name="test_plans" value="XTP-456,XTP-789" />
</properties>
</testcase>