简体   繁体   English

在Visual Studio中进行正确的单元测试

[英]Proper unit testing in Visual Studio

I have solution containing several C++/x64 projects. 我有包含几个C ++ / x64项目的解决方案。 For one of this project I want to write unit tests. 对于这个项目之一,我想编写单元测试。 I'm following this tutorial and I'm using Visual Studio Community 2013. Hello world example from mentioned tutorial runs fine, but if I create test project inside my larger solution which contains multiple projects then I get bunch of errors. 我正在遵循教程,并且正在使用Visual Studio Community2013。提到的教程中的Hello world示例运行良好,但是如果我在包含多个项目的较大解决方案中创建测试项目,则会遇到很多错误。 Before I will go into deeper troubleshooting I would like to clarify a few questions that I've not found answers for: 在进行更深入的故障排除之前,我想澄清一些我没有找到答案的问题:

  1. If I have multiple tests projects how does Test -> Run -> All Tests knows which one to run? 如果我有多个测试项目,那么Test -> Run -> All Tests知道要运行哪个?

  2. Should I exclude projects that I do not want to test via: Right click on solution name -> Properties -> Configuration 我是否应该排除我不想通过其进行测试的项目: Right click on solution name -> Properties -> Configuration

  3. If I have projects that are to be build under x64 should I set Test -> Test Settings -> Default Processor Architecture -> x64 ? 如果我有要在x64下构建的项目,是否应该设置“ Test -> Test Settings -> Default Processor Architecture -> x64

  4. Is it important if my StartUp project ( Right click on project name -> Set as StartUp Project ) is tested project or testing project? 如果我的StartUp项目( Right click on project name -> Set as StartUp Project )是已测试项目还是正在测试项目,是否重要?

  5. I've noted that my test project has platform win32 while tested project has x64 under Right click on solution name -> Properties -> Configuration is this OK? 我注意到我的测试项目的平台为win32,而测试项目的平台为x64,请Right click on solution name -> Properties -> Configuration ,这样可以吗?

PS: I'm running tests via Test -> Run -> All Tests PS:我正在通过Test -> Run -> All Tests

  1. It runs all tests it can find. 它运行它可以找到的所有测试。 It finds tests via test runner plugins, which are invoked whenever a project finishes building to see if it contains any tests. 它通过测试运行程序插件查找测试,每当项目完成构建以查看其是否包含任何测试时,都会调用该插件。
  2. Why do you have projects that you don't want to test in your solution? 为什么会有不希望在解决方案中测试的项目? I don't think there's a simple way to exclude a project from testing, aside from unloading it. 我认为除了卸载项目之外,没有一种简单的方法可以将项目排除在测试范围之外。
  3. It should not be necessary, but if you run into problems, it's worth trying. 它不是必须的,但是如果遇到问题,则值得尝试。
  4. No, test runners ignore the starting project. 不,测试跑步者会忽略开始的项目。
  5. Depending on how your projects are set up, this may be either slightly wrong or completely wrong. 根据项目的设置方式,这可能是略有错误或完全错误。

Let me elaborate on that last point. 让我详细说明最后一点。 There are two typical ways to set up unit tests in C++. 有两种在C ++中设置单元测试的典型方法。

One is to have your executable project with all its files, and then you have the test project which also contains all the source files of the main project except for the file containing the entry point, and also contains the test files. 一种是让可执行项目及其所有文件,然后是测试项目,该项目还包含主项目的所有源文件,除了包含入口点的文件之外,还包含测试文件。

This has the downside of you having to manage those files, ie when you add a new file to the project, you have to add it to both projects. 这具有必须管理这些文件的缺点,即,当您向项目中添加新文件时,必须将其添加到两个项目中。 It also means that the compiler will compile all those source files twice, once for each project. 这也意味着编译器将编译所有这些源文件两次,每个项目一次。

It also means that if the two projects have differing configurations (x64 vs Win32) it works. 这也意味着,如果两个项目具有不同的配置(x64与Win32),则可以正常工作。 That is not fully an upside, because you generally want to test the same thing you're actually delivering, and testing a 32-bit build of your code is not useful in finding bugs that only happen in 64-bit builds. 这还不是完全有好处的,因为您通常想测试与实际交付的内容相同的东西,并且测试32位代码的构建对于发现仅在64位版本中发生的错误没有用。

The other option is to have three projects: a library containing most of the source code (I prefer static libraries, though DLLs are an option), a main project that just contains the entry point, and a test project that contains only the test files. 另一个选择是拥有三个项目:一个包含大部分源代码的库(我更喜欢静态库,尽管DLL是一个选项),一个仅包含入口点的主项目以及一个仅包含测试文件的测试项目。 。 The two latter link against the first. 后两个链接与第一个链接。

This means that all code is only compiled once, and that you test the exact same thing you're delivering. 这意味着所有代码仅被编译一次,并且您测试的是交付的完全相同的东西。

In this case, all project configurations have to match. 在这种情况下,所有项目配置必须匹配。 If they don't, you get build errors with potentially confusing error messages at best ("I just wrote this function, what do you mean you can't link it?"), and links against stale versions of the library ("I put a debug printf in that function, why isn't it printing?") at worst. 如果不这样做,那么您将得到构建错误,充其量可能会产生令人困惑的错误消息(“我刚刚编写了此函数,这意味着您不能链接它?”),以及指向该库的过时版本的链接(“我将调试printf放到该函数中,为什么它不打印?”)

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

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