Git Product home page Git Product logo

xunitparser's Introduction

===========
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].

xunitparser's People

Contributors

dnozay avatar joshuagoodson-wf avatar kevinfrommelt avatar laurentb avatar uucidl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xunitparser's Issues

xunitparser does not handle multiple <failure> or <error> per <testcase>

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

see also https://svn.jenkins-ci.org/trunk/hudson/dtkit/dtkit-format/dtkit-junit-model/src/main/resources/com/thalesgroup/dtkit/junit/model/xsd/junit-4.xsd

            <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>

All tests are None in TestSuite in Python 3

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'))

xunittest.Parser() assertion error when a test case error out

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'))

Security vulnerability: use safe ElementTree from defusedxml instead

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.