Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you retrofit unit tests into a code base?

Do you have any strategies for retrofitting unit tests onto a code base that currently has no unit tests ?

like image 617
David Avatar asked Sep 03 '08 23:09

David


2 Answers

Why do you want to add unit tests? Do you feel the code has bugs? Do you just want something to do? Are you about to embark on a new feature?

If it is an older product that has been released for quite some time then I'd agree with the others and only add the tests when I find a bug or add a new feature.

If it is a product that is still being developed and not released or only recently released, then I'd start by reviewing the code. If I saw something not quite right then I'd add a test for it. I'd probably make some tests to create some sample data. Creating sample data seems to offer quite a bang for your buck, and it can be useful too.

I think there is benefit to writing the tests even when you don't have a bug to test - when you're adding new features or fixing bugs later, your tests confirm that you haven't introduced new bugs.

like image 119
dan gibson Avatar answered Oct 02 '22 11:10

dan gibson


Here's another great article on testing. In particular, a somewhat relevant quote from it:

Here’s a terrible idea - decide you are going to spend a whole week building a test suite for your project. First of all, you’ll likely just get frustrated and burn out on testing. Secondly, you’ll probably write bad tests at first, so even if you get a bunch of tests written, you’re going to need to go back and rewrite them one you figure out how slow, brittle, or unreadable they are.

I think you are better off building tests 1 at a time as you are fixing bugs or adding new functionality... don't try to build missing test cases, you should have an end goal for each test, rather than just to improve coverage.

like image 40
Mike Stone Avatar answered Oct 02 '22 12:10

Mike Stone