Rails: Good process for adding tests retroactively?

I've got a few apps that I'd like to go back and retroactively build a test suite (RSpec & Cucumber) for, but it's a little daunting to get that process started.

What would your process be for going back on an existing app and building out a test suite for it?


I recently started tackling adding tests to a bunch of old code myself, and what I found immensely helpful is rcov (I don't bother with the rcov rails plugin and just cd to test and run a little shell script that runs rcov with the right exclusions and opens the report if the tests all pass.) Then, I started tackling the ones that were closest to 100% coverage and just working up the percentages bit by bit. It's much more measurable progress than, "Ugh, where do I start with adding tests for this?!"


I would go and add highlevel tests (cucumber) first. This will give you the confidence that the behavior won't change unnoticed. I wouldn't go and add rspec tests (or maybe just a few imporant ones) because you'll probably want to refactor a lot too.

Then, run metrics. MetricFu recently got a metric called "HotSpots", that will combine other metrics and point you to the biggest troublespots in the code. These places are usually also the most critical to your application. Fix them just enough so there readable and you get a good sense of what it's about. Don't go overboard just yet.

Then, for every new feature that you'll add, add specs and clean up some code that you're interacting with. So test and refactor the dependencies of new features, but don't go beyond that. Do it in tiny chunks or you'll lose hope quickly.


I've been doing a lot of this lately for client projects. The biggest blocks for me seem to be the rampant use of inline javascript with or without RJS. [Side note: there's a right and a wrong way to do AJAX, and most people are Doing It Wrong™.] I typically use cucumber heavily with a bit of rspec for strange unit tests.

The variables to consider are diverse, but a good place to start is with a few unit tests for your models. Create some factories and test your validations, along with any customized behavior you think needs testing.

If you're not into that or you've already got a suite of unit tests and you want to add integration, the next question is the extent to which you're doing a lot of inline javascript or RJS. If your app is very "ajaxy" you'll need to start with the selenium driver for cucumber, which is slow as molasses in february, but it'll get the job done. Once you've got a suite of tests covering the full functionality (or even just the important stuff) for your application, I'd begin refactoring the javascript to function unobtrusively.

Another direction you could go would be to build additional rspecs for your controllers and views, but I don't really like this pattern as you're getting into testing the implementation instead of the functionality.

The important thing to remember is that it doesn't all have to happen overnight. Analyze your workflows (eg logging in, doing task A, doing task B, etc) and determine which ones cover 80% of your typical use cases. Test those first. Then use something like metric_fu or just plain rcov (or any other coverage tool) and find areas of your code that are logic-dense and untested. I like metric_fu for this because the suite of tools it runs can give you both of those pieces of information.

链接地址: http://www.djcxy.com/p/49428.html

上一篇: 滚动时,Android listview图像消失

下一篇: Rails:追溯添加测试的好过程?