views:

214

answers:

5

I'm about to embark on a bout of refactoring of some functions in my code. I have a nice amount of unit tests that will ensure I didn't break anything, but I'm not sure about the coverage they give me. Are there any tools that can analyze the code and see that the functionality remains the same?

I plan to refactor some rather isolated code, so I don't need to check the entire program, just the areas that I'm working on.

For context, the code I'm working on is in C/C++, and I work in Linux with GCC and VIM.

+9  A: 

gcov will give you coverage information for your unit tests.

It's difficult to answer your question in an accurate manner without knowing more about the refactorings you plan to perform.

An advice one might give is to proceed with small iterations instead of refactoring lots and lots of parts of your code base and then realize everything breaks.

Reference: The GNU Coverage Tool - A Brief Tutorial

Gregory Pakosz
+1 Refactor in small testable chunks. It's MUCH easier to see where something broke and figure out how to fix it.
Mark B
+7  A: 

There's no "easy" way to ensure that functionality hasn't been changed. You have to have complete unit tests that cover all possibilities. It's impossible to test absolutely everything but you can make sure that your most important user cases have thorough tests.

You can also use a coverage tool to ensure you have good test coverage:

http://covtool.sourceforge.net/

Mark Byers
+5  A: 

If you have unit tests but you're not happy that they cover the areas you're going to be refactoring, you can find out using code coverage analysis tools. If you find gaps, you can create and double-check tests to fill the gaps, then proceed with your refactoring fairly happy that your (updated) unit tests cover the ground thoroughly -- which is good for the project in the long term as well.

T.J. Crowder
+1  A: 

Im not sure about your specific platform of choice, but have you looked at code coverage tools, such as Bullseye. It wont provide you an analysis of the overall functionality (and if it deviates) but it will help you ensure that your tests are adequately exercising your target libraries. Its a commercial applications, but I know there are similar OSS versions for other languages, there might be free licenses if you need one.

GrayWizardx
+2  A: 

The trick is to use unit tests. Basically, the compiler checks the linguistical correctness of your creation while your unit tests verify it from a functional standpoint. By having a large set of good unit tests you can feel safe when refactoring (especially when working in a multi-developer project)

e8johan