向遗留代码添加单元测试 [英] Adding unit tests to legacy code

查看:23
本文介绍了向遗留代码添加单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事后,您是否曾向遗留代码添加单元测试?代码有多复杂,存根和模拟一切有多难?最终结果值得吗?

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?

推荐答案

我发现,最好的方法是逐步添加单元测试,而不是直接说我们现在将对应用程序进行单元测试.

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.

这个问题可能有助于提供更多建议.您如何将单元测试引入大型遗留 (C/C++) 代码库?

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

这篇关于向遗留代码添加单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