Adding tests to legacy rails apps
This talk was excellent and provided some insight as to how we can start writing tests on the apps we haven’t done jack with when it comes to testing. Since this is one of my main goals for our team in the coming months, I found this talk quite informative. Some tidbits:
- Having no tests at all doesn’t mean you can’t start writing them.
- It’s really not that hard to get started writing tests. Rail scaffold will actually generate tests out of the box although the speaker implied they are not very good and might actually even break.
- Write a test every time you refactor
- If you are following the principal of putting that extra 10% of effort in to refactoring or improving some other portion of code at every single check-in, you have ample opportunity for writing beginning tests.
- Simply create a test that passes under the current code, then refactor the test, and ensure the test still passes.
- We should use RSpec to write tests
- Since we’re just getting started with TDD and writing tests at all, we should start with a tool that of people seem to think is superior to Test::Unit.
- RSpec allows people to write tests in a domain specific language that makes the tests read better and make more sense.
- There are some really handy tools available to help us test and help us test our tests.
- rake stats
- The rake stats output will show you a ratio of lines of code to lines of tests. You want to have a higher ratio because that means you’re writing a lot of tests.
- rcov
- Rcov is a neat tool that will run your tests and show you lines of code that were not tested.
- With an output similar to the cvsspam diff output that highlights lines of code that the whole body of your tests didn’t run.
- Heckle
- Heckle is tool that sounds pretty damn cool. Basically, it fucks up your code and then tells you if your tests failed like should have.
- Essentially, it changes variables, swaps if statements, and generally wreaks havoc on your code, one item at a time, running your tests each time.
- At the end, it tells you what tests still passed after stuff was changed and shows you what it changed. By examining this, you can determine what stuff might be tested properly.
- rake stats
I’m pretty anxious to start testing and I think I’ll try out some of these techniques on Find Mini Golf tonight.
