When writing unit tests, I sometimes cut and paste a test and don’t remember to change the method name. This results in overwriting the previous test, effectively hiding it and preventing it from running. For example;
class WidgetTestCase(unittest.TestCase):
def test_foo_should_do_some_behavior(self):
self.assertEquals(42, self.widget.foo())
def test_foo_should_do_some_behavior(self):
self.widget.bar()
self.assertEquals(314, self.widget.foo())
In this case, only the latter test would get called. Is there a way of programmatically catching this sort of error, short of parsing the raw source code directly?
If you run Pylint over your code, it will inform you when you have overwritten another method:
For example, I ran this:
In this online Pylint checker. Besides all the missing docstrings and such, I get this:
Alternatively, via the command line:
Output: