I have just encountered a bug with my unit test, because I was essentially monkey patching in the set up like so:
def test_some_class_to_string():
some_class = SomeClass()
some_class.foo = 'bar'
some_class.monkey_patched = 'baz'
assert str(some_class) == 'barbaz'
class SomeClass(models.Model):
foo = models.CharField(max_length=100)
def __str__(self):
#monkey_patched property removed from model
return '{0}{1}'.format(self.foo, self.monkey_patched)
I had removed a property from SomeClass but the str method was still passing in the unit test due to re-adding monkey_patched at runtime.
Is it best to deal with this particular situation by calling str(some_class) before any property setup as an additional test, or should I always use keyword arguments to initialise classes in unit tests?