GENERAL COMMENTS
One purpose in writing this book was to aid you, the reader, in your quest to improve your skills and your code. Another purpose was to let me think through the issues involved in TDD, mainly by having to express them clearly and understandably. I've written this section very near the end of the process, and have taken the opportunity to read through the book, looking for things that I've learned or become more convinced of.
I have, for the most part, resisted the temptation to go back and revise the code in this book. As such, it serves as a fairly accurate picture of how the development went. And as such, there are a few things I would have done differently.
One thing is that I would have used mocks more during the development of the GUI. This was a very simple application so we incurred no real penalty from using the real application code behind the GUI in our tests. Had it been a more involved application, the tests would have been noticably slow. Mocks would help avoid that.
Another thingone more obvious and somewhat more embarrassing in hindsightjumped out and hit me as I read through the book. Think back to the first task, specifically the test for containment in the list. I'll replicate it here for convenience, hoping that my editors don't refactor away the duplication:
public void testContents() { assertTrue("List should contain starWars.", movieList.contains(starWars)); assertTrue("List should contain starTrek.", movieList.contains(starTrek)); assertFalse("List should not contain stargate.", movieList.contains(stargate)); }
There are two things wrong with this test:
Checking for the containment of two movies is redundant and doesn't add much, if anything.
There are three assertions in this single test. I feel even more strongly now that you should always strive for a single assertion per test. In a perfect world, setUp() would build the fixture, and each test method would make a single assertion about it. Test methods this simple cease to require the message parameter for the assert.
If I were to yield to temptation, I would replace the above testContains() with:
public void testContains() { assertTrue(movieList.contains(starWars)); }
public void testDoesNotContain() { assertFalse(movieList.contains(stargate)); }
|
No comments:
Post a Comment