向现有项目添加单元测试 [英] Adding unit tests to an existing project

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

问题描述

我的问题与之前提出的问题很相关,但我需要一些实用的建议。

My question is quite relevant to something asked before but I need some practical advice.

我有有效地使用遗留代码在我手中,我在使用书中的建议它在我工作的项目。该项目是一个C ++应用程序,由几个库组成,但代码的主要部分被编译为单个可执行文件。我必须使用googletest在现有代码中添加单元测试。

I have "Working effectively with legacy code" in my hands and I 'm using advice from the book as I read it in the project I 'm working on. The project is a C++ application that consists of a few libraries but the major portion of the code is compiled to a single executable. I 'm using googletest for adding unit tests to existing code when I have to touch something.

我的问题是如何设置我的构建过程,以便我可以建立我的单元测试,因为有两个不同的可执行文件需要共享代码,而我不能提取代码从我的测试中应用程序到库。现在我已经使我的构建过程的应用程序,其中保存单元测试链接到主应用程序的构建过程生成的对象文件,但我真的不喜欢它。有任何建议吗?

My problem is how can I setup my build process so I can build my unit tests since there are two different executables that need to share code while I am not able to extract the code from my "under test" application to a library. Right now I have made my build process for the application that holds the unit tests link against the object files generated from the build process of the main application but I really dislike it. Are there any suggestions?

推荐答案

如果你的测试应用程序只链接对象文件需要测试,他们作为一个库,应该可以将这些对象文件分组到主和测试应用程序的单独的库。

If your test app is only linking the object files it needs to test then you are effectively already treating them as a library, it should be possible to group those object files into a separate library for the main and the test app. If you can't then I don't see that what you are doing is too bad an alternative.

如果你不得不链接其他没有被测试的目标文件,那么我不知道你在做什么太糟糕了。这是一个依赖的标志,需要被打破,你有完美的书。
我们有类似的问题,并使用像Vlion建议的系统

If you are having to link other object files not under test then that is a sign of dependencies that need to be broken, for which you have the perfect book. We have similar problems and use a system like the one suggested by Vlion

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

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