My Testing Theory
I figured I would start of this Test or Die month of January with the theories behind why and how I test. I am far from a “purist”, meaning what I care about most is that my butt is covered and that I can refactor until my little heart is content. I could care less about whether my unit tests hit the database and I really don’t want this emphasis on testing to become that.
In fact, my previous statement is really important and I’m going to say it again. I don’t want this emphasis on testing to turn into philosophical discussions on what is the true way. I want it to be down and dirty, in the mud, at the beginner level and then step slowly but surely up to intermediate and end where I am actually at (most likely not expert). I want it to be practical. I want people to learn the basics and not worry if they are doing it the “best” way. This month is all about starting.
That said, what are my beliefs? Well, like I said above, I like to cover my butt. I want to know if something breaks when I change something. If I update to the latest released version of Rails or I dare to ride on the edge, I want to know if the update broke anything in my application. I have heard some speak of testing as a contract. It is an agreement with your code that it will behave under certain terms and if it doesn’t, someone call the lawyer (or turn red in autotest).
Just for the heck of it, I’ll give you a little look at where I am at tool-wise. I started with good old test/unit. Then, a long time ago, in a windy city (literally, the windy city, Chicago), I attended a a chirb.org meeting on BDD with RSpec. I thought it was cool and loved the syntax, but wasn’t ready for a switch. test/spec came along and I greeted it with open arms. I used it for all of 2007 and part of 2008.
I don’t even remember what my first project was that used RSpec, but it was after RailsConf so it must have been June or July. I’ve used pretty much only RSpec since then. I drank the kool-aide and it went down smooth. Then, I started running into errors that my tests weren’t catching, I think, in part, because I was isolating too much and not testing enough. I realized that I needed to write more tests in RSpec to get the coverage I wanted but I didn’t feel it was worth all the extra effort. Again, just my opinion, you are free to disagree, but you are wrong. Just kidding. :)
Then, in December, after reading about the current state of ruby testing, I felt swung back to good old test/unit, so I gave context and stump a try and really liked it. They provide just enough of what I need from RSpec by adding it onto vanilla test unit. I haven’t started a new Rails app since trying them out, but my next one will most likely have them both included in my test helper. Enough with the history lesson.
Speaking of test coverage, what all do I think I’ll be covering in the next month? Well, truth is, I didn’t really think that far ahead, I just felt like testing was something I needed to push and hoped that the rest of the community would step up too. I’m actually amazed at the response thus far to be honest.
I would like to show the different options out there in a post or two and maybe elaborate a bit on my thoughts of them. I would like to run through all the basic contracts that one would want to have in their app. Simple tests like validations, associations, controllers, etc. From there, I will probably jump out of Rails and into plain old Ruby and show how I have begun to test my gems, specifically those that interact with other services and such.
That is really as far as I have thought ahead. I hope the discussions on each post are positive. I hope those in the know of testing help and encourage those that aren’t. I hope that this spurs those in the know that disagree with me to post their own articles and explain why I am wrong (and I will gladly link to them if you notify me). I just want to get all the opinions out there. I am fine if this takes longer than a month. Also, don’t expect every post for the next month or two to be about testing. I have some other stuff up my sleeve too.