<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="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>

[{'answer_id': 79667, 'author': 'etchasketch', 'author_id': 14640, 'author_profile': 'https://Stackoverflow.com/users/14640', 'pm_score': -1, 'selected': False, 'text': "<p>Unit tests are really useful when you refactor your code (ie. completely rewrite or move a module). As long as you have unit tests before you do the big changes, you'll have confidence that you havent forgotten to move or include something when you finish.</p>\n"}, {'answer_id': 80028, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 3, 'selected': False, 'text': '<p><strong>If you are writing your tests after you\'ve written your code and making them pass, you are not doing TDD</strong> (nor are you getting any benefits of Test-First or Test-Driven development.. check out SO questions for definitive books on TDD)</p>\n\n<blockquote>\n  <p>One of the things I\'ve noticed with\n  using mocker is that it\'s easier to\n  write the application code first and\n  then go back and write the tests\n  second, since most of the time I\'m\n  mocking many method calls and the\n  syntax to write the mocked calls is\n  much more verbose (thus harder to\n  write) than the application code. It\'s\n  easier to write the app code and then\n  model the test code off of that.</p>\n</blockquote>\n\n<p>Of course, its easier because you are just testing that the sky is orange after you made it orange by painting it with a specific kind of brush. \nThis is retrofitting tests (for self-assurance). Mocks are good but you should know how and when to use them - Like the saying goes \'When you have a hammer everything looks like a nail\' It\'s also easy to write a whole load of unreadable and not-as-helpful-as-can-be  tests. The time spent understanding what the test is about is time lost that can be used to fix broken ones. </p>\n\n<p>And the point is: </p>\n\n<ul>\n<li>Read <a href="http://martinfowler.com/articles/mocksArentStubs.html#ClassicalAndMockistTesting" rel="nofollow noreferrer">Mocks aren\'t stubs - Martin Fowler</a> if you haven\'t already. Google out some documented instances of good <a href="http://martinfowler.com/eaaDev/ModelViewPresenter.html" rel="nofollow noreferrer">ModelViewPresenter</a> patterned GUIs (Fake/Mock out the UIs if necessary). </li>\n<li>Study your options and choose wisely. I\'ll play the guy with the halo on your left shoulder in white saying \'Don\'t do it.\' Read this question as to <a href="https://stackoverflow.com/questions/59195/how-are-mocks-meant-to-be-used">my reasons</a> -  St. Justin  is on your right shoulder. I believe he has also something to say:) </li>\n</ul>\n'}, {'answer_id': 82049, 'author': 'Paweł Hajdan', 'author_id': 9403, 'author_profile': 'https://Stackoverflow.com/users/9403', 'pm_score': -1, 'selected': False, 'text': '<p><strong>Please remember that TDD is not a panaceum.</strong> It\'s hard, it\'s supposed to be hard, and it\'s especially hard to write mocking tests "in advance".</p>\n\n<p>So I would say - do what works for you. Even it\'s not "certified TDD". I do basically the same thing.</p>\n\n<p>You may want to provide your own API for GUI that would sit between controller code and GUI library code. That could be easier to mock, or you can even add some testing hooks to it.</p>\n\n<p>Last but not least, your code doesn\'t look too unreadable to me. Code using mocks is generally harder to understand. Fortunately in Python mocking is much easier and cleaner than i n other languages.</p>\n'}]
['https://Stackoverflow.com/questions/79454', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10840/']