laurentb / xunitparser Goto Github PK
View Code? Open in Web Editor NEWmirror of git://git.p.engu.in/laurentb/xunitparser
Home Page: https://pypi.python.org/pypi/xunitparser
License: MIT License
mirror of git://git.p.engu.in/laurentb/xunitparser
Home Page: https://pypi.python.org/pypi/xunitparser
License: MIT License
=========== xunitparser =========== Description ----------- xunitparser reads a JUnit/XUnit XML file and maps it to Python objects. It tries to use the objects available in the standard ``unittest`` module. Usage ----- :: import xunitparser ts, tr = xunitparser.parse(open('/path/to/unit.xml')) ``ts`` is a ``TestSuite`` class, containing ``TestCase`` classes. ``tr`` is a ``TestResult`` class. You can change the classes used (though they probably would not work unless they inherit from the ``xunitparser`` ones) by using your own ``xunitparser.Parser`` class and changing the ``*_CLASS`` variables. Some helpful properties are added to the ``TestCase`` class:: for tc in ts: print('Class %s, method %s' % (tc.classname, tc.methodname)) if tc.good: print('went well...', 'but did not run.' if tc.skip else '') else: print('went wrong.') For more, please read the source code - it is very minimal. The classes also inherit from the `unittest`__ module so it is actually a good reference of what you can do with ``xunitparser``. __ http://docs.python.org/library/unittest.html Changes ------- + 1.3.0 - Multiple results in a single TestCase are seen as one. The previous way was never validated by real-life examples. - Handle system-out / system-err at the testsuite level Development ----------- Contributions can be sent in the form of git patches, to [email protected].
e.g. parsing these will fail:
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite name="myexample" timestamp="2015-06-24T11:31:08" time="0.01" errors="0" tests="1" skipped="0" failures="2">
<testcase classname="myexample" name="mytest" time="0.005">
<failure type="toBe" message="failed expectation #1"></failure>
<failure type="toBe" message="failed expectation #2"></failure>
</testcase>
</testsuite>
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite name="myexample" timestamp="2015-06-24T11:31:08" time="0.01" errors="2" tests="1" skipped="0" failures="0">
<testcase classname="myexample" name="mytest" time="0.005">
<error type="systemerror" message="system error #1"></error>
<error type="systemerror" message="system error #2"></error>
</testcase>
</testsuite>
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite name="myexample" timestamp="2015-06-24T11:31:08" time="0.01" errors="1" tests="1" skipped="0" failures="1">
<testcase classname="myexample" name="mytest" time="0.005">
<error type="systemerror" message="system error #1"></error>
<failure type="failure" message="could not make system call due to system error #1"></failure>
</testcase>
</testsuite>
however based on the schema defined for jUnit format, they are valid.
see also larrymyers/jasmine-reporters#102
<xs:sequence>
<xs:element ref="skipped" minOccurs="0" maxOccurs="1"/>
<xs:element ref="error" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="failure" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="system-out" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="system-err" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
Populated testsuite instance has a _cleanup flag set to True in python 3 and it's omitted in python 2
As result in python 3 final test suite looks like this:
<xunitparser.TestSuite tests=[None, None, None]>
Workaround is define custom testsuite class in parser:
import xunitparser
class TestSuite(xunitparser.TestSuite):
_cleanup = False
class MyParser(xunitparser.Parser):
TS_CLASS = TestSuite
parser = MyParser()
ts, tr = parser.parse(open('nosetests.xml'))
parser for the following junit test case results xml, will produce an AssertionError
<?xml version="1.0" encoding="utf-8"?>
<testsuite errors="1" failures="147" name="pytest" skips="1" tests="149" time="6.702">
<testcase classname="tests.functional.test_tasks" file="tests/functional/test_case.py" line="164" name="test_query" time="0.5505721569061279">
<system-out>
</system-out>
</testcase>
<testcase classname="tests.functional.test_tasks" file="tests/functional/test_case.py" line="424" name="TEST_CASE" time="0.0059661865234375">
<error message="test teardown failure"></error>
<system-out>
</system-out>
</testcase>
</testsuite>
The AssertionError is;
File "/home/jenkins/.pyenv/versions/3.6.3/lib/python3.6/site-packages/xunitparser.py", line 141, in parse
return self.parse_root(root)
File "/home/jenkins/.pyenv/versions/3.6.3/lib/python3.6/site-packages/xunitparser.py", line 163, in parse_root
assert len(list(ts)) == int(root.attrib['tests'])
AssertionError
That is because the tests count is off by errors count of tests. The fix for that assert line should be
if 'tests' in root.attrib:
# this is a fix for xunitparser AssertionError out when a test errors out
assert len(list(ts)) == int(root.attrib['tests']) - int(root.attrib.get('errors', '0'))
Hi there, thanks for the great lib ! I use it in genbadge
to generate a nice badge from junit reports.
However there is a known vulnerability in the stdlib XML parser: https://docs.python.org/3/library/xml.etree.elementtree.html
You should maybe try to import the ElementTree
from defusedxml
as recommended by the python software foundation, so that at least if people have it installed, they will be safe ?
I'll propose a PR in case you're interested
Hi there, it seems that this library cannot be installed anymore on setuptools>=58 because use_2to3
is not supported anymore.
(see https://stackoverflow.com/a/69100830/7262247)
See also smarie/python-genbadge#18
Best,
Sylvain
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.