I recently experimented with TDD while developing a GUI application in Python. I am very pleased that I have tests that test the functionality of my code, but it was difficult to execute some of the recommended TDD methods. Namely, writing tests at first was difficult. And it's hard for me to make my tests readable (due to the widespread use of the mocking library).
I chose a mocking library called mocker . I use it a lot since most of the code I'm testing calls (a) other methods in my application, which depend on the state of the system or (b) ObjC / Cocoa objects that cannot exist without an event loop, etc. d.
Anyway, I have many tests that look like this:
def test_current_window_controller():
def test(config):
ac = AppController()
m = Mocker()
ac.iter_window_controllers = iwc = m.replace(ac.iter_window_controllers)
expect(iwc()).result(iter(config))
with m:
result = ac.current_window_controller()
assert result == (config[0] if config else None)
yield test, []
yield test, [0]
yield test, [1, 0]
Note that these are actually three tests; all use the same parameterized test function. The code is checked here:
def current_window_controller(self):
try:
wc = self.iter_window_controllers().next()
except StopIteration:
return None
return wc
One of the things I've noticed with mocker is that at first itβs easier to write the application code and then go back and write the tests secondly, since most of the time I mock many method calls and the syntax to write the mocked calls much more verbose (therefore more difficult to write) than application code. Itβs easier to write application code and then simulate test code.
I find that with this testing method (and a bit of discipline) I can easily write code with 100% testing coverage.
, ? , ?
TDD , ?