如何为 Visual Studio C++ 设置单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up unit testing for Visual Studio C++
提问by DShook
I'm having trouble figuring out how to get the testing framework set up and usable in Visual Studio 2008for C++
presumably with the built-in unit testing suite.
我无法搞清楚如何获得测试框架设置和使用在Visual Studio 2008中的C++
推测可能与内置的单元测试工具。
Any links or tutorials would be appreciated.
任何链接或教程将不胜感激。
采纳答案by Aardvark
This pagemay help, it reviews quite a few C++ unit test frameworks:
这个页面可能会有所帮助,它回顾了很多 C++ 单元测试框架:
- CppUnit
- Boost.Test
- CppUnitLite
- NanoCppUnit
- Unit++
- CxxTest
- 单位
- Boost.Test
- CppUnitLite
- 纳米Cpp单元
- 单位++
- 测试
Check out CPPUnitLiteor CPPUnitLite2.
CPPUnitLitewas created by Michael Feathers, who originally ported Java's JUnit to C++ as CPPUnit (CPPUnit tries mimic the development model of JUnit - but C++ lacks Java's features [e.g. reflection] to make it easy to use).
CPPUnitLite由 Michael Feathers 创建,他最初将 Java 的 JUnit 作为 CPPUnit 移植到 C++(CPPUnit 尝试模仿 JUnit 的开发模型 - 但 C++ 缺乏 Java 的特性 [例如反射] 使其易于使用)。
CPPUnitLite attempts to make a true C++-style testing framework, not a Java one ported to C++. (I'm paraphrasing from Feather's Working Effectively with Legacy Codebook). CPPUnitLite2seems to be another rewrite, with more features and bug fixes.
CPPUnitLite 试图制作一个真正的 C++ 风格的测试框架,而不是一个移植到 C++ 的 Java 框架。(我是从 Feather 的《有效处理遗留代码》一书中转述)。CPPUnitLite2似乎是另一种重写,具有更多功能和错误修复。
I also just stumbled across UnitTest++which includes stuff from CPPUnitLite2 and some other framework.
我还偶然发现了UnitTest++,其中包括来自 CPPUnitLite2 和其他一些框架的内容。
Microsoft has released WinUnit.
微软已经发布了WinUnit。
回答by Jared
There is a way to test unmanaged C++ using the built in testing framework within Visual Studio 2008. If you create a C++ Test Project, using C++/CLI, you can then make calls to an unmanaged DLL. You will have to switch the Common Language Runtime support to /clr from /clr:safe if you want to test code that was written in unmanaged C++.
有一种方法可以使用 Visual Studio 2008 中的内置测试框架来测试非托管 C++。如果使用 C++/CLI 创建 C++ 测试项目,则可以调用非托管 DLL。如果要测试用非托管 C++ 编写的代码,则必须将公共语言运行时支持从 /clr:safe 切换到 /clr。
I have step by step details on my blog here: http://msujaws.wordpress.com/2009/05/06/unit-testing-mfc-with-mstest/
我在我的博客上有分步详细信息:http: //msujaws.wordpress.com/2009/05/06/unit-testing-mfc-with-mstest/
回答by aracntido
Here is the approach I use to test the IIS URL Rewrite module at Microsoft (it is command-line based, but should work for VS too):
这是我用来在 Microsoft 测试 IIS URL 重写模块的方法(它基于命令行,但也适用于 VS):
- Make sure your header files are consumable by moving source code to cpp files and using forward declaration if needed.
- Compile your code to test as library (.lib)
- Create your UnitTest project as C++ with CLR support.
- Include your header files.
- Include your .lib files.
- Add a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
- Use a really small class for declaring your unit test and jump from managed to C++/Native code like this (may have typos):
- 通过将源代码移动到 cpp 文件并在需要时使用前向声明来确保您的头文件是可使用的。
- 编译您的代码以作为库 (.lib) 进行测试
- 使用 CLR 支持将 UnitTest 项目创建为 C++。
- 包括你的头文件。
- 包括您的 .lib 文件。
- 添加对 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 的引用
- 使用一个非常小的类来声明您的单元测试,并像这样从托管代码跳转到 C++/Native 代码(可能有拼写错误):
Here is an example:
下面是一个例子:
// Example
#include "stdafx.h"
#include "mstest.h"
// Following code is native code.
#pragma unmanaged
void AddTwoNumbersTest() {
// Arrange
Adder yourNativeObject;
int expected = 3;
int actual;
// Act
actual = yourNativeObject.Add(1, 2);
// Assert
Assert::AreEqual(expected, actual, L"1 + 2 != 3");
}
// Following code is C++/CLI (Managed)
#pragma managed
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
[TestClass]
public ref class TestShim {
public:
[TestMethod]
void AddTwoNumbersTest() {
// Just jump to C++ native code (above)
::AddTwoNumbersTest();
}
};
With this approach, people don't have to learn too much C++/CLI stuff, all the real test will be done in C++ native and the TestShim class will be used to 'publish' the test to MSTest.exe (or make it visible).
使用这种方法,人们不必学习太多 C++/CLI 的东西,所有真正的测试都将在 C++ 原生中完成,并且 TestShim 类将用于将测试“发布”到 MSTest.exe(或使其可见) )。
For adding new tests you just declare a new [TestMethod] void NewTest(){::NewTest();} method and a new void NewTest() native function. No macros, no tricks, straighforward.
要添加新测试,您只需声明一个新的 [TestMethod] void NewTest(){::NewTest();} 方法和一个新的 void NewTest() 本机函数。没有宏,没有技巧,直截了当。
Now, the heade file is optionally, but it can be used to expose the Assert class' methods with C++ native signatures (e.g. wchar_t* instead of Stirng^), so it can you can keep it close to C++ and far from C++/CLI:
现在,头文件是可选的,但它可用于使用 C++ 本地签名(例如 wchar_t* 而不是 Stirng^)公开 Assert 类的方法,因此您可以使其接近 C++ 而远离 C++/CLI :
Here is an example:
下面是一个例子:
// Example
#pragma once
#pragma managed(push, on)
using namespace System;
class Assert {
public:
static void AreEqual(int expected, int actual) {
Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual(expected, actual);
}
static void AreEqual(int expected, int actual, PCWSTR pszMessage) {
Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual(expected, actual, gcnew String(pszMe
ssage));
}
template<typename T>
static void AreEqual(T expected, T actual) {
Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual(expected, actual);
}
// Etcetera, other overloads...
}
#pragma managed(pop)
HTH
HTH
回答by moswald
Personally, I prefer WinUnitsince it doesn't require me to write anything except for my tests(I build a .dll as the test, not an exe). I just build a project, and point WinUnit.exe to my test output directory and it runs everything it finds. You can download the WinUnit project here. (MSDN now requires you to download the entire issue, not the article. WinUnit is included within.)
就个人而言,我更喜欢WinUnit,因为它不需要我编写除测试之外的任何内容(我构建了一个 .dll 作为测试,而不是 exe)。我只是构建了一个项目,并将 WinUnit.exe 指向我的测试输出目录,它会运行它找到的所有内容。您可以在此处下载 WinUnit 项目。(MSDN 现在要求您下载整个问题,而不是文章。WinUnit 包含在其中。)
回答by Ben Straub
The framework included with VS9 is.NET, but you can write tests in C++/CLI, so as long as you're comfortable learning some .NET isms, you should be able to test most any C++ code.
VS9 中包含的框架是.NET,但您可以使用 C++/CLI 编写测试,因此只要您能轻松学习一些 .NET 主义,您应该能够测试大多数 C++ 代码。
boost.testand googletestlook to be fairly similar, but adapted for slightly different uses. Both of these have a binary component, so you'll need an extra project in your solution to compile and run the tests.
boost.test和googletest看起来非常相似,但适用于略有不同的用途。这两者都有一个二进制组件,因此您的解决方案中需要一个额外的项目来编译和运行测试。
The framework we use is CxxTest, which is much lighter; it's headers only, and uses a Perl (!) script to scrape test suite information from your headers (suites inherit from CxxTest::Base, all your test methods' names start with "test"). Obviously, this requires that you get Perl from one sourceor another, which adds overhead to your build environment setup.
我们使用的框架是CxxTest,它要轻得多;它只是标题,并使用 Perl (!) 脚本从标题中抓取测试套件信息(套件继承自 CxxTest::Base,所有测试方法的名称都以“test”开头)。显然,这要求您从一个或另一个来源获取 Perl ,这会增加构建环境设置的开销。
回答by graham.reeds
I use UnitTest++.
我使用UnitTest++。
In the years since I made this post the source has moved from SourceForge to github. Also the example tutorialis now more agnostic - doesn't go into any configuration or project set up at all.
在我发表这篇文章后的几年里,源代码已经从 SourceForge 转移到了 github。此外,示例教程现在更加不可知 - 根本不涉及任何配置或项目设置。
I doubt it will still work for Visual Studio 6 as the project files are now created via CMake. If you still need the older version support you can get the last available version under the SourceForgebranch.
我怀疑它仍然适用于 Visual Studio 6,因为项目文件现在是通过 CMake 创建的。如果您仍然需要旧版本支持,您可以在SourceForge分支下获取最后一个可用版本。
回答by graham.reeds
The tools that have been mentioned here are all command-line tools. If you look for a more integrated solution, have a look at cfix studio, which is a Visual Studio AddIn for C/C++ unit testing. It is quite similar to TestDriven.Net, but for (unmanaged) C/C++ rather than .NET.
这里提到的工具都是命令行工具。如果您正在寻找更集成的解决方案,请查看cfix studio,它是用于 C/C++ 单元测试的 Visual Studio插件。它与 TestDriven.Net 非常相似,但适用于(非托管)C/C++ 而不是 .NET。
回答by Nayana Adassuriya
I was suffering to implement unit testing for an unmanaged C++ application in a Windows environment with Visual Studio. So I managed to overcome and wrote a post as a step-by-step guidance to unmanaged C++ application unit testing. I hope it may help you.
我正在努力使用 Visual Studio 在 Windows 环境中为非托管 C++ 应用程序实现单元测试。所以我设法克服并写了一篇文章作为非托管 C++ 应用程序单元测试的分步指南。我希望它可以帮助你。
回答by colgur
回答by Orion Edwards
I'm not 100% sure about VS2008, but I know that the Unit Testing framework that microsoft shipped in VS2005 as part of their Team Suite was only for .NET, not C++
我对 VS2008 不是 100% 确定,但我知道微软在 VS2005 中作为 Team Suite 的一部分提供的单元测试框架仅适用于 .NET,而不适用于 C++
I've used CppUnit also and it was alright. Much the same as NUnit/JUnit/so on.
我也用过 CppUnit,没问题。与 NUnit/JUnit/等非常相似。
If you've used boost, they also have a unit testing library
如果您使用过 boost,它们还有一个单元测试库
The guys behind boost have some seriouscoding chops, so I'd say their framework should be pretty good, but it might not be the most user friendly :-)
boost 背后的人有一些严重的编码技巧,所以我想说他们的框架应该非常好,但它可能不是最用户友好的 :-)