Skip to content

junitxml: file attribute uses backslashes on Windows (xunit1/legacy) #15453

Backlog

Description

@surkuar

With junit_family=xunit1 (or legacy), the file attribute of <testcase> uses the OS path separator. On Windows it contains backslashes, while the nodeid for the same test uses /:

tests/sub/test_plain.py::test_one
<testcase classname="tests.sub.test_plain" name="test_one" file="tests\sub\test_plain.py" line="0" time="0.000" />

On Linux/macOS the same run gives file="tests/sub/test_plain.py". So one test suite produces different file values depending on the OS that ran it. Tools that read this attribute (CI test-splitting by timing, test-result dashboards, or anything matching results back to files in the repo) have to normalise it themselves or the paths won't match across runners.

I ran into this while working on tooling that reads pytest JUnit XML from CI runs on several OSes. I noticed it while looking into #9388, which is about the same attribute but a different problem.

Cause

_NodeReporter.record_testreport in src/_pytest/junitxml.py sets "file": location[0]. That value comes from Item.location, which builds the path with bestrelpath, so it uses os.sep. That's fine for terminal output, but the XML is a file that gets passed between systems.

Expected

file uses / on every platform, the same as the nodeid. Using nodes.SEP in junitxml only, without changing Item.location, would keep terminal output on Windows as it is.

I'd be happy to send a PR with a test if that sounds right.

Minimal example

# tests/sub/test_plain.py
def test_one():
    assert True
pytest -o junit_family=xunit1 --junit-xml=out.xml tests

xunit2 (the default) doesn't write file, so it's not affected.

Versions

  • pytest 9.2.0 (same code on main)
  • Python 3.15.2
  • Windows 11 (10.0.26300)
Package   Version
--------- -------
colorama  0.4.6
iniconfig 2.3.1
packaging 26.4
pip       26.2.2
pluggy    1.7.0
Pygments  2.21.0
pytest    9.2.0

Activity

  1. added a commit that references this issue on Nov 25, 2026
    c06ded0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions