Microsoft Unit Testing Framework for C++

Author: kagula
Date: 2018-05-11

Introduction

VC自带多个VC++单元测试工具,其中Microsoft Unit Testing Framework原生支持Test Explorer,这里通过三个例子来学习Microsoft Unit Testing Framework.


Environment

[1]Visual Studio 2017,update 7.1


Content

一、最简单的测试
这种情况只发生在“测试驱动”编码方式中,现实世界我很少用到,这里只是为了大致有个了解。
Step1:新建我们的第一个测试单元UnitTest1,新建solution
Visual C++ -> Test -> Native Unit Test Project

Step2:
用下面的代码替代建立的默认模板

unittest1.cpp

#include "stdafx.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

#include <iostream>

using namespace std;

TEST_MODULE_INITIALIZE(ModuleInitialize)
{
	Logger::WriteMessage("In Module Initialize");
}

TEST_MODULE_CLEANUP(ModuleCleanup)
{
	Logger::WriteMessage("In Module Cleanup");
}

/*
第一次运行本unit test,使用Ctrl+R+A 运行全部测试.
以后你可以在Test Explorer窗口中运行指定的测试函数.
*/
namespace KagulaUnitTest
{		
	//模拟我的第一个待测试的function集合
	TEST_CLASS(myFirstFunctionSetForTest)
	{
	public:
		
		TEST_METHOD(TestMethod1)
		{
			//运行unit test不会打开控制台窗口,所以你也不会看到下面这条代码的任何std输出.
			cout << "hello,World!" << endl;
		}

		TEST_METHOD(TestMethod2)
		{
			//模拟耗时的操作.
			for (size_t i = 0; i < 1000; i++)
			{
				if ((i % 100) == 0)
				{
					//VisualStudio2017Update7.1有个很严重的缺陷,有时候,你改了代码,但是使用Test Explorer中的"Run selected Tests"菜单项,//运行的还是老的代码,这时候,你得rebuild solution后再运行.
					//所以修改代码后,修改下面的字符串输出,从output窗口中查看是不是修改已经生效很重要.
					Logger::WriteMessage("d.");
				}
			}//for
		}//test method
	};//test class

	//模拟我的第二个待测试的function集合
	TEST_CLASS(mySecondFunctionSetForTest)
	{
	public:

		//Assert的具体用法参考下面的地址
		//https://docs.microsoft.com/en-us/visualstudio/test/microsoft-visualstudio-testtools-cppunittestframework-api-reference#general_asserts
		TEST_METHOD(TestMethod2_1)
		{
			Logger::WriteMessage("TestMethod2_1");
			int expected = 0;
			int actual = 0;
			Assert::AreEqual(expected,actual);
			Assert::AreEqual(expected,actual,L"are not equal assert!");
		}

		TEST_METHOD(TestMethod2_2)
		{
			Logger::WriteMessage("TestMethod2_2");
			Assert::Fail(L"Fail");
		}
	};
}

Step3:
使用菜单项[test]->[Run]->[All Tests]或则直接使用快捷键Ctrl+R+A
而不是习惯的F5或Ctrl+F5,之后会出现Test Explorer子窗口,如下图。


Output子窗口,Show output from下拉框选项改为Tests,然后你就可以看到单元测试项目的输出。


二、对已存exe的测试这才是我最常见的使用场合

Step1:

建个简单的console程序c++项目,用来模拟已经存在的EXE程序项目

我写了个很简单的代码Source.cpp

#include <iostream>
using namespace std;

//这个函数用来演示你写的函数如何被"单元测试"工具调用
int myFunction()
{
	return 1;
}

int main(int argc,char* argv[])
{
	cout << "hello,World!" << endl;



	return 0;
}

Step2:为项目添加Unit Test依赖的头文件和库文件搜索路径.
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\VS\UnitTest\include
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\VS\UnitTest\lib

Step3:
使用下面的步骤添加unit test文件
Solution Explorer -> Add -> New Item -> C++ Unit Test.

如果没有“C++ Unit Test”模板,就添加个普通的cpp文件

Test.cpp

//若不需要单元测试,把UNIT_TEST宏注释掉就可以了!
//否则exe程序会去找我们不需要的unit test依赖的dll.
#define UNIT_TEST

#ifdef UNIT_TEST
#include <CppUnitTest.h>

//假设这是我待做unit test测试的函数.begin
extern int myFunction();
//假设这是我待做unit test测试的函数.end


using namespace Microsoft::VisualStudio::CppUnitTestFramework;

BEGIN_TEST_MODULE_ATTRIBUTE()
TEST_MODULE_ATTRIBUTE(L"Date",L"2010/6/12")
END_TEST_MODULE_ATTRIBUTE()

TEST_MODULE_INITIALIZE(ModuleInitialize)
{
	Logger::WriteMessage("In Module Initialize");
}

TEST_MODULE_CLEANUP(ModuleCleanup)
{
	Logger::WriteMessage("In Module Cleanup");
}

/*
第一次运行本unit test,使用Ctrl+R+A 运行全部测试.
以后你可以在Test Explorer窗口中运行指定的测试函数.
*/
namespace KagulaUnitTest
{
	TEST_CLASS(myFunctionSetForTest)
	{
	public:

		//Assert的具体用法参考下面的地址
		//https://docs.microsoft.com/en-us/visualstudio/test/microsoft-visualstudio-testtools-cppunittestframework-api-reference#general_asserts
		TEST_METHOD(TestMethod2_1)
		{
			Logger::WriteMessage("TestMethod1");
			int expected = 0;
			int actual = myFunction();
			Assert::AreEqual(expected,L"are not equal assert!");
		}
	};
}
#endif
Step4:
把项目的Configuration Type属性从原来的Application (.exe)改为Dynamic Library (.dll)
否则你单元测试代码是不能正常运行的。

Step5:
使用Ctrl+R+A运行所有的单元测试,或使用主菜单项
Test->Run->All->Tests菜单命令运行所有单元测试。
接下来你可以探索Test Explorer窗口里的unit test功能的。

Final Step:
[1]
如果你项目unit test没问题了,再把project的Configuration Type属性 改回Application (.exe)
[2]
如果改回EXE后,Ctrl+F5运行程序,console一闪而过,使用下面的设置
Configuration Properties -> Linker -> System -> SubSystem -> Console(/SUBSYSTEM:CONSOLE)

三、对已存dll项目的测试
Step1:新建个空白项目,当作是我们待测的现存dll项目。

Installed -> Visual C++ -> General -> Empty Project
新起项目名称为UnitTest3
Configuration Type 设为 Dynamic Library(.dll)
这样编译源代码后会生成dll lib等两个文件.

我这个项目,由两个原文件组成,源码内容如下

CDemoLib.h

#ifndef _LIB_H_
#define _LIB_H_

#ifdef _WIN32

#ifdef _WINDLL //VC2017新建项目后,改生成方式为dll,会自带这个宏定义.
#define LIB_API  extern "C" __declspec(dllexport)
#else
#define LIB_API  extern "C" __declspec(dllimport)
#endif

//我不喜欢编写非C语言风格的接口供调用者使用,除非时间紧.
#ifdef _WINDLL
#define CLASS_EXPORT __declspec(dllexport)
#else
#define CLASS_EXPORT __declspec(dllimport)

#endif

#else
//非Windows环境不需要那么麻烦直接定一个空白的就可以了.
#define  LIB_API 
#endif


LIB_API int add(int x,int y);

class CLASS_EXPORT MyDemoClass
{
public:
	int add(int a,int b);
};

#endif

CDemoLib.cpp

#include "CDemoLib.h"

int add(int x,int y)
{
	return x + y;
}

int MyDemoClass::add(int a,int b)
{
	return a + b;
}

Step 2:
在当前solution下新建单元测试项目
Visual C++ -> Test -> Native Unit Test Project
我把project起名为MyUnitTester。

这个项目只有一个源文件,清单如下:

#include "stdafx.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

#include "../UnitTest3/CDemoLib.h"

//添加All Configuration 库文件搜索路径: $(SolutionDir)$(Configuration);
#pragma comment(lib,"UnitTest3.lib")

namespace MyUnitTester
{		
	TEST_CLASS(UnitTest1)
	{
	public:
		
		TEST_METHOD(TestMethod1)
		{
			Logger::WriteMessage("MyUnitTester....");
			int expect = 0;
			int actual = add(0,0);
			//C风格接口  测试
			Assert::AreEqual(expect,L"are not equal assert!");

			MyDemoClass mdc;
			actual = mdc.add(0,0);
			//C++风格接口  测试
			Assert::AreEqual(expect,L"are not equal assert!");
		}
	};
}

Final Step:

然后你就可以Ctrl+R+A运行单元测试了。


Remark:

Q: 为什么不用boost.test?
A 因为在Visual Studio 中 Debug方式运行,console会一闪而过.


Reference

[1]<<Use the Microsoft Unit Testing Framework for C++ in Visual Studio>> https://docs.microsoft.com/en-us/visualstudio/test/how-to-use-microsoft-test-framework-for-cpp [2]<<Microsoft.VisualStudio.TestTools.CppUnitTestFramework API Reference>> https://docs.microsoft.com/en-us/visualstudio/test/microsoft-visualstudio-testtools-cppunittestframework-api-reference#general_asserts

相关文章

文章浏览阅读2.2k次,点赞6次,收藏20次。在我们平时办公工作...
文章浏览阅读1k次。解决 Windows make command not found 和...
文章浏览阅读3.2k次,点赞2次,收藏6次。2、鼠标依次点击“计...
文章浏览阅读1.3w次。蓝光版属于高清版的一种。BD英文全名是...
文章浏览阅读974次,点赞7次,收藏8次。提供了更强大的功能,...
文章浏览阅读1.4w次,点赞5次,收藏22次。如果使用iterator的...