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