Skip to content

Figure out and document alternative ways to have QA Sphere marker in junit xml with pytest #55

@satvik007

Description

@satvik007

We have a test case @src/tests/fixtures/junit-xml/pytest-style.xml

<?xml version="1.0" encoding="utf-8"?>
  <testsuites name="pytest tests">
    <testsuite name="pytest" errors="0" failures="1" skipped="0" tests="4" time="3.5" timestamp="2026-02-09T12:00:00.000000+00:00" hostname="test-host">
      <testcase classname="tests.login_test.TestLogin" name="TEST-002 test_valid_login[chromium]" time="0.649">
      </testcase>
      <testcase classname="tests.login_test.TestLogin" name="TEST-003 test_login_error[chromium-invalid_password]" time="0.625">
      </testcase>
      <testcase classname="tests.checkout_test.TestCheckout" name="TEST-004 test_checkout_counter[chromium-standard_user]" time="0.988">
      </testcase>
      <testcase classname="tests.inventory_test.TestInventory" name="TEST-006 test_inventory_page[chromium-standard_user]" time="0.584">
        <failure message="AssertionError: expected True but got False">self = &lt;tests.inventory_test.TestInventory object&gt;

      def test_inventory_page(self, page) -&gt; None:
  &gt;       assert page.title() == "Expected Title"
  E       AssertionError: expected True but got False

  tests/inventory_test.py:12: AssertionError</failure>
      </testcase>
      <testcase classname="tests.inventory_test.TestInventory" name="TEST-007 test_inventory_sort[chromium-standard_user]" time="0.734">
      </testcase>
    </testsuite>
  </testsuites>

where we have assumed that its possible to have the QA Sphere markers with dash (-) (e.g. TEST-002) are possible as prefix.

Claude pointed out that it might be possible to do this

pytest JUnit XML reports can have dashes in the name attribute. Here's how:

  1. Parametrize IDs (most common for dashes)
@pytest.mark.parametrize("user", ["std"], ids=["chromium-standard_user"])
def test_checkout_counter(self, user):
    ...

This produces test_checkout_counter[chromium-standard_user] in the XML — the part in brackets can contain dashes freely.

  1. The TEST-002 marker prefix

Python function names can't contain dashes, so the TEST-002 test_valid_login[chromium] format from your fixture requires prepending the marker to the test name. Common approaches:

  • Custom conftest hook that renames tests in the XML output:
# conftest.py
def pytest_itemcollected(item):
    marker = item.get_closest_marker("qas")
    if marker:
        item._nodeid = f"{marker.args[0]} {item.nodeid}"
  • with usage: @pytest.mark.qas("TEST-002")
  • Putting the marker directly in the test name using underscores like test_TEST_002_valid_login — but that uses underscores, not dashes.
  • A custom plugin / conftest that modifies record_property or uses the junit_family=xunit2 format.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions