开发者

How can I check that I didn't break anything when refactoring?

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 p开发者_如何学JAVArogram, 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.


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


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/


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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