How many times does unittest run each test?

Josh English Joshua.R.English at gmail.com
Sat Aug 10 18:53:55 EDT 2013


On Saturday, August 10, 2013 1:40:43 PM UTC-7, Roy Smith wrote:
> In article <f7b24010-f3f4-4e86-b6c4-9ddb503d0412 at googlegroups.com>,
> 
>  Josh English  wrote:
> The first thing to do is get this down to some minimal amount of code 
> that demonstrates the problem.
> 
> 
> 
> For example, you drag in the logging module, and do some semi-complex 
> configuration.  Are you SURE your tests are getting run multiple times, 
> or maybe it's just that they're getting LOGGED multiple times.  Tear out 
> all the logging stuff.  Just use a plain print statement.
> 
> You've got two different TestCases here.  Does the problem happen with 
> just LoaderTC, or with just NameSpaceTC?
> 


Ok, then why would things get logged multiple times? The two test cases actually test a loader function that I could strip out, because it wasn't relevant. In these cases, the loader was being called with two different configurations in the individual setUp methods.

I left them there to show that in LoaderTC, there is one debug log coming from SimpleChecker, but in the NameSpaceTC, each debug message is printed twice. If I print statements on each test_ method, they are called once. 

As far as stripping down the code, I suppose 15 lines can be culled:

#---------
import logging

class SimpleChecker(object):
    def __init__(self,):
        self.logger = logging.getLogger(self.__class__.__name__)
        h = logging.StreamHandler()
        f = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
        h.setFormatter(f)
        self.logger.addHandler(h)

    def __call__(self, thing):
        self.logger.debug('calling %s' % thing)
        return thing in ['a','b','c']

import unittest

class LoaderTC(unittest.TestCase):
    def setUp(self):
        self.checker = SimpleChecker()

    def tearDown(self):
        del self.checker

    def test_callable(self):
        self.checker.logger.setLevel(logging.DEBUG)
        self.assertTrue(self.checker('q') is False, "checker accepted bad input")

class NameSpaceTC(unittest.TestCase):
    def setUp(self):
        self.checker = SimpleChecker()

    def tearDown(self):
        del self.checker

    def test_callable(self):
        print "testing callable"

        self.checker.logger.setLevel(logging.DEBUG)
        self.assertTrue(self.checker('a'), "checker did not accept good value")
        self.assertFalse(self.checker('f'), "checker accepted bad value")

if __name__=='__main__':
    unittest.main(verbosity=0)

#-----------
The output:

SimpleChecker - DEBUG - calling q
setting up NameSpace
testing callable
SimpleChecker - DEBUG - calling a
SimpleChecker - DEBUG - calling a
SimpleChecker - DEBUG - calling f
SimpleChecker - DEBUG - calling f
----------------------------------------------------------------------
Ran 2 tests in 0.014s

OK
Exit code:  False

Josh



More information about the Python-list mailing list