Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding unit tests to legacy code [closed]

Have you ever added unit tests, after the fact, to legacy code? How complicated was code and how difficult to stub and mock everything? Was the end result worthwhile?

like image 926
BuckeyeSoftwareGuy Avatar asked Oct 09 '09 02:10

BuckeyeSoftwareGuy


People also ask

How do I add unit tests to an existing Xcode project?

To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.


1 Answers

The best way, I have found, is to incrementally add the unit tests, not to just jump in and say we will now unit test the application.

So, if you are going to touch the code, for bug fixes or refactoring, then first write the unit tests. For bugs unit tests will help prove where the problem is, as you can duplicate it.

If refactoring, you will want to write unit tests, but you may find that the test is impossible to write, so you may need to find a high level, that calls the function that will be refactored, and unit test that part. Then, as you refactor the offensive function, write your tests so you can ensure that it is operating as it should.

There is no easy way to do this.

This question may help with more suggestions. How do you introduce unit testing into a large, legacy (C/C++) codebase?

like image 152
James Black Avatar answered Sep 24 '22 18:09

James Black