简体   繁体   English

如何为 Visual Studio C++ 设置单元测试

[英]How to set up unit testing for Visual Studio C++

I'm having trouble figuring out how to get the testing framework set up and usable in Visual Studio 2008 for C++ presumably with the built-in unit testing suite.我无法弄清楚如何在Visual Studio 2008 for C++设置和使用测试框架,大概是内置的单元测试套件。

Any links or tutorials would be appreciated.任何链接或教程将不胜感激。

This page may help, it reviews quite a few C++ unit test frameworks: 这个页面可能会有所帮助,它回顾了很多 C++ 单元测试框架:

  • CppUnit单位
  • Boost.Test Boost.Test
  • CppUnitLite CppUnitLite
  • NanoCppUnit纳米Cpp单元
  • Unit++单位++
  • CxxTest测试

Check out CPPUnitLite or CPPUnitLite2 .查看CPPUnitLiteCPPUnitLite2

CPPUnitLite was 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 [eg 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++. CPPUnitLite 试图制作一个真正的 C++ 风格的测试框架,而不是一个移植到 C++ 的 Java 框架。 (I'm paraphrasing from Feather's Working Effectively with Legacy Code book). (我是从 Feather 的《有效处理遗留代码》一书中转述)。 CPPUnitLite2 seems to be another rewrite, with more features and bug fixes. CPPUnitLite2似乎是另一种重写,具有更多功能和错误修复。

I also just stumbled across UnitTest++ which includes stuff from CPPUnitLite2 and some other framework.我还偶然发现了UnitTest++ ,其中包括来自 CPPUnitLite2 和其他一些框架的内容。

Microsoft has released WinUnit .微软已经发布了WinUnit

Also checkout Catch or Doctest还可以查看CatchDoctest

There is a way to test unmanaged C++ using the built in testing framework within Visual Studio 2008 .有一种方法可以使用 Visual Studio 2008 中的内置测试框架来测试非托管 C++ If you create a C++ Test Project, using C++/CLI, you can then make calls to an unmanaged DLL.如果使用 C++/CLI 创建 C++ 测试项目,则可以调用非托管 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++.如果要测试用非托管 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/

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):

  1. Make sure your header files are consumable by moving source code to cpp files and using forward declaration if needed.通过将源代码移动到 cpp 文件并在需要时使用前向声明来确保您的头文件是可使用的。
  2. Compile your code to test as library (.lib)编译您的代码以作为库 (.lib) 进行测试
  3. Create your UnitTest project as C++ with CLR support.使用 CLR 支持将您的 UnitTest 项目创建为 C++。
  4. Include your header files.包括你的头文件。
  5. Include your .lib files.包括您的 .lib 文件。
  6. Add a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll添加对 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 的引用
  7. Use a really small class for declaring your unit test and jump from managed to C++/Native code like this (may have typos):使用一个非常小的类来声明您的单元测试,并像这样从托管代码跳转到 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.要添加新测试,您只需声明一个新的 [TestMethod] void NewTest(){::NewTest();} 方法和一个新的 void NewTest() 本机函数。 No macros, no tricks, straighforward.没有宏,没有技巧,直截了当。

Now, the heade file is optionally, but it can be used to expose the Assert class' methods with C++ native signatures (eg wchar_t* instead of Stirng^), so it can you can keep it close to C++ and far from C++/CLI:现在,头文件是可选的,但它可用于公开具有 C++ 本地签名的 Assert 类的方法(例如 wchar_t* 而不是 Stirng^),因此您可以使其接近 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

Personally, I prefer WinUnit since it doesn't require me to write anything except for my tests (I build a .dll as the test, not an exe).就个人而言,我更喜欢WinUnit,因为它不需要我编写除测试之外的任何内容(我构建了一个 .dll 作为测试,而不是 exe)。 I just build a project, and point WinUnit.exe to my test output directory and it runs everything it finds.我只是构建了一个项目,并将 WinUnit.exe 指向我的测试输出目录,它会运行它找到的所有内容。 You can download the WinUnit project here .您可以在此处下载 WinUnit 项目 (MSDN now requires you to download the entire issue, not the article. WinUnit is included within.) (MSDN 现在要求您下载整个问题,而不是文章。WinUnit 包含在其中。)

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.test and googletest look to be fairly similar, but adapted for slightly different uses. boost.testgoogletest看起来非常相似,但适用于略有不同的用途。 Both of these have a binary component, so you'll need an extra project in your solution to compile and run the tests.这两者都有一个二进制组件,因此您的解决方案中需要一个额外的项目来编译和运行测试。

The framework we use is CxxTest , which is much lighter;我们使用的框架是CxxTest ,它要轻得多; 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").它只是标题,并使用 Perl (!) 脚本从标题中抓取测试套件信息(套件继承自 CxxTest::Base,所有测试方法的名称都以“test”开头)。 Obviously, this requires that you get Perl from one source or another , which adds overhead to your build environment setup.显然,这要求您从一个另一个来源获取 Perl,这会增加构建环境设置的开销。

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.如果您正在寻找更集成的解决方案,请查看cfix studio ,它是用于 C/C++ 单元测试的 Visual Studio插件 It is quite similar to TestDriven.Net, but for (unmanaged) C/C++ rather than .NET.它与 TestDriven.Net 非常相似,但适用于(非托管)C/C++ 而不是 .NET。

