Publish unittest results from test discovery

Peter Otten __peter__ at web.de
Sat Aug 25 11:55:05 EDT 2012


Lucretiel wrote:

> So I've started using unittest, and I love it. I use testdiscovery (python
> -m unittest discover) so that I can distribute my tests and don't have to
> manage them all manually. I wanted to start publishing my test results to
> xml, though. I found xmlrunner by googling around, but it requires me to
> add an if __name__ == '__main__' block to my code, which isn't executed by
> unittest discover. Is there a way to get unittest disover to work with
> xmlrunner, or to some other way to solve this without restructuring all my
> test code?

I don't see where you could specify a test runner on the commandline, but 
you can reuse the discovery code in your own scripts. For the following 
example I basically copied unittest.__main__.py:

$ cat discover.py 
#!/usr/bin/env python
import xmlrunner

__unittest = True

from unittest.main import main, TestProgram, USAGE_AS_MAIN
TestProgram.USAGE = USAGE_AS_MAIN

main(module=None, testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
$ cat test_alpha.py 
import unittest

class T(unittest.TestCase):
    def test_alpha(self):
        pass
    def test_beta(self):
        self.assertEquals(["a", "b", "c"], ["a", "B", "c"])
$ ./discover.py discover

Running tests...
----------------------------------------------------------------------
.F
======================================================================
FAIL [0.001s]: test_beta (test_alpha.T)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/somewhere/over/the/rainbow/discover/test_alpha.py", line 7, in 
test_beta
    self.assertEquals(["a", "b", "c"], ["a", "B", "c"])
AssertionError: Lists differ: ['a', 'b', 'c'] != ['a', 'B', 'c']

First differing element 1:
b
B

- ['a', 'b', 'c']
?        ^

+ ['a', 'B', 'c']
?        ^


----------------------------------------------------------------------
Ran 2 tests in 0.002s

FAILED (failures=1)

Generating XML reports...
$ ls
discover.py  test_alpha.py  test_alpha.pyc  test-reports
$ ls test-reports/
TEST-test_alpha.T.xml
$ 





More information about the Python-list mailing list