1python-junit-xml 2================ 3.. image:: https://travis-ci.org/kyrus/python-junit-xml.png?branch=master 4 5About 6----- 7 8A Python module for creating JUnit XML test result documents that can be 9read by tools such as Jenkins. If you are ever working with test tool or 10test suite written in Python and want to take advantage of Jenkins' 11pretty graphs and test reporting capabilities, this module will let you 12generate the XML test reports. 13 14*As there is no definitive Jenkins JUnit XSD that I could find, the XML 15documents created by this module support a schema based on Google 16searches and the Jenkins JUnit XML reader source code. File a bug if 17something doesn't work like you expect it to.* 18 19Installation 20------------ 21 22Install using pip or easy_install: 23 24:: 25 26 pip install junit-xml 27 or 28 easy_install junit-xml 29 30You can also clone the Git repository from Github and install it manually: 31 32:: 33 34 git clone https://github.com/kyrus/python-junit-xml.git 35 python setup.py install 36 37Using 38----- 39 40Create a test suite, add a test case, and print it to the screen: 41 42.. code-block:: python 43 44 from junit_xml import TestSuite, TestCase 45 46 test_cases = [TestCase('Test1', 'some.class.name', 123.345, 'I am stdout!', 'I am stderr!')] 47 ts = TestSuite("my test suite", test_cases) 48 # pretty printing is on by default but can be disabled using prettyprint=False 49 print(TestSuite.to_xml_string([ts])) 50 51Produces the following output 52 53.. code-block:: xml 54 55 <?xml version="1.0" ?> 56 <testsuites> 57 <testsuite errors="0" failures="0" name="my test suite" tests="1"> 58 <testcase classname="some.class.name" name="Test1" time="123.345000"> 59 <system-out> 60 I am stdout! 61 </system-out> 62 <system-err> 63 I am stderr! 64 </system-err> 65 </testcase> 66 </testsuite> 67 </testsuites> 68 69Writing XML to a file: 70 71.. code-block:: python 72 73 # you can also write the XML to a file and not pretty print it 74 with open('output.xml', 'w') as f: 75 TestSuite.to_file(f, [ts], prettyprint=False) 76 77See the docs and unit tests for more examples. 78 79NOTE: Unicode characters identified as "illegal or discouraged" are automatically 80stripped from the XML string or file. 81 82Running the tests 83----------------- 84 85:: 86 87 # activate your virtualenv 88 pip install tox 89 tox 90 91