junitparser icon indicating copy to clipboard operation
junitparser copied to clipboard

Junit XML with TestSuites vs TestSuite as root element

Open guilhem-martin opened this issue 2 years ago • 3 comments

Hello,

As JUnit XML file inputs, I get sometimes TestSuite as root and sometimes TestSuites, which is as expected from the JUnit XSD file definition.

To be able to know in which case I'm, I do:

    junitxml = JUnitXml.fromfile("junit-testsuites.xml")
   # or junitxml = JUnitXml.fromfile("junit-testsuite.xml") without test suiteS element
    # Analyse the type of the root element
    instance_type = type(junitxml).__name__

Then depending on whether instance_type is JUnitXml or TestSuite, I'll parse differently.

Is this the proper way of dealing with the JUnit XML file root element variance when using junitparser module?

thanks.

guilhem-martin avatar Oct 13 '22 20:10 guilhem-martin

Alternative is to check junitxml._tag. I do:

suites = [suite for suite in (junitxml if junitxml._tag == "testsuites" else [junitxml])]

Junitparser could always return a TestSuites, even if the file itself contains a TestSuite as root.

Technically, this wouldn't even be a breaking change, but it should be considered one as behaviour for the same file changes.

EnricoMi avatar Oct 14 '22 05:10 EnricoMi

Thanks @EnricoMi , looks elegant.

guilhem-martin avatar Oct 14 '22 07:10 guilhem-martin

@weiwei what do you think about Junitparser always returning a TestSuites?

EnricoMi avatar Oct 14 '22 08:10 EnricoMi

Frankly I'm not sure. I think it eases things for me a bit? I'm open to suggestions though.

weiwei avatar Dec 26 '23 02:12 weiwei

Closing because this is rather old. But if needed we can reopen and discuss again.

weiwei avatar Dec 26 '23 06:12 weiwei

what do you think about Junitparser always returning a TestSuites?

IMHO it should always return a TestSuites object. Forcing the user to examine the returned object is not optimal.

jcbsv avatar Feb 16 '24 18:02 jcbsv

Alternative is to check junitxml._tag. I do:

suites = [suite for suite in (junitxml if junitxml._tag == "testsuites" else [junitxml])]

This should be sufficient:

suites = junitxml if junitxml._tag == "testsuites" else [junitxml]

jcbsv avatar Feb 16 '24 18:02 jcbsv