VS2015 builds the GoogleTest framework -- configure the first project

 

1. Introduction to googletest

This article uses googletest for unit testing, here is a brief introduction to googletest. googletest (gtest for short) is a cross-platform and open source (Linux, Mac OS X, Windows, Cygwin, Windows CE and Symbian) C++ unit testing framework, released by Google. gtest is generated for writing C++ tests on different platforms. It provides rich assertions, fatal and non-fatal judgments, parameterization, "death tests", and more.

2. Related preparations

The content of this article is based on win10+vs2015 using the googletest framework for C++ unit testing, so the first job we have to do is to download and configure googletest for our next use.

2.1 Download the source code

Go to https://github.com/google/googletest to download the source code of googletest to prepare for our next compilation.

2.2 Compile the googletest source code to generate the library files we need

After the download is complete, what we get is a compressed file in zip format, and we unzip it to a suitable directory (in order to facilitate our own search). Here we need to pay attention to two folders, which will be used later.

 

 

Use vs2015 to open gtest.sln in msvc/2010, you will be prompted to upgrade the VC++ compiler and library, click OK.

After the upgrade is complete, open the gtest project properties dialog box (you can right-click the project name to open the properties), and configure the project properties. It is possible to modify in two ways as shown in the figure (note the comparison with the property settings of the later test items).

Both Debug and Release modes must be compiled.

When compiling, the words "Unable to start the program" will appear, which is normal, because we only generate the relevant library files, not the executable files.

 

After compiling, there are two folders Win32-Debug and Win32-Release under msvc/2010/gtest/, and each folder has a gtstd.lib file, as shown in the following figure.

At this point, the preparation phase is completed.

2.3 Learning the necessary knowledge

Assertions: A large number of macros are used to wrap assertions in gtest. The assertions here are different from the assertions in the C language.

According to its usage, it can be divided into two categories:

  1. ASSERT series (ASSERT_& series, when the checkpoint fails, exit the current function, not the current case);
  2. EXPECT series (the EXPECT_* series of assertions, when the checkpoint fails, continue to execute).

According to the commonly used functions, it can be divided into 12 categories in turn. The following categories are mainly used:

  1. boolean comparison
  2. Numerical data comparison
  3. string comparison
  4. floating point comparison
  5. Approximate Number Comparison
  6. abnormal detection
  7. Custom format functions and parameter checking

boolean comparison

ASSERT_TRUE(condition)

EXPECT_TRUE(condition)

condition == true

ASSERT_FALSE(condition)

EXPECT_FALSE(condition)

condition == false

Numerical data comparison

ASSERT_EQ (expected, actual)

EXPECT_EQ (expected, actual)

expected == actual

ASSERT_NE (val1, val2)

EXPECT_NE (val1, val2)

val1 != val2

ASSERT_LT (val1, val2)

EXPECT_LT (val1, val2)

val1 < val2

ASSERT_LE (val1, val2)

EXPECT_LE (val1, val2)

val1 <= val2

ASSERT_GT (val1, val2)

EXPECT_GT (val1, val2)

val1 > val2

ASSERT_GE (val1, val2)

EXPECT_GE (val1, val2)

val2 >= val2

string comparison

ASSERT_STREQ (str1, str2)

EXPECT_STREQ (str1, str2)

Both C strings have the same content (both char* and wchar_t* types are supported)

ASSERT_STRNE (p1, p2)

EXPECT_STRNE (p1, p2)

Two C strings with different contents (both char* and wchar_t* types are supported)

ASSERT_STRCASEEQ (str1, str2)

EXPECT_STRCASEEQ (str1, str2)

The contents of two C strings are the same, ignoring case (only char * type is supported)

ASSERT_STRCASENE (str1, str2)

EXPECT_STRCASENE (str1, str2)

The contents of two C strings are different, ignoring case (only char * type is supported)

floating point comparison

ASSERT_FLOAT_EQ (val1, val2)

