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

[{'answer_id': 42787, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 4, 'selected': True, 'text': '<p>Read <a href="https://rads.stackoverflow.com/amzn/click/com/0131177052" rel="noreferrer" rel="nofollow noreferrer">Working Effectively With Legacy Code by Feathers</a>.</p>\n\n<p>Jimmy Bogard has a <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/tags/Refactoring/default.aspx" rel="noreferrer">good blog series on SOC</a>.</p>\n'}, {'answer_id': 42808, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s <a href="http://web.archive.org/web/20080806072701/http://devver.net/blog/2008/07/tips-for-unit-testing/" rel="nofollow noreferrer">another great article</a> on testing.  In particular, a somewhat relevant quote from it:</p>\n\n<blockquote>\n  <p>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.</p>\n</blockquote>\n\n<p>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.</p>\n'}, {'answer_id': 42847, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 3, 'selected': False, 'text': '<p>The best way to retrofit an existing project without any unit tests is to do it when fixing bugs.  Write a test that fails on the logic that has the bug in it with the steps to reproduce the bug.  Then refactor the code until the tests pass.  Now you can have confidence that the bug is fixed and it will not be introduced later on in the cycle and you started introducing unit tests into the project.</p>\n'}, {'answer_id': 42865, 'author': 'xanadont', 'author_id': 1886, 'author_profile': 'https://Stackoverflow.com/users/1886', 'pm_score': 2, 'selected': False, 'text': "<p>Dale gets voted up.  Yes, there is no gain for adding unit tests to code that's working.  Lets say there are two unknown bugs X &amp; Y.  At some point X is revealed by typical field use.  You fix it, add a unit test, and move on.  Now lets assume Y is never uncovered over the entire lifetime of the program.  Since Y never revealed itself it's as if it never existed; no need to waste the resources.  Multiply this by hundreds or thousands of dormant bugs and you save yourself a great deal of superfluous maintenance.</p>\n"}, {'answer_id': 43345, 'author': 'Vagnerr', 'author_id': 3720, 'author_profile': 'https://Stackoverflow.com/users/3720', 'pm_score': 2, 'selected': False, 'text': '<p>If ever you are trying to add unit tests to old perl code I strongly recommend</p>\n\n<p><a href="https://rads.stackoverflow.com/amzn/click/com/0596100922" rel="nofollow noreferrer" rel="nofollow noreferrer">Perl Testing: A Developer\'s Notebook</a> by  Ian Langworth and chromatic.</p>\n\n<p>It has some very nice trick on testing legacy and "untestable" code.</p>\n'}, {'answer_id': 43370, 'author': 'dan gibson', 'author_id': 4495, 'author_profile': 'https://Stackoverflow.com/users/4495', 'pm_score': 2, 'selected': False, 'text': "<p>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?</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 63740, 'author': 'Kinjal Dixit', 'author_id': 6629, 'author_profile': 'https://Stackoverflow.com/users/6629', 'pm_score': 1, 'selected': False, 'text': '<p>Is it possible that we are in a panic and are getting confused between unit tests and performance tests?  Is it that your application works fine with few users, but starts throwing errors when under heavier load?  If so, unit tests are not the answer.  Unit tests != Load tests.</p>\n\n<p>If unit tests are in fact the answer, retrofitting unit tests is a good idea as it will help clean up the code.  Just be prepared to refactor a lot.  Code written with TDD turns out looking a lot different than code written without TDD.  In my case, I had a method HandleDisposition() which took care of a lot of cases.  This kind of method would not have existed if we had written the code with TDD.  When retrofitting unit tests, we refactored that function and now have methods like XDisposition(), YDisposition(), ZDisposition(), which are a lot easier to write unit tests against.</p>\n'}]
['https://Stackoverflow.com/questions/42785', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/381/']