对于如何在VS2013中进行Boost单元测试,这方面资料太少。自己也因此走了不少弯路。下文将会阐述一下如何在VS2013中进行Boost单元测试。
在开始Boost单元测试之前,我们需要先安装VS2013插件Boost Unit Test Adapter (Update 3) 以及编译Boost库。Boost Unit Test Adapte可以在VS2013中的“工具->扩展与更新”中找到并安装。对于Boost Unit Test Adapter所支持的Boost库版本请参考网页。我选择的是版本号为1.55.0的Boost库(可以在其官网下载得到)。在编译Boost库的时候,可以参考教程及博文。我选择的是完全编译(bjam --toolset=msvc-12.0 --build-type=complete)。
下文以几个步骤来阐述:
1. 新建解决方案及工程
接下来,我们新建一个BoostUnitTest解决方案,然后在该方案下添加(鼠标右击解决方案新建项目)两个项目。第一个项目是空的“WIN32”项目“Tested”,另一个是“Boost Unit Test Project”项目(新建项目->模板->Visual C++->Test下)“BoostUnitTest”。如下图:
2. 在被测试工程中添加文件
在Tested工程中添加一个头文件tested.h及源文件tested.cpp. 具体代码如下
1 #include <iostream> 2 3 using namespace std; 4 5 class Tested 6 { 7 public: 8 9 Tested(); 10 virtual ~Tested(); 11 int add(const int a, const int b); 12 13 private: 14 15 }; |
1 #include "tested.h" 2 3 Tested::Tested() 4 { 5 6 } 7 8 Tested::~Tested() 9 { 10 11 } 12 13 int Tested::add(const int a, const int b) 14 { 15 return a + b; 16 } |
在这里,我们并不需要新建一个main函数对这个类tested进行测试(因为我们已经有单元测试了),但一个程序默认是需要main函数的,所以在需要在Tested项目属性中进行这样的设置(“配置类型”改为“动态库(.dll)”见下图):