EXPECT_FLOAT_EQ (val1, val2)

the two float values are almost equal

ASSERT_DOUBLE_EQ (val1, val2)

EXPECT_DOUBLE_EQ (val1, val2)

the two double values are almost equal

Approximate Number Comparison

ASSERT_NEAR (val1, val2, abs_error)

EXPECT_NEAR (val1, val2, abs_error)

The absolute value difference between the two values ​​val1 and val2 does not exceed abs_error

 

 

 

 

exception checking

ASSERT_THROW (statement, exception_type)

EXPECT_THROW (statement, exception_type)

Throws an exception of the specified type

ASSERT_THROW(statement)

EXPECT_THROW(statement)

throw any type of exception

ASSERT_NO_THROW(statement)

EXPECT_NO_THROW(statement)

do not throw exceptions

 

Function value and parameter checking (currently only supports up to 5 parameters)

ASSERT_PRED1(pred1, val1)

EXPECT_PRED1(pred1, val1)

pred1(val1) returns true

ASSERT_PRED2(pred2, val1, val2)

EXPECT_PRED2(pred2, val1, val2)

pred2(val1, val2) returns true

 

Windows HRESULT

ASSERT_HRESULT_SUCCEEDED(expression)

EXPECT_HRESULT_SUCCEEDED(expression)

expression is a success HRESULT

ASSERT_HRESULT_FAILED(expression)

EXPECT_HRESULT_FAILED(expression)

expression is a failure HRESULT

 

自定义格式函数与参数检查(目前最多支持5个参数)

ASSERT_PRED_FORMAT1(pred1, val1)

EXPECT_PRED_FORMAT1(pred1, val1)

pred1(val1) is successful

ASSERT_PRED_FORMAT1(pred1, val1, val2)

EXPECT_PRED_FORMAT1(pred1, val1, val2)

pred2(val1, val2) is successful

 

2.4创建测试项目并配置属性

在vs2015中,创建Win32控制台应用程序MyTest。

打开项目属性对话框,配置工程属性。展开“配置属性 -> C/C++ -> 常规”,在【附加包含目录】中添加“.\include”(这里省略了include的相关母目录,在之前有过关于include的说明,如果忘记在哪找,可以在前边的内容中复习一下);展开“C/C++ -> 代码生成”,在【运行库】中修改为多线程调试DLL(/MDd),如下图所示。

 

展开“链接器 -> 输入”,在【附加依赖项】添加刚刚生成的lib文件路径,注意这里是配置Debug,要输入Win32-Debug里面的gtestd.lib文件路径(这里需要注意文件名后面有个d)。

 

以上就是配置Debug的方式,配置Release只需要把前边相对应的位置改为Release的相关内容即可。

2.5编写测试代码

这里测试代码主要分为3个文件,主文件FirstGtest.cpp,以及Box类的两个相关文件CBox.h和CBox.cpp

 

运行结果:

三、遇到的问题及解决办法

  1. 在生成两个gtestd.lib文件时,编译器报“不是有效的Win32应用程序”,因为之前写的程序都是生成可执行程序,而没有做过只生成库文件的程序,本能地以为程序出错了,之后经过查阅资料和以及进一步地深入研究,发现这里只是生成了依赖库文件(我们也只需要这个库),并没有生成可执行文件,所以那是正常的。
  2. 配置googletest依赖库时,“配置属性 -> C/C++ -> 代码生成”中的【运行库】,前后没有设置相同的模式,从而导致找不到对应的库的情况。
  3. 在配置googletest时,修改“配置属性 -> 链接器 -> 输入”中的【附加依赖项】时,只填到了我们所生成的库文件的目录,但是没有添加文件名,报找不到*.obj文件的错误,只需要把路径填写到库文件就可以解决这个问题。

 

作者:耑新新,发布于  博客园

转载请注明出处,欢迎邮件交流:[email protected]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324812854&siteId=291194637