Skip to content

Google C++ Testing Framework

Google C++ Testing & Mocking Framework.

Google C++ Mocking Framework

Inspired by jMock, EasyMock, and Hamcrest, and designed with C++'s specifics in mind, Google C++ Mocking Framework (or Google Mock for short) is a library for writing and using C++ mock classes. Google Mock:

  • lets you create mock classes trivially using simple macros,
  • supports a rich set of matchers and actions,
  • handles unordered, partially ordered, or completely ordered expectations,
  • is extensible by users, and
  • works on Linux, Mac OS X, Windows, Windows Mobile, minGW, and Symbian.

We hope you find it useful!

Google C++ Testing Framework

Google's framework for writing C++ tests on a variety of platforms (Linux, Mac OS X, Windows, Cygwin, Windows CE, and Symbian). Based on the xUnit architecture. Supports automatic test discovery, a rich set of assertions, user-defined assertions, death tests, fatal and non-fatal failures, value- and type-parameterized tests, various options for running the tests, and XML test report generation.

How to install

$ autoreconf -ifv
$ ./configure
$ make

make install은 지원하지 않는다. 아래와 같이 설치해야 한다.

## make install is dangerous and not supported.
cp -r include/gtest "$OPM_LOCAL/include"
lib_files=`find . -iname '*.a' | grep -v samples`
for cursor in $lib_files; do
    cp $cursor "$OPM_LOCAL/lib"
done

CMake setup

FetchContent CMake module 을 사용하면 된다.

cmake_minimum_required(VERSION 3.14)
project(my_project)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

Assertions

These assertions do basic true/false condition testing.

Fatal assertion

Nonfatal assertion

Verifies

ASSERT_TRUE(condition);

EXPECT_TRUE(condition);

condition is true

ASSERT_FALSE(condition);

EXPECT_FALSE(condition);

condition is false

Binary Comparison

This section describes assertions that compare two values.

Fatal assertion

Nonfatal assertion

Verifies

ASSERT_EQ(val1,val2);

EXPECT_EQ(val1,val2); || val1 == val2

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);

val1 >= val2

String Comparison

The assertions in this group compare two C strings. If you want to compare two string objects, use EXPECT_EQ, EXPECT_NE, and etc instead.

Fatal assertion

Nonfatal assertion

Verifies

ASSERT_STREQ(str1,str2);

EXPECT_STREQ(str1,_str_2);

the two C strings have the same content

ASSERT_STRNE(str1,str2);

EXPECT_STRNE(str1,str2);

the two C strings have different content

ASSERT_STRCASEEQ(str1,str2);

EXPECT_STRCASEEQ(str1,str2);

the two C strings have the same content, ignoring case

ASSERT_STRCASENE(str1,str2);

EXPECT_STRCASENE(str1,str2);

the two C strings have different content, ignoring case

Sample program

우선 main함수가 포함된 test.cpp파일이다.

#define UNICODE 1
#include <gmock/gmock.h>
#include <gtest/gtest.h>

// List of test headers.
#include <BaseTest.h>

// Project Entry point.
int main(int argc, char ** argv)
{
    testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

//// For Windows Project Entry point.
//#if defined(WIN32) || defined(_WIN32)
//#include <windows.h>
//int APIENTRY
//WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, int cmdShow)
//{
//    return main(__argc, __argv);
//}
//#endif

아래는 테스트 케이스가 포함된 BaseTest.h파일 이다.

#include <stdafx.h>
#include <config.h>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

class BaseTest: public testing::Test
{
public:
    virtual void SetUp() { }
    virtual void TearDown() { }
};

TEST_F(BaseTest, Default)
{
    ASSERT_TRUE(TRUE);
}

TestFixture

class CommonTestFixture : public ::testing::Test
{
public:
    CommonTestFixture()
    {
        std::cout << " - Constructor\n";
    }

    ~CommonTestFixture()
    {
        std::cout << " - Destructor\n";
    }

public:
    virtual void SetUp()
    {
        std::cout << " -- SetUp\n";
    }

    virtual void TearDown()
    {
        std::cout << " -- TearDown\n";
    }

    static void SetUpTestCase()
    {
        std::cout << "SetUpTestCase\n";
    }

    static void TearDownTestCase()
    {
        std::cout << "TearDownTestCase\n";
    }
};

TEST_F(CommonTestFixture, Default1)
{ }

TEST_F(CommonTestFixture, Default2)
{ }

OUTPUT:

SetUpTestCase
 - Constructor
 -- SetUp
 -- TearDown
 - Destructor
 - Constructor
 -- SetUp
 -- TearDown
 - Destructor
TearDownTestCase

Test loop

--gtest_repeat=100플래그를 사용하여 테스트를 반복할 수 있지만, 에러가 발생될 경우 중지되지 않는다.(방법이 있다. 추후 정리 요망) 아래와 같이 입력하면 에러 발생시 중지된다.

$ for i in {0..10}; do ./tester --gtest_filter=ThreadPool* || break; done

Skip test

Source code example:

// Exclude a specific test
testing::GTEST_FLAG(filter) = "-MyLibrary.TestWriting"; // The writing test is broken, so skip it

Command line example:

./tester --gtest_filter=*foo1*:*foo2*-*bar1*:*bar2*

Local Download

Googletest release 1.7.0
Googletest-release-1.7.0.tar.gz
Googletest release 1.8.0
Googletest-release-1.8.0.zip

See also

Favorite site

References


  1. Googletest-Getting_started_with_Google_C++_Testing_Framework.pdf 

  2. Au-googletestingframework-pdf.pdf 

  3. Zerovirus79.blogspot.kr_-_Google_Mock.pdf