C ++ unit testing framework Gtest configuration and use

 

Address reprint: https://blog.csdn.net/clayanddev/article/details/53771248

"Your time is very valuable, and I is a unit test, I'll meet you in the programming world."

Previously I rarely write unit tests, and believe it is a waste of time. But later found, save that point of time writing test code much not worth the time spent on post-commissioning. Recently read "Angile Java", this gradually come to understand the importance of unit testing in software development process, it was decided from now on to develop the habit of writing unit tests.

Java has the famous JUnit, but must also have mature C ++ unit testing framework. After some searching, I finally chose Google's open-source C ++ unit testing framework Gtest. The following records at the configuration and simple to use Gtest, hoping to help a friend in need.

Configuration
(1) to extract the custom directory, for convenience, the gtest-1.7.0 / CMakeList.txt in option (BUILD_SHARED_LIBS "Build shared libraries ( DLLs)." OFF) OFF to ON in the dynamic link library to generate .

(2) use cmake compiled, then the library file and include files configuration, using the following command:

unzip gtest-1.7.0.zip  
cd gtest-1.7.0  
mkdir build  
cd build  
cmake ..

(3) After compilation, a dynamic link header files and configuration files, using the following command:

sudo mkdir /usr/lib/gtest
sudo cp *.so /usr/lib/gtest/
sudo cp ../include/gtest /usr/include/gtest -R
sudo chmod 755 /usr/lib/gtest/*.so
sudo chmod 755 /usr/include/gtest -R

This, Gtest even if the configuration is complete, it can be used in a dynamic link library in C ++ programs.

use

GTest's very easy to use, here I will use a simple project to show basic usage of GTest.

Project documents constitute:

TestCpp 

  • CMakeLists.txt
  • main.cpp
  • test 
    • StringUtilTest.cpp
  • utils 
    • StringUtil.cpp
    • StringUtil.h
    • TestUtil.cpp
    • TestUtil.h

Code that constitutes

main.cpp

#include "TestUtil.h"  

int main(int argc, char** argv)  
{  
   TestUtil::runTests(argc, argv);  
   return 0;  
}

 

StringUtilTest.cpp

#include "StringUtil.h"  
#include <gtest/gtest.h>  
#include <string>  

using namespace std;  

TEST(StringUtilTest, getTestString)  
{  
    StringUtil* str = new StringUtil();  
    EXPECT_EQ("Hello,world!", str->getTestString());  
    delete str;  
}  

StringUtil.cpp

#include "StringUtil.h"  

using namespace std;  
StringUtil::StringUtil()  
{  
}  

std::string StringUtil::getTestString()  
{  
    return string("Hello,world!");  
}  

StringUtil.h

#ifndef STRINGUTIL_H  
#define STRINGUTIL_H  
#include <string>  

class StringUtil  
{  
public:  
    StringUtil();  

    std::string getTestString();  
};  

#endif // STRINGUTIL_H

TestUtil.cpp

#include "TestUtil.h"  
# include <gtest/gtest.h>  

int TestUtil::runTests(int argc, char **argv)  
{  
    testing::InitGoogleTest(&argc, argv);  
    return RUN_ALL_TESTS();  
}  

TestUtil.h

#ifndef TESTUTIL_H  
#define TESTUTIL_H  
#include <string>  

class TestUtil  
{  
public:  
    static int runTests(int argc, char **argv);  

private:  
    TestUtil(){}  
};  

#endif // TESTUTIL_H

 CMakeLists.txt

#basic setting  
PROJECT(TestCpp)  
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)  
#SET(CMAKE_BUILD_TYPE Debug)  

#header files  
SET(HEADER_FILES_PATH  
./utils  
./test  
)  
INCLUDE_DIRECTORIES(${HEADER_FILES_PATH})  

#src files  
AUX_SOURCE_DIRECTORY(. SRC_LIST)  
AUX_SOURCE_DIRECTORY(./utils SRC_LIST_1)  
LIST(APPEND SRC_LIST ${SRC_LIST_1})  
AUX_SOURCE_DIRECTORY(./test SRC_LIST_2)  
LIST(APPEND SRC_LIST ${SRC_LIST_2})  

#build setting  
LINK_DIRECTORIES(/usr/lib/gtest)  
ADD_EXECUTABLE(${PROJECT_NAME} ${SRC_LIST})  
TARGET_LINK_LIBRARIES(${PROJECT_NAME} libgtest.so)  

run

Use cmake to compile run to see the test results StringUtil, the command is as follows:

cd TestCpp  
mkdir build  
cd build  
cmake ..  
make .  
./TestCpp  

postscript

This is just the most basic usage Gtest more information, please refer Gtest official website documents

Guess you like

Origin blog.csdn.net/liangzhao_jay/article/details/87283424