SAK
SAK

Reputation: 25418

Initializing logger in python unit test

import logging

class TestMyClass(unittest.TestCase):
       def __init__(self):
            self.logger = logging.getLogger('MyClassLog')

       def setUp(self):

I am trying to instantiate the logger in the constructor. But I get this error: ... TypeError: init() takes exactly 1 argument (2 given)

Why is this? How to instantiate logger correctly?

Upvotes: 2

Views: 1316

Answers (4)

user1639698
user1639698

Reputation: 21

You may try this..

class TestMyClass(unittest.TestCase):
def __init__(self, methodName):
    super(TestMyClass, self).__init__(methodName)
    self.logger = logging.getLogger('MyClassLog')

Upvotes: 2

Sam Mussmann
Sam Mussmann

Reputation: 5983

setUpClass() should let you do initialization for all the tests in your test case, and should do it only once.

Upvotes: 1

dririan
dririan

Reputation: 305

You shouldn't have an __init__ method; do everything you need to do in the setUp method instead.

Upvotes: 2

Julian
Julian

Reputation: 3429

You are overriding __init__. You can't do that in a TestCase like that, because your test case is going to be instantiated by the test runner, and the argument passed to it there is going to be the method to run, so you've quashed all the initialization that needs to be done (and not taken the correct amount of arguments).

If you want to do some logging, you can do that in a setUp method, or more likely, just globally.

Upvotes: 3

Related Questions