Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting GUI code: should I use a mocking library?
    text
    copied!<p>Recently I've been experimenting with TDD while developing a GUI application in Python. I find it very reassuring to have tests that verify the functionality of my code, but it's been tricky to follow some of the recommened practices of TDD. Namely, writing tests first has been hard. And I'm finding it difficult to make my tests readable (due to extensive use of a mocking library).</p> <p>I chose a mocking library called <a href="http://labix.org/mocker" rel="nofollow noreferrer">mocker</a>. I use it a lot since much of the code I'm testing makes calls to (a) other methods in my application that depend on system state or (b) ObjC/Cocoa objects that cannot exist without an event loop, etc.</p> <p>Anyway, I've got a lot of tests that look like this:</p> <pre><code>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] </code></pre> <p>Notice that this is actually three tests; all use the same parameterized test function. Here's the code that is being tested:</p> <pre><code>def current_window_controller(self): try: # iter_window_controllers() iterates in z-order starting # with the controller of the top-most window # assumption: the top-most window is the "current" one wc = self.iter_window_controllers().next() except StopIteration: return None return wc </code></pre> <p>One of the things I've noticed with using mocker is that it's easier to write the application code first and then go back and write the tests second, since most of the time I'm mocking many method calls and the syntax to write the mocked calls is much more verbose (thus harder to write) than the application code. It's easier to write the app code and then model the test code off of that.</p> <p>I find that with this testing method (and a bit of discipline) I can easily write code with 100% test coverage.</p> <p>I'm wondering if these tests are good tests? Will I regret doing it this way down the road when I finally discover the secret to writing good tests?</p> <p>Am I violating the core principles of TDD so much that my testing is in vain?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload