[原创翻译]在Visual Studio 中为原生C++代码编写单元测试

在Visual Studio 中为原生C++代码编写单元测试

原文地址http://blogs.msdn.com/b/jsocha/archive/2010/11/19/writing-unit-tests-in-visual-studio-for-native-c.aspx

原文作者:John Socha-Leialoha

作者博客http://blogs.msdn.com/b/jsocha/

翻译原因:1、锻炼自己蹩脚的英语;2、一直在寻找为原生C++代码编写单元测试的方法和工具,也了解了一些单元测试库包括googletest (http://code.google.com/p/googletest) 、cppunit等,但是还是没有MSTest方便。

排版方式:采用英汉对照方式排版,方便阅读和指正错误。排版格式跟原文可能有所不同,想看原文的请点击上面的原文地址。

注意事项:1、翻译过程中,对于IDE中的术语,使用Visual Studio 2010 旗舰中文版中对应的名称;2、不是所有的Visual Studio都支持单元测试,具体的请自行上网搜索确认。我所知肯定支持的版本包括Visual Studio 2008 Team Suite,Visual Studio 2010 Ultimate (旗舰版).

正文部分

        My team frequently creates wizards that run in Windows PE as part of a Configuration Manager task sequence, and we write them in C++. I’m a big fan of TDD, so when I started to work on a new architecture for our wizards, I wanted to write all the code using TDD.

       作为配置管理工作的一部分,我们团队经常需要使用C++来创建一些运行在Windows PE上的向导。作为驱动测试开发(Test-Driver Developement)的爱好者,当我开始为我们的向导创建新的架构时,我希望使用驱动测试开发来编写所有的代码。

       When I asked around about writing C++ tests, I was told there isn’t any support for C++ unit tests in Visual C++. Not true. In this blog post I’ll describe what’s required in order to write unit test for native C++ code using Visual Studio.

       在我到处打听怎么编写C++的测试案例时,被告知没有任何支持在Visual C++中编写C++单元测试的方法。这其实是错误的,在这篇博客里,我将描述在Visual Studio中编写原生C++代码的单元测试所需做的工作。

      For new C++ projects, you can set them up as I’ll describe below. However, for your existing code, you may have to reorganize your project structure before you can start writing unit tests.

      对于重头开始创建的C++项目,你只需按照我下面所说的步骤来做即可。但是,对于已经存在的代码(项目),你也许不得不重新组织您的项目结构才能开始编写单元测试案例。

Here is a quick overview of the setup for testing C++ code:

  • The unit tests need to be written in C++/CLI in order to use the .NET unit test framework
  • The code you’re testing should be in a static library that is linked into both your production EXE/DLL, as well as the test DLL

       下面是测试C++代码的简短步骤预览:

  • 为了使用.NET单元测试框架,单元测试必须使用C++/CLI来编写
  • 要测试的代码应该作为静态库连接到您的EXE/DLL产品和测试DLL中

Setting up the projects

As I mentioned above, the first step is to setup the projects correctly:

  1. Create a new Win32 Project and select Static library in the Application Settingsscreen. This is where you’ll add your production code
  2. Create a second Win32 Project for your production EXE or DLL
  3. Create a Test Project (in the C++ section of the Add New Project dialog box)
  4. In the Properties dialog box for the test project, change the Target Name to the name you want to use for you unit tests. The default is always DefaultTest, rather than the name of the project you just created

创建项目

       据上所述,首先是正确的创建项目:

       1. 创建一个新的 Win32 项目,然后在 应用程序设置中选择静态库,这是您添加产品代码的地方

       2. 为您的产品EXE或DLL创建第2个 Win32 项目 

       3. 创建一个 测试项目 (在新建项目中的C++部分){译注:VS2010需要旗舰版才有测试项目,Pro版本是没有的}

       4. 在测试项目的属性对话框中,修改目标文件名为您想要的名字。缺省是DefaultTest 

I’m using Visual Studio 2010, but these instruction will most likely work with prior versions, especially Visual Studio 2008.

      我(原作者)使用的是Visual Studio 2010,但是本文介绍的内容应该能在低版本上使用,特别是Visual Studio 2008.

Next we need to link the static library into the test and production projects, and provide both of those projects access to the header files in the static library:

  1. Right click the test project and select Properties
  2. In the Configuration combo box, select All Configurations to ensure the changes below are made to both Debug and Release configurations
  3. Click the Common Properties node and then Framework and References
  4. Click Add New Reference…, select the static library and click OK
  5. Click the Configuration Properties node, then click the C/C++ node
  6. Click in the Additional Include Directories property and type something like this (substitute the name of your library project for ProductLibrary): 
    $(SolutionDir)\ProductLibrary;%(AdditionalIncludeDirectories)
  7. Change the Common Language RunTime Support property to Common Language RunTime Support (/clr)

You’ll want to repeat steps 1-6 for your EXE/DLL project.

     接下来我们需要连接静态库到测试项目和产品项目,并提供静态库的头文件给这两个项目访问:

     1. 右键点击测试项目,选择属性

     2. 在配置选择框中选择所有配置来确保接下来的修改在Debug和Release配置中都应用到

     3. 点击通用属性,选择框架和引用

     4. 点击 添加新引用(N)... ,选择静态库并点击 确定

     5. 点击配置属性节点,然后点击 C/C++ 节点

     6. 点击 附加包含目录 属性,并输入类似这样的地址(用你的静态库名字来替代ProductLibrary):$(SolutionDir)\ProductLibrary;%(AdditionalIncludeDirectories)

     7. 修改 公共语言运行时支持 属性到 公共语言运行时支持(/clr)

      对你的EXE/DLL项目重复上述1-6个步骤

Writing C++/CLI Unit Tests

At this point you should be able to build your solution, so the next step if you’re using TDD is to write a test method. If you’ve written test methods in C#, most of this should be familiar to you, although with a different syntax. But if you’re a C++ programmer and you’ve never written C# unit tests, there is a bit of a learning curve. Here I’ll explain enough to get you started.

编写C++/CLI 单元测试

到此为止,您应该已经可以编译您的解决方案了,所以使用驱动测试开发接下来就是编写测试方法了。如果您曾经使用C#来编写过测试方法,除了不同的语法外,这里的大部分步骤您应该都很熟悉。但是,如果您是一个C++程序员并且从未编写过C#单元测试,这里可能会有一点学习难度。这里我会足够详细的告诉您如何开始。

Creating a new C++ Unit Test project initially creates a file called UnitTest1.cpp that contains one unit test. I usually delete the extra code and just leave the single test method. If you have a simple class called SomeClass with a method called SomeValue that returns an int, you can write a test that looks like this:

创建新的C++单元测试项目时初始会创建一个包含一个单元测试的名为UnitTest1.cpp的文件。我一般都会删除额外的代码只留下那个测试函数。如果您有一个名为SomeClass的简单类包含了一个返回一个int的名为SomeValue的函数,您可以类似下面的方法来编写一个测试案例:

[TestMethod]
void ShouldReturn1()
{
std::unique_ptr<SomeClass> pClass(new SomeClass());
Assert::AreEqual<int>(1, pClass->SomeValue());
};

There are several elements to this test that you may not be familiar with, but you can find full details on MSDN. First is the TestMethod attribute. This identifies the method as being a test method. When you run the tests in Visual Studio, it will run any public methods marked with this attribute.

测试代码里面有几项你可能不熟悉,但是你都可以在MSDN里找到详细的解释。首先是TestMethod属性,这标识该函数是一个测试函数。当您在Visual Studio中运行测试时,所有标记为该属性的public函数都将运行。

Next, notice the use of the Assert class static method called AreEqual. There are a number of static methods that allow you to validate the results of running a command. Again, you’ll find all of these documented on MSDN.

接下来,注意Assert类的静态成员函数AreEqual 的使用,Assert类的一系列的静态成员函数可以让您检查命令的执行结果。这些您都可以在MSDN上找到文档。

 

(未完待续)


 

 

posted @ 2012-03-07 11:49  金陵书生  阅读(1869)  评论(0编辑  收藏  举报