简体   繁体   English

将单元测试添加到现有项目

[英]Adding unit tests to an existing project

My question is quite relevant to something asked before but I need some practical advice. 我的问题与之前提出的问题非常相关,但我需要一些实用的建议。

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. 该项目是一个C ++应用程序,由几个库组成,但代码的主要部分被编译为单个可执行文件。 I 'm using googletest for adding unit tests to existing code when I have to touch something. 当我不得不触摸某些内容时,我正在使用googletest将单元测试添加到现有代码中。

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? 有什么建议吗?

Working Effectively With Legacy Code is the best resource for how to start testing old code. 有效地使用旧版代码是如何开始测试旧代码的最佳资源。 There are really no short term solutions that won't result in things getting worse. 实际上没有短期解决方案不会导致事情变得更糟。

I'll sketch out a makefile structure you can use: 我将勾勒出一个可以使用的makefile结构:

all: tests executables

run-tests: tests
    <commands to run the test suite>

executables: <file list>
    <commands to build the files>

tests: unit-test1 unit-test2 etc

unit-test1: ,files that are required for your unit-test1>
    <commands to build unit-test1>

That is roughly what I do, as a sole developer on my project 这大致是我的工作,作为我项目的唯一开发人员

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. 如果你不能,那么我不会发现你正在做的事情太糟糕了。

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 我们有类似的问题,并使用类似于Vlion建议的系统

I personally would continue doing as you are doing or consider having a build script that makes the target application and the unit tests at the same time (two resulting binaries off the same codebase). 我个人会继续做你正在做的事情或者考虑使用一个构建脚本来同时进行目标应用程序和单元测试(两个结果二进制文件离开相同的代码库)。 Yes it smells fishy but it is very practical. 是的它闻起来很腥,但非常实用。

Kudos to you and good luck with your testing. 感谢你,祝你测试顺利。

I prefer one test executable per test. 我更喜欢每个测试一个测试可执行 This enables link-time seams and also helps allow TDD as you can work on one unit and not worry about the rest of your code. 这可以实现链接时间接缝,并且还可以帮助允许TDD,因为您可以在一个单元上工作而不用担心代码的其余部分。

I make the libraries depend on all of the tests. 我使库依赖于所有测试。 Hopefully this means your tests are only run when the code actually changes. 希望这意味着您的测试仅在代码实际更改时运行。

If you do get a failure the tests will interrupt the build process at the right place. 如果确实出现故障,测试将在正确的位置中断构建过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM