I have a Python class and its methods are partially covered by unit tests like this:
class MyClass:
def __init__(self):
self.instancevar1 = None # some methods change this value
self.instancevar2 = None # some methods change this value
...
def method1(self, input_data):
...
<self.instancevar1 and self.instancevar2 are used here>
<return True or False>
...
class TestMyClass(unittest.TestCase):
def test_method1(self):
mc = MyClass()
input_data = 'foo'
self.assertEqual(mc.method1(input_data), True)
The tests are passing for simple cases like this.
I also have other classes that use instances of MyClass like this:
class MyOtherClass:
def __init__(self):
self.mc = MyClass()
Now, in some method of MyOtherClass, I have a situation like this:
class MyOtherClass:
...
def some_method(self):
...
result = self.mc.method1('foo') # result is expected to be True, but it is False
I think I understand the reason why this might happen. In the test case, I instantiate MyClass and call the test. In MyOtherClass, self.mc is referenced multiple times, misc methods of it are called and they probably change the internal state of the object (some values of instance variables), thus producing a different return value.
What are the suggested best practices for debugging such issues?
The fact that I have some tests for MyClass is good, but doesn't seem to help much here. Does this example contain some design flaws which cause my problems?