Sie sind auf Seite 1von 15

National University of Computer & Emerging Sciences (NUCES),

Islamabad Department of Computer Science

CS201: Data Structures (Fall 2018)

Google Unit Test (GTest)

Google Test is a unit testing library for the C++ programming language, based on
the xUnit architecture. It is a cross platform system that provides automatic test discovery.

C++ GTest Tutorial:

Step 01:

Create Project (Console Application) which calculates square root of provided number:
Uncheck “Precompiled Headers” before creating project.
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science

Step 02: Download Google test (gtest)

Download the gtest-1.7.0-rc1.zip. Then extracts it

Link: http://www.bogotobogo.com/cplusplus/files/cpptest/gtest-1.7.0-rc1.zip
Or download from slate

The src folder has all the gtest source files and later we need to add the include directory to the
include path.
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science

Step 03: Compile gtest into a static library (For Visual studio 2010, 2012 and 2013)

1. Create a new static library project with a name GoogleTest.


Solution->Add-> New Project->Win32 Project->Static Library without precompiled
header.
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science
2. Right click on our new project, GoogleTest.
On the Properties Pages, add include path:
D:\Fall 2018\Gunit\gtest-1.7.0 and D:\Fall 2018\Gunit\gtest-1.7.0\include
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science
3. Add source files by Add->Existing Item...

D:\Fall 2018\Gunit\gtest-1.7.0\src\gtest_all.cc
and D:\Fall 2018\Gunit\gtest-1.7.0\src\gtest_main.cc.

4. Build GoogleTest into static library.


National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science

In the build process, we may have some errors related to class template:
VC++ 2012 does not (and will never) support variadic templates; consequently, its standard
library implementation attempts to fake them using preprocessor-generated overloads and
specializations. The number of faux variadic template parameters defaults to 5 - the problem is
that gtest is trying to instantiate std::tuple<> with as many as 10 template arguments. - Google
Test in Visual Studio 2012.

So, we need to set _VARIADIC_MAX=10 for Preprocessor Definitions under C/C++.

Build GoogleTest
Output:

========== Build: 2 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science
Step 04 Create a unit test project

Now, it's time to create a unit test project.

1. Right click on Solution->Add->New Project with a name unittest_SquareRoot as a


Win32 Console. We've just added 3rd prodject to our solution:
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science

2. We need to add the two paths as we've done in Step 2:


Right click on our new project, unittest_SquareRoot .
On the Properties Pages, add include path:
D:\Fall 2018\Gunit\gtest-1.7.0 and D:\Fall 2018\Gunit\gtest-1.7.0\include
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science

3. This project needs additional path to the initial project (SquareRoot) which we want to
be tested.
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science

4. Let's add new references (GoogleTest and SquareRoot) to unittest_ SquareRoot.

Great. Our Unit Test project has been set up.


Final step will be making a test case.
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science

Step 04: Create a test Case

Now, we need to create a test case.


Type in the following lines of code:
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science

Step 5: Change the startup project.


Right Click on the Solution. Go to “Properties”. In Properties go to “Startup Project”. Under
“Single Startup Project” select your unittest. Click apply and then Run the project.

Output:
National University of Computer & Emerging Sciences (NUCES),
Islamabad Department of Computer Science

Das könnte Ihnen auch gefallen