BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

C++ Tutorial - Google Test (gtest)

cplusplus_icon.png




Bookmark and Share





bogotobogo.com site search:





Google Unit Test (GTest)

The Framework of Google C++ Testing is based on xUnit architecture. It is a cross platform system that provides automatic test discovery. In other words, we don't have to enumerate all of the test in our test suite manually. It supports a rich set of assertions such as fatal assertions (ASSERT_), non-fatal assertions (EXPECT_), and death test which checks that a program terminates expectedly.

Here is the Primer.

GTest also provides various options for running tests and offers textual and XML report. It also supports a mock object testing framework (Google Mock).

Initially, we have a project calculating a cubic:

// simplemath.h

#include <cmath>

double cubic(double d)
{
	return pow(d,3);
}


// SimpleMath.cpp : Defines the entry point for the console application.
#include "simplemath.h"

int main()
{
	cubic(10);
	return 0;
}
InitialSolution.png

In the following example, we used Visual Studio 2012 with 4 steps:

  1. Download Google test
  2. Compile gtest into a static library
  3. Create a unit test project
  4. Make a test case

Step 1. Download Google test (gtest)

Download the gtest-1.7.0-rc1.zip from Google C++ Unit Test or from gtest-1.7.0-rc1.zip, then extracts it.

Let's look at the C:\GTEST\gtest-1.7.0 directory to see what files are there.

gtest_directory_files.png

The src folder has all the gtest source files and later we need to add the include directory to the include path.



Step 2. Compile gtest into a static library
  1. Create a new static library project with a name GoogleTest.
    Add->New Project->Win32 Project->Static Library without precompiled header.

    static_library_project.png

  2. Right click on our new project, GoogleTest.
    On the Properties Pages, add include path:
    C:\GTEST\gtest-1.7.0 and C:\GTEST\gtest-1.7.0\include.

    include_path.png

  3. Add source files by Add->Existing Item...
    C:\GTEST\gtest-1.7.0\src\gtest_all.cc
    and C:\GTEST\gtest-1.7.0\src\gtest_main.cc.

    Source_Files_in_Google_Test.png

  4. Build GoogleTest into static library.
    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++.

    PreprocessorDefinitions.png

    Now, build it again:

    1>------ Rebuild All started: Project: GoogleTest, Configuration: Debug Win32 ------
    1>  gtest_main.cc
    1>  gtest-all.cc
    1>  Generating Code...
    1>  GoogleTest.vcxproj -> c:\users\khyuck\documents\visual studio 2012\Projects\SimpleMath\Debug\GoogleTest.lib
    ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
      






Step 3. 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_SimpleMath as a Win32 Console. We've just added 3rd prodject to our solution:

    thirdProject.png

  2. We need to add the two paths as we've done in Step 2:
    Right click on our new project, unittest_SimpleMath.
    On the Properties Pages, add include path:
    C:\GTEST\gtest-1.7.0 and C:\GTEST\gtest-1.7.0\include.

    adding2paths.png

  3. This project needs additional path to the initial project (SimpleMath) which we want to be tested.

    PathToSimpleMath.png

  4. Let's add new references (GoogleTest and SimpleMath) to unittest_SimpleMath.
    Right click on unittest_SimpleMath->References...
    Under Property Pages->Add New References...

    AddReference.png

    AddedReferences.png

  5. Great. Our Unit Test project has been set up.
    Final step will be making a test case.


Step 4. Create a Test Case

Now, we need to create a test case.
Type in the following lines of code:

// unittest_SimpleMath.cpp : Defines the entry point for the console application.

#include "gtest/gtest.h"
#include "simplemath.h"

TEST(testMath, myCubeTest)
{
    EXPECT_EQ(1000, cubic(10));	
}
Here, we're testing the cubic() function we wrote before, and it compares the output of 10^3 with 1000 using macro EXPECT_EQ.
If we run the unittest_SimpleMath, we get the test result:

