All Projects → xmlrunner → Unittest Xml Reporting

xmlrunner / Unittest Xml Reporting

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

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Unittest Xml Reporting

pytest-spark
pytest plugin to run the tests with support of pyspark
Stars: ✭ 65 (-74.51%)
Mutual labels:  pytest, unittest
Green
Green is a clean, colorful, fast python test runner.
Stars: ✭ 691 (+170.98%)
Mutual labels:  unittest, test-runner
Testfx
MSTest V2 framework and adapter
Stars: ✭ 391 (+53.33%)
Mutual labels:  unittest, test-runner
Pytest Spec
Library pytest-spec is a pytest plugin to display test execution output like a SPECIFICATION.
Stars: ✭ 65 (-74.51%)
Mutual labels:  pytest, unittest
ci-testing-python
Sample Microservice App in Python for Testing using pytest, uber/doubles, tox on CI servers like Jenkins and Travis CI using Docker + Docker-Compose for test environment.
Stars: ✭ 37 (-85.49%)
Mutual labels:  jenkins, pytest
Seleniumbase
A Python framework that inspires developers to become better test automation engineers. 🧠💡
Stars: ✭ 2,520 (+888.24%)
Mutual labels:  jenkins, pytest
Pytest Ui
Text User Interface for running python tests
Stars: ✭ 23 (-90.98%)
Mutual labels:  pytest, test-runner
elm-doctest
doctest runner against Elm-lang source files
Stars: ✭ 13 (-94.9%)
Mutual labels:  test-runner, unittest
jwql
The James Webb Space Telescope Quicklook Application
Stars: ✭ 42 (-83.53%)
Mutual labels:  jenkins, pytest
podsearch bot
Telegram bot that searches Podcast in iTunes store.
Stars: ✭ 28 (-89.02%)
Mutual labels:  test-runner
pytest-reportlog
Replacement for the --resultlog option, focused in simplicity and extensibility
Stars: ✭ 36 (-85.88%)
Mutual labels:  pytest
cookiecutter-flask-react
Cookiecutter for a Flask+React project
Stars: ✭ 22 (-91.37%)
Mutual labels:  pytest
cnp-jenkins-library
Shared jenkins library
Stars: ✭ 21 (-91.76%)
Mutual labels:  jenkins
jframework
基于对spring boot的二次封装,目的是减少重复代码,提高开发效率
Stars: ✭ 88 (-65.49%)
Mutual labels:  jenkins
pipeline-as-code-with-jenkins
Pipeline as Code with Jenkins
Stars: ✭ 56 (-78.04%)
Mutual labels:  jenkins
flask-rest-api
This program shows how to set up a flaskrestapi with postgre db, blueprint, sqlalchemy, marshmallow, wsgi, unittests
Stars: ✭ 28 (-89.02%)
Mutual labels:  unittest
gosimhash
A simhasher for Chinese documents implemented by golang, simply translated from yanyiwu/gosimhash
Stars: ✭ 17 (-93.33%)
Mutual labels:  jenkins
parameterized-suite
Provides a new Runner for JUnit 4 that combines the features of Suite and Parameterized
Stars: ✭ 19 (-92.55%)
Mutual labels:  test-runner
Rspec junit formatter
RSpec results that your CI can read
Stars: ✭ 255 (+0%)
Mutual labels:  jenkins
fake-sftp-server-rule
A JUnit rule that runs an in-memory SFTP server.
Stars: ✭ 34 (-86.67%)
Mutual labels:  unittest

License Latest Version Development Status Documentation Status

Build Status 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.

Requirements

  • Python 3.5+
  • Please note Python 2.7 end-of-life was in Jan 2020, last version supporting 2.7 was 2.5.2
  • Please note Python 3.4 end-of-life was in Mar 2019, last version supporting 3.4 was 2.5.2
  • Please note Python 2.6 end-of-life was in Oct 2013, last version supporting 2.6 was 1.14.0

Limited support for unittest.TestCase.subTest

https://docs.python.org/3/library/unittest.html#unittest.TestCase.subTest

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

unittest also does not report successful sub-tests, so the accounting won't be exact.

Jenkins plugins

Jenkins JUnit plugin

This plugin does not perform XSD validation (at time of writing) and should parse the XML file without issues.

Jenkins xUnit plugin version 1.100

This plugin does perfom XSD validation and uses the more lax XSD. This should parse the XML file without issues.

Jenkins xUnit plugin version 1.104+

This plugin does perfom XSD validation and uses the more strict XSD.

See https://github.com/xmlrunner/unittest-xml-reporting/issues/209

import io
import unittest
import xmlrunner

# run the tests storing results in memory
out = io.BytesIO()
unittest.main(
    testRunner=xmlrunner.XMLTestRunner(output=out),
    failfast=False, buffer=False, catchbreak=False, exit=False)

Transform the results removing extra attributes.

from xmlrunner.extra.xunit_plugin import transform

with open('TEST-report.xml', 'wb') as report:
    report.write(transform(out.getvalue()))

JUnit Schema ?

There are many tools claiming to write JUnit reports, so you will find many schemas with minor differences.

We used the XSD that was available in the Jenkins xUnit plugin version 1.100; a copy is available under tests/vendor/jenkins/xunit-plugin/.../junit-10.xsd (see attached license).

You may also find these resources useful:

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

# basic sanity test, friendly output
$ tox -e pytest

# all combinations
$ tox
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].