15.01.2013 Views

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5 - Unit Testing<br />

Why Wasn't I Unit Testing 3 Years Ago?<br />

For those <strong>of</strong> us who’ve discovered the joy <strong>of</strong> unit testing, it’s hard to understand why everyone isn’t<br />

doing it. For those who haven’t adopted it, you probably wish we’d shut up about it already. For many<br />

years I’d read blogs and speak to colleagues who were really into unit testing, but didn’t practice myself.<br />

Looking back, here’s why it took me a while to get on the bandwagon:<br />

1. I had misconception about the goals <strong>of</strong> unit testing. As I’ve already said, unit testing does<br />

improve the quality <strong>of</strong> a system, but it’s really all about making it easier to change / maintain the<br />

system later on. Furthermore, if you go to the next logical step and adopt Test Driven<br />

Development, unit testing really becomes about design. To paraphrase Scott Bellware, TDD isn't<br />

about testing because you're not thinking as a tester when doing TDD – you’re thinking as a<br />

designer.<br />

2. Like many, I used to think developers shouldn’t write tests! I don’t know the history behind this<br />

belief, but I now think this is just an excuse used by bad programmers. Testing is the process <strong>of</strong><br />

both finding bugs in a system as well as validating that it works as expected. Maybe developers<br />

aren’t good at finding bugs in their own code, but they are the best suited to make sure it works<br />

the way they intended it to (and clients are best suited to test that it works like it should (if<br />

you’re interested to find out more about that, I suggest you research Acceptance Testing and<br />

FitNess ). Even though unit testing isn’t all that much about testing, developers who don’t<br />

believe they should test their own code simply aren’t accountable.<br />

3. Testing isn’t fun. Sitting in front <strong>of</strong> a monitor, inputting data and making sure everything’s ok<br />

sucks. But unit testing is coding, which means there are a lot <strong>of</strong> challenges and metrics to gauge<br />

your success. Sometimes, like coding, it’s a little mundane, but all in all it’s no different than the<br />

other programming you do every day.<br />

4. It takes time. Advocates will tell you that unit testing doesn’t take time, it SAVES time. This is<br />

true in that the time you spend writing unit tests is likely to be small compared to the time you<br />

save on change requests and bug fixes. That’s a little too Ivory Tower for me. In all honesty, unit<br />

testing DOES take a lot <strong>of</strong> time (especially when you just start out). You may very well not have<br />

enough time to unit test or your client might not feel the upfront cost is justified. In these<br />

situations I suggest you identify the most critical code and test it as thoroughly as possible –<br />

even a couple hours spent writing unit tests can have a big impact.<br />

Ultimately, unit testing seemed like a complicated and mysterious thing that was only used in edge<br />

cases. The benefits seemed unattainable and timelines didn’t seem to allow for it anyways. It turns out it<br />

took a lot <strong>of</strong> practice (I had a hard time learning what to unit test and how to go about it), but the<br />

benefits were almost immediately noticeable.<br />

<strong>Foundations</strong> <strong>of</strong> <strong>Programming</strong> Copyright © <strong>Karl</strong> <strong>Seguin</strong> www.codebetter.com<br />

35

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!