PassedTest.png

OK!
We passed our first Google Test!





Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






C++ Tutorials

C++ Home

Algorithms & Data Structures in C++ ...

Application (UI) - using Windows Forms (Visual Studio 2013/2012)

auto_ptr

Binary Tree Example Code

Blackjack with Qt

Boost - shared_ptr, weak_ptr, mpl, lambda, etc.

Boost.Asio (Socket Programming - Asynchronous TCP/IP)...

Classes and Structs

Constructor

C++11(C++0x): rvalue references, move constructor, and lambda, etc.

C++ API Testing

C++ Keywords - const, volatile, etc.

Debugging Crash & Memory Leak

Design Patterns in C++ ...

Dynamic Cast Operator

Eclipse CDT / JNI (Java Native Interface) / MinGW

Embedded Systems Programming I - Introduction

Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu and Fedora

Embedded Systems Programming III - Eclipse CDT Plugin for gcc ARM Toolchain

Exceptions

Friend Functions and Friend Classes

fstream: input & output

Function Overloading

Functors (Function Objects) I - Introduction

Functors (Function Objects) II - Converting function to functor

Functors (Function Objects) - General



Git and GitHub Express...

GTest (Google Unit Test) with Visual Studio 2012

Inheritance & Virtual Inheritance (multiple inheritance)

Libraries - Static, Shared (Dynamic)

Linked List Basics

Linked List Examples

make & CMake

make (gnu)

Memory Allocation

Multi-Threaded Programming - Terminology - Semaphore, Mutex, Priority Inversion etc.

Multi-Threaded Programming II - Native Thread for Win32 (A)

Multi-Threaded Programming II - Native Thread for Win32 (B)

Multi-Threaded Programming II - Native Thread for Win32 (C)

Multi-Threaded Programming II - C++ Thread for Win32

Multi-Threaded Programming III - C/C++ Class Thread for Pthreads

MultiThreading/Parallel Programming - IPC

Multi-Threaded Programming with C++11 Part A (start, join(), detach(), and ownership)

Multi-Threaded Programming with C++11 Part B (Sharing Data - mutex, and race conditions, and deadlock)

Multithread Debugging

Object Returning

Object Slicing and Virtual Table

OpenCV with C++

Operator Overloading I

Operator Overloading II - self assignment

Pass by Value vs. Pass by Reference

Pointers

Pointers II - void pointers & arrays

Pointers III - pointer to function & multi-dimensional arrays

Preprocessor - Macro

Private Inheritance

Python & C++ with SIP

(Pseudo)-random numbers in C++

References for Built-in Types

Socket - Server & Client

Socket - Server & Client 2

Socket - Server & Client 3

Socket - Server & Client with Qt (Asynchronous / Multithreading / ThreadPool etc.)

Stack Unwinding

Standard Template Library (STL) I - Vector & List

Standard Template Library (STL) II - Maps

Standard Template Library (STL) II - unordered_map

Standard Template Library (STL) II - Sets

Standard Template Library (STL) III - Iterators

Standard Template Library (STL) IV - Algorithms

Standard Template Library (STL) V - Function Objects

Static Variables and Static Class Members

String

String II - sstream etc.

Taste of Assembly

Templates

Template Specialization

Template Specialization - Traits

Template Implementation & Compiler (.h or .cpp?)

The this Pointer

Type Cast Operators

Upcasting and Downcasting

Virtual Destructor & boost::shared_ptr

Virtual Functions



Programming Questions and Solutions ↓

Strings and Arrays

Linked List

Recursion

Bit Manipulation

Small Programs (string, memory functions etc.)

Math & Probability

Multithreading

140 Questions by Google



Qt 5 EXPRESS...

Win32 DLL ...

Articles On C++

What's new in C++11...

C++11 Threads EXPRESS...

Go Tutorial

OpenCV...








Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master