Skip to main content

unittest-based test runner with Ant/JUnit like XML reporting.

Project description

License Latest Version Development Status Documentation Status

Build Status Code Health codecov.io Coverage Status Coveralls Coverage Status Requirements Status

unittest-xml-reporting (aka xmlrunner)

A unittest test runner that can save test results to XML files in xUnit format. The files can be consumed by a wide range of tools, such as build systems, IDEs and continuous integration servers.

Schema

There are many schemas with minor differences. We use one that is compatible with Jenkins xUnit plugin, a copy is available under tests/vendor/jenkins/xunit-plugin/junit-10.xsd (see attached license). You may also find these resources useful:

Things that are somewhat broken

Python 3 has the concept of sub-tests for a unittest.TestCase; this doesn't map well to an existing xUnit concept, so you won't find it in the schema. What that means, is that you lose some granularity in the reports for sub-tests.

Requirements

  • Python 2.7 or 3.3+
  • Please note Python 2.6 end-of-life was in Oct 2013, last version supporting 2.6 was 1.14.0

Installation

The easiest way to install unittest-xml-reporting is via Pip:

$ pip install unittest-xml-reporting

If you use Git and want to get the latest development version:

$ git clone https://github.com/xmlrunner/unittest-xml-reporting.git
$ cd unittest-xml-reporting
$ sudo python setup.py install

Or get the latest development version as a tarball:

$ wget https://github.com/xmlrunner/unittest-xml-reporting/archive/master.zip
$ unzip master.zip
$ cd unittest-xml-reporting
$ sudo python setup.py install

Or you can manually download the latest released version from PyPI.

Command-line

python -m xmlrunner [options]
python -m xmlrunner discover [options]

# help
python -m xmlrunner -h

e.g.

python -m xmlrunner discover -t ~/mycode/tests -o /tmp/build/junit-reports

Usage

The script below, adapted from the unittest, shows how to use XMLTestRunner in a very simple way. In fact, the only difference between this script and the original one is the last line:

import random
import unittest
import xmlrunner

class TestSequenceFunctions(unittest.TestCase):

    def setUp(self):
        self.seq = list(range(10))

    @unittest.skip("demonstrating skipping")
    def test_skipped(self):
        self.fail("shouldn't happen")

    def test_shuffle(self):
        # make sure the shuffled sequence does not lose any elements
        random.shuffle(self.seq)
        self.seq.sort()
        self.assertEqual(self.seq, list(range(10)))

        # should raise an exception for an immutable sequence
        self.assertRaises(TypeError, random.shuffle, (1,2,3))

    def test_choice(self):
        element = random.choice(self.seq)
        self.assertTrue(element in self.seq)

    def test_sample(self):
        with self.assertRaises(ValueError):
            random.sample(self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assertTrue(element in self.seq)

if __name__ == '__main__':
    unittest.main(
        testRunner=xmlrunner.XMLTestRunner(output='test-reports'),
        # these make sure that some options that are not applicable
        # remain hidden from the help menu.
        failfast=False, buffer=False, catchbreak=False)

Reporting to a single file

if __name__ == '__main__':
    with open('/path/to/results.xml', 'wb') as output:
        unittest.main(
            testRunner=xmlrunner.XMLTestRunner(output=output),
            failfast=False, buffer=False, catchbreak=False)

Doctest support

The XMLTestRunner can also be used to report on docstrings style tests.

import doctest
import xmlrunner

def twice(n):
    """
    >>> twice(5)
    10
    """
    return 2 * n

class Multiplicator(object):
    def threetimes(self, n):
        """
        >>> Multiplicator().threetimes(5)
        15
        """
        return 3 * n

if __name__ == "__main__":
    suite = doctest.DocTestSuite()
    xmlrunner.XMLTestRunner().run(suite)

Django support

In order to plug XMLTestRunner to a Django project, add the following to your settings.py:

TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'

Also, the following settings are provided so you can fine tune the reports:

setting default values description
TEST_OUTPUT_VERBOSE 1 0|1|2 Besides the XML reports generated by the test runner, a bunch of useful information is printed to the sys.stderr stream, just like the TextTestRunner does. Use this setting to choose between a verbose and a non-verbose output.
TEST_OUTPUT_DESCRIPTIONS False True|False If your test methods contains docstrings, you can display such docstrings instead of display the test name (ex: module.TestCase.test_method).
In order to use this feature, you have to enable verbose output by setting TEST_OUTPUT_VERBOSE = 2.
Only effects stdout and not XML output.
TEST_OUTPUT_DIR "." <str> Tells the test runner where to put the XML reports. If the directory couldn't be found, the test runner will try to create it before generate the XML files.
TEST_OUTPUT_FILE_NAME None <str> Tells the test runner to output a single XML report with this filename under os.path.join(TEST_OUTPUT_DIR, TEST_OUTPUT_FILE_NAME).
Please note that for long running tests, this will keep the results in memory for a longer time than multiple reports, and may use up more resources.

Contributing

We are always looking for good contributions, so please just fork the repository and send pull requests (with tests!).

If you would like write access to the repository, or become a maintainer, feel free to get in touch.

Testing changes with tox

Please use tox to test your changes before sending a pull request. You can find more information about tox at https://testrun.org/tox/latest/.

$ pip install tox
$ tox

Work in progress

There is a rewrite branch which contains better unit tests and uses a different runner. It introduces a CompositeTestResult which allows the use of both TextTestResult and XMLTestResult as delegates. If you would like to help reconcile both branches, pull requests are welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unittest-xml-reporting-2.3.2.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

unittest_xml_reporting-2.3.2-py2.py3-none-any.whl (18.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file unittest-xml-reporting-2.3.2.tar.gz.

File metadata

  • Download URL: unittest-xml-reporting-2.3.2.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for unittest-xml-reporting-2.3.2.tar.gz
Algorithm Hash digest
SHA256 0b16cf177400d2df88550d4bc1ae1116cde7cd5f9825daf3a9c3558756497d06
MD5 d571e3d9b93a9a8b9e4437c0ab5d4363
BLAKE2b-256 2d9a9c46665c6d6673774bf24a4e45f69a561d26406f8a8d2771e13d3a538569

See more details on using hashes here.

File details

Details for the file unittest_xml_reporting-2.3.2-py2.py3-none-any.whl.

File metadata

  • Download URL: unittest_xml_reporting-2.3.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for unittest_xml_reporting-2.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8198222445e1d19d9c1250d61fa1004be6a44729be6e6f264427e88eb7bb37f6
MD5 92a6349dd652d9b3603aa9ffd091450d
BLAKE2b-256 d3ac7a345328b3319b65850566ec547f07b9effc9a3f35fc3db22f89306e30e5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page