Mocking __init__() for unittesting
You should use mock
package to mock the method __init__
of the class:
from mock import patch
def test_database_thing(self):
def __init__(self, dbName, user, password):
# do something else
with patch.object(DatabaseThing, '__init__', __init__):
# assert something
Instead of mocking, you could simply subclass the database class and test against that:
class TestingDatabaseThing(DatabaseThing):
def __init__(self, connection):
self.connection = connection
and instantiate that class instead of DatabaseThing
for your tests. The methods are still the same, the behaviour will still be the same, but now all methods using self.connection
use your test-supplied connection instead.