How to have one test suite per file in pytest’s junit XML

Currently, when I execute pytest cases via pytest . --junit-xml=test.xml, all the test cases of all files are merged into a single test suite. Something like:

<testsuites>
  <testsuite name="TestSuite Name" errors="0" failures="0" skipped="0" tests="2"
    time="0.161" timestamp="2024-01-19T12:24:42.091679">
    <testcase classname="testClassA" name="test_something"
      file="tests/test_class_a.py" line="98" time="0.015" />
    <testcase classname="testClassB" name="test_something_else"
      file="tests/test_class_b.py" line="98" time="0.015" />
  </testsuite>
</testsuites>

I would like to have multiple test suites (one per file) which is also generally supported by the JUnit XML format. So something like this:

<testsuites name="Name of my Test Suite">
  <testsuite name="Tests of file A" errors="0" failures="0" skipped="0" tests="2"
    time="0.161" timestamp="2024-01-19T12:24:42.091679" file="tests/test_class_a.py">
    <testcase classname="testClassA" name="test_something"
       line="98" time="0.015" />
  </testsuite>
  <testsuite name="Tests of file B" errors="0" failures="0" skipped="0" tests="2"
    time="0.161" timestamp="2024-01-19T12:24:42.091679" file="tests/test_class_a.py">
    <testcase classname="testClassB" name="test_something_else"
      line="98" time="0.015" />
  </testsuite>
</testsuites>

I couldn’t find anything on this in the docs. Is there a way to configure this in pytest?

Leave a Comment