To use XMLTestRunner in your own Python test scripts, pass an instance of xmlrunner.XMLTestRunner to the testRunner argument of unittest.main().
Basic Usage (Output to directory)
By default, providing a string to output creates a directory for the reports.
import unittest
import xmlrunner
if __name__ == '__main__':
unittest.main(
testRunner=xmlrunner.XMLTestRunner(output='test-reports'),
failfast=False, buffer=False, catchbreak=False)
Reporting to a single file
To consolidate results into one specific file, pass a file object to the output parameter.
import unittest
import xmlrunner
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)
Running Doctests
XMLTestRunner can also be used to report on doctest suites.
import doctest
import xmlrunner
# ... (doctest definitions) ...
if __name__ == '__main__':
suite = doctest.DocTestSuite()
xmlrunner.XMLTestRunner().run(suite)