Finally, I get the tests
I’ve had people tell me I need to write unit tests for a long time. And my “people” I mean people who write books and blogs. But I’ve never worked with these people before. I’ve never worked on a project that used unit tests. I’ve never actually had a real-live conversation with somebody that tried to convince me to write unit tests.
Lately I’ve been working on one my of own personal projects on the side, an ActionScript 3 Facebook API. It’s nice to be completely in charge of a project now and then; ultimately my client is me and that kind of makes the coding more fun. Sure there are other people who use the API (at least I hope there are) and if they want something different then they’ve got the code; it’s in their hands. But I do it my way, in my own time line.
But I wanted a way to ensure, as much as I could, that the code I was writing was working like I thought it was. I’m currently reading The Pragmatic Programmer and had just finished one of the sections on unit testing. I had already written a few thousand lines of code for this thing (with quite a few more to go . . . ) and I had always heard that the best time to start writing your tests was before you had written any code. But thinking better late then never I got started. I grabbed the latest build of FlexUnit (the AS3 version of jUnit), and got to work tossing a few tests together.
Nothing worked. None of my tests would pass. My API was working just fine so what was I doing wrong? What was wrong with my tests? I fiddled for quite a long time; setting up testing suites, changing which classes to extend, changing what I was asserting. Nothing worked! I was about to give up and spend my precious spare time doing some productive coding instead of bashing my head against the wall when I thought, “What if my code isn’t quite as perfect as I thought?”. Sure enough the login procedure wasn’t working quite like I thought it was. None of my tests were passing because my first “test”, logging into the Facebook server, was failing and I never knew it.
Armed with my new knowledge I reworked some of my logic and a few minutes later . . . test after test was passing! Green! Green! Green! Red!? Oh no, another test messed up? Nope. More bad logic I NEVER would have found. Each test took me only minutes to setup; a fraction of the time it was taking me to write what was being tested. And now I KNEW that what was was being tested was right.
I stayed up ’till 4 A.M. testing my code; I was THAT excited. I wrote as many assertions as I could think of, tried to come up with every edge case I could until I knew that my bits were rolling around the right way. Due to the nature of the API, I’m not able to test everything I want to, but I sure do feel a lot better about my code now. And it’s been quite some time since I stayed up until the wee hours of the morning writing code.
For some more details on how I did it, I suggest you check our my tests in the Facebook AS3 API over on Google code (not to be confused with Keith Salisbury’s excellent work). Darron Shall’s quick tutorial helped put me on the right track as well as the documentation in the FlexUnit code.
Finally I understand why people are so energetic about unit testing. It’s changing the way I code; it’s changing the way I THINK about code. I’ve got a lot to learn in the testing world, and I hope it stays a good habit. Tight deadlines could very easily get in the way of my progress and I don’t always have as much control of my tasks as I do for my personal projects. Wish me luck!