Search code examples
pythonnosetests

Nosetest overwrites xml output


I am having problems with nosestests writing to the unittext xml file twice.

I have this python script

import unittest

class aa(unittest.TestCase):
    def test_aa(self):
        self.assertTrue(True)


testlist = []

suite = unittest.TestLoader().loadTestsFromTestCase(aa)
testlist.append(suite)

allSuites = unittest.TestSuite(testlist)
unittest.TextTestRunner(verbosity=3).run(allSuites)

print 'done'

The problem is if I run it like this

nosetests  --with-xunit -s --verbosity=2 test.py 

I get this output

test_aa (test.aa) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
done
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

The problem is this writes to the nosetests.xml file twice. Is there any way to only make this run the test once?

The reason I need this is I have a unit test that basically runs fine the first time, then fails the second time and the xml file is getting overwritten with 0 tests run.

Cheers,
Paul


Solution

  • You are running tests twice, once during import (line with unittest.xx.run(allSuites)), and second time with nose once it discovers test_aa() test. If you are intending to use nose, you do not have to run tests in the script - nose will act as a test runner for you.

    You can run it once by running test with plain python your_test.py (not using nose), or by rewriting test to be something like and using your command as stated in question:

    import unittest
    
    class aa(unittest.TestCase):
        def test_aa(self):
            self.assertTrue(True)
    
    if __name__ == '__main__':
        # this will not run on import, only when tests are running with unittest with python
        testlist = []
    
        suite = unittest.TestLoader().loadTestsFromTestCase(aa)
        testlist.append(suite)
    
        allSuites = unittest.TestSuite(testlist)
        unittest.TextTestRunner(verbosity=3).run(allSuites)
    
        print 'done'