在 Visual Studio 2012 中对 exe 中的 C++ 代码进行单元测试时阻力最小的路径 [英] Path of least resistance when unit testing C++ code in an exe, in Visual Studio 2012

查看:27
本文介绍了在 Visual Studio 2012 中对 exe 中的 C++ 代码进行单元测试时阻力最小的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些明智的建议.长话短说,我正在重建一个 - 对我来说 - 由大约 7000 行代码组成的相对复杂的应用程序.我在创建应用程序的第一次迭代时遇到了许多问题,在我看来,测试驱动开发可能只是一张票.

I'm in need of some sage advice here. Long story short, I'm rebuilding a - for me - relatively complex app comprised of about 7000 lines of code. I ran into a number of issues when I created the first iteration of my application and it seems to me that test driven development might just be the ticket.

我很高兴看到 Visual Studio 2012 现在原生支持 C++ 中的 TDD,所以我继续阅读尽可能多的内容.不幸的是,Vs2012 是相当新的,我觉得文档有点缺乏.但这有点离题了.我主要依赖 MSDN 站点上的以下指南:

I was pleased to see that Visual Studio 2012 now natively supports TDD in C++, so I went ahead and read as much as I could. Unfortunately, Vs2012 is fairly new and I feel the documentation is somewhat lacking. But this is a little beside the point. I'm relying mainly on the following guide on the MSDN site:

http://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef

它相当清楚地指出,如果要测试的代码构建为 .exe,那么前进的方法是创建一个单独的测试项目并链接输出目标文件.我猜他们的意思是目标文件?或者也许不是?

It fairly clearly states that if the code under testing is to be built as an .exe, then the way forward is creating a separate test project and linking the output object file. I'm guessing they mean the object files? Or maybe not?

老实说,我对需要链接多少个 .obj 有点困惑.起初我以为我需要链接每个 obj 文件,这很乏味.

I'm honestly a little confused as to how many .obj's I need to link. At first I thought I needed to link every single obj file which is fairly tedious.

如果有人有这方面的经验,并且也许还可以推荐使用哪些宏或类似的捷径以使这个过程尽可能轻松,我将非常感激!

If anyone has experience doing this and could perhaps also recommend which macros or similar short cuts to use in order to make this process as painless as possible, I'd be much obliged!

推荐答案

这取决于您如何构建解决方案.我喜欢构建我的解决方案的方式是拥有三个项目.

This will depend on how you have your solution structured. The way I like to structure my solutions is to have three projects.

  • 一个 .lib 项目,其中包含我的源代码.
  • 一个可执行项目,与 .lib 链接.这会调用 main() 调用中的 .lib
  • 一个测试项目 (exe),与 .lib 链接.
  • A .lib project that has my source code in it.
  • An executable project, linked with the .lib. This calls into the .lib in the main() call
  • A test project (exe), linked with the .lib.

使用此结构,您可以使用 Common Properties 部分中的 Add New Reference... 按钮,引用将为您排序(标题包含路径除外)在 C++\General\Additional 包含目录中找到.

With this structure you can use the Add New Reference... button in the Common Properties section and the references will be sorted for you (except the header include path found in C++\General\Additional include directories).

如果你不想重构你的项目,你可以告诉链接器每个 obj 文件(Linker\Input\Additional dependencies).如果您有很多要测试的类,这可能是大量的 .obj 文件.不幸的是,如果您使用预编译的头文件,您可能会遇到问题.

If you do not want to restructure your projects you can tell the linker about each obj file (Linker\Input\Additional dependencies). This may be a significant number of .obj files if you have a lot of classes that you want to test. Unfortunately, you may have issues if you use pre-compiled headers.

如果可以,我建议重组项目.

I would suggest restructuring the projects if you can.

这篇关于在 Visual Studio 2012 中对 exe 中的 C++ 代码进行单元测试时阻力最小的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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