I use UnitTest++ .我使用UnitTest++

In the years since I made this post the source has moved from SourceForge to github.在我发表这篇文章后的几年里,源代码已经从 SourceForge 转移到了 github。 Also the example tutorial is now more agnostic - doesn't go into any configuration or project set up at all.此外, 示例教程现在更加不可知 - 根本不涉及任何配置或项目设置。

I doubt it will still work for Visual Studio 6 as the project files are now created via CMake.我怀疑它仍然适用于 Visual Studio 6,因为项目文件现在是通过 CMake 创建的。 If you still need the older version support you can get the last available version under the SourceForge branch.如果您仍然需要旧版本支持,您可以在SourceForge分支下获取最后一个可用版本。

I was suffering to implement unit testing for an unmanaged C++ application in a Windows environment with Visual Studio.我正在努力使用 Visual Studio 在 Windows 环境中为非托管 C++ 应用程序实现单元测试。 So I managed to overcome and wrote a post as a step-by-step guidance to unmanaged C++ application unit testing.所以我设法克服并写了一篇文章作为非托管 C++ 应用程序单元测试的分步指南。 I hope it may help you.我希望它可以帮助你。

Unit test for unmanaged C++ in Visual Studio Visual Studio 中非托管 C++ 的单元测试

The unit tester for Visual Studio 2008 is only for .NET code as far as I know.据我所知,Visual Studio 2008 的单元测试器仅适用于 .NET 代码。

I used CppUnit on Visual Studio 2005 and found it to be pretty good.我在 Visual Studio 2005 上使用了 CppUnit,发现它非常好。

As far as I remember, the setup was relatively painless.据我所知,设置相对轻松。 Just make sure that in your testing projects the linker (Linker → Input → Additional Dependencies) includes cppunitd.lib.只需确保在您的测试项目中,链接器(链接器 → 输入 → 附加依赖项)包含 cppunitd.lib。

Then, #include <cppunit/extensions/HelperMacros.h> in your header.然后,在您的标题中#include <cppunit/extensions/HelperMacros.h>

You can then follow the steps inhttp://cppunit.sourceforge.net/doc/1.11.6/cppunit_cookbook.html to get your test class working.然后,您可以按照http://cppunit.sourceforge.net/doc/1.11.6/cppunit_cookbook.html 中的步骤让您的测试类工作。

I like the CxxTest as well for the same reasons.出于同样的原因,我也喜欢 CxxTest。 It's a header file only so no linking required.它只是一个头文件,因此不需要链接。 You aren't stuck with Perl as there is a Python runner as well.您不会被 Perl 困住,因为还有一个 Python 运行程序。 I will be reviewing the google library soon.我将很快审查谷歌图书馆。 The Boost stuff pulls in too much other baggage. Boost 的东西带来了太多的其他包袱。

I've used CppUnit with VS2005 and Eclipse.我在 VS2005 和 Eclipse 中使用了CppUnit The wiki is very thorough (especially if you are familiar with JUnit). wiki 非常详尽(特别是如果您熟悉 JUnit)。

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.我也用过 CppUnit,没问题。 Much the same as NUnit/JUnit/so on.与 NUnit/JUnit/等非常相似。

If you've used boost, they also have a unit testing library如果您使用过 boost,它们 还有一个单元测试库

The guys behind boost have some serious coding chops, so I'd say their framework should be pretty good, but it might not be the most user friendly :-) boost 背后的人有一些严重的编码技巧,所以我想说他们的框架应该非常好,但它可能不是最用户友好的 :-)

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

相关问题 如何使用Visual Studio 2005设置Google C ++测试框架(gtest) - How to set up Google C++ Testing Framework (gtest) with Visual Studio 2005 用于并行计算的C ++ + openmp:如何在visual studio中进行设置? - C++ + openmp for parallel computing: how to set up in visual studio? 需要Visual Studio 2005兼容的C ++单元测试框架 - Need Visual Studio 2005 Compatible C++ Unit Testing Framework 在Visual Studio 2012中对C ++控制台应用程序进行单元测试 - Unit testing c++ console application in Visual Studio 2012 使用Visual Studio测试框架进行C ++单元测试 - C++ unit testing with Visual Studio test framework 在 Visual Studio Code 中对本机 C++ 进行单元测试 - Unit Testing Native C++ In Visual Studio Code Visual Studio 2008中的非托管C ++单元测试 - Unmanaged C++ Unit testing in Visual Studio 2008 C++ 中的单元测试问题 (Visual Studio 2022) - Problems with Unit Testing in C++ (Visual studio 2022) 使用 Visual Studio C++ 进行单元测试时出现链接器错误 - Linker error while unit testing with Visual Studio C++ Visual Studio是否有扩展可以优化C ++代码单元测试? - Is there any extension for visual studio to optimize C++ code unit testing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM