Skip to content

junitxml: Incorrect (?) file reported for inherited tests #9388

Open

Description

@tpict

When a class A inherits tests from a class B in another file, the test cases are reported as belonging to the file that houses class B.

Example:

# example/base.py

class Base:
    def test_something(self):
        assert 1 == 1
# example/test_something.py
from unittest import TestCase

from .base import Base

class MyTest(Base, TestCase):
    pass
> pytest -o junit_family=xunit1 --junit-xml=test.xml example/test_something.py
> cat test.xml
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
  <testsuite name="pytest" errors="0" failures="0" skipped="0" tests="1" time="0.039" timestamp="2021-12-07T09:54:49.284569" hostname="hello">
    <testcase classname="example.test_something.MyTest" name="test_something" file="example/base.py" line="1" time="0.001" />
  </testsuite>
</testsuites>

I'm not sure if this is correct or not. In CircleCI, it prevents the "split tests by timing" feature from working correctly because it cannot find e.g. example/test_something.py in the XML doc. Is this intended? Is there a better method for inheriting tests?

> pip freeze
astroid==2.9.0
attrs==21.2.0
iniconfig==1.1.1
isort==5.10.1
lazy-object-proxy==1.6.0
mccabe==0.6.1
packaging==21.3
platformdirs==2.4.0
pluggy==1.0.0
py==1.11.0
pyparsing==3.0.6
pytest==6.2.5
toml==0.10.2
typing-extensions==4.0.1
wrapt==1.13.3

Activity

  1. added
    plugin: junitxml
    topic: collection
    type: question
    on Dec 9, 2021
  2. tpict commented on Dec 9, 2021

    @tpict
    Author

    Not strictly related but I figure anyone with domain-specific knowledge can answer this too: functional tests have their classname set to the module containing them, not the function itself. This means that Circle cannot divide these tests any more granularly than by file, whereas class-based tests can be split by class (because the classname points to the class). Any idea why the discrepancy?

  3. surkuar commented on Nov 25, 2026

    @surkuar

    This still happens on pytest 9.2.0, and not only with unittest.TestCase. A plain pytest class that inherits its tests behaves the same way:

    # example/test_something.py
    from .base import Base
    
    class TestPlain(Base):
        pass
    <testcase classname="example.test_something.TestPlain" name="test_something" file="example\base.py" line="1" ... />
    

    The nodeid is example/test_something.py::TestPlain::test_something, but file points to base.py. That happens because junitxml takes file and line from item.location, which is where the function is defined, not where it was collected.

    I work on tooling that reads pytest's JUnit XML and maps test results back to files. For that, and for CircleCI-style splitting by timing, the collected module is what you want, since that's the file you pass on the command line.

    On Windows the path also uses backslashes (example\base.py) even though the nodeid uses /, so the same test can show a different file depending on the OS.

    Would you accept a PR that makes junitxml take file from the nodeid's path part (so it's the collected module and always uses /)? The open question is line: it's the line in base.py, so it would no longer match file. I can see three options: drop line when the function is defined in another file, keep it as is, or leave file alone and only normalise the separator. Happy to go with whichever you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

plugin: junitxmltopic: collectiontype: question

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions