Google test and gtestlinux
GTest is a cross-platform and open-source C ++ unit testing framework developed by Google. It is very powerful.
: Https://code.google.com/p/googletest.
About GTest in Windows, CoderZh provides a very detailed user guide: http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html.
My working environment: Ubuntu12.04, python 2.7, Makefile, SVN, etc.
- 1. Download The gtest File
I used svn for download.
Svn checkout http://googletest.googlecode.com/svn/trunk/ gtest-svn
Get the latest gtest version. The gtest-svn folder is displayed successfully.
- Ii. Generate a gtest static library
First enter the folder:
$ Cd gtest-svn
Compile the gtest-all.o file (note-I without spaces ):
$ G ++-I./include-I./-c./src/gtest-all.cc
Generate the. a static library file again:
$ Ar-rv libgtest. a gtest-all.o
If successful, the libgtest. a library is generated in the current directory. You can copy it to the C ++ unit test project for ease of use.
- 3. Compile simple functions
Implement the addition, subtraction, multiplication, and division of two int variables.
//functions.h#ifndef _FUNCTIONS_H#define _FUNCTIONS_Hint add(int one,int two);int myMinus(int one,int two);int multiply(int one,int two);int divide(int one,int two);#endif
//functions.cpp#include "functions.h"int add(int one,int two){ return one+two;}int myMinus(int one,int two){ return one-two;}int multiply(int one,int two){ return one*two;}int divide(int one,int two){ return one/two;}
1. Write the unit test code functionsTest. cpp.
//functionsTest.cpp#include "gtest/gtest.h"#include "functions.h"TEST(AddTest,AddTestCase){ ASSERT_EQ(2,add(1,1));}TEST(MinusTest,MinusTestCase){ ASSERT_EQ(10,myMinus(25,15));}TEST(MultiplyTest,MutilplyTestCase){ ASSERT_EQ(12,multiply(3,4));}TEST(DivideTest,DivideTestCase){ ASSERT_EQ(2,divide(7,3));}
2. Write the test code TestAll. cpp.
// TestAll. cpp # include "gtest/gtest. h "# include <iostream> using namespace std; int main (int argc, char * argv []) {// testing: GTEST_FLAG (output) =" xml :"; // to generate the xml result file testing: InitGoogleTest (& argc, argv); // initialize RUN_ALL_TESTS (); // run the unit test return 0 ;}
- V. Compile and run the test
1. Compile
1) copy the gtest Library File
Create the lib directory under the GTestApp directory and copy libgtest. a to it.
$ Mkdir lib
$ Cp <the path>/libgtest. a lib
2) copy the gtest header file
The include file in the gtest1.6.0 directory contains the header file to be used. Copy include to GTestApp.
3) Compilation and link
$ G ++-o functions. o-c functions. cpp
$ G ++-o functionsTest. o-c funciontsTest. cpp-I./include
$ G ++-o TestAll. o-c TestAll. cpp-I./include
Link:
$ G ++-o main *. o-I./include-L./lib-lgtest-lpthread # note that the library libpthread is not-libgtest and must be used at the same time.
Finally, we can get an executable file of main.
2. Run and Test
$./Main
[=========] Running 4 tests from 4 test cases.
[----------] Global test environment set-up.
[----------] 1 test from AddTest
[RUN] AddTest. AddTestCase
[OK] AddTest. AddTestCase (0 MS)
[----------] 1 test from AddTest (1 MS total)
[----------] 1 test from MinusTest
[RUN] MinusTest. MinusTestCase
[OK] MinusTest. MinusTestCase (0 MS)
[----------] 1 test from MinusTest (0 MS total)
[----------] 1 test from MultiplyTest
[RUN] MultiplyTest. MultiplyTestCase
[OK] MultiplyTest. MultiplyTestCase (0 MS)
[----------] 1 test from MultiplyTest (1 MS total)
[----------] 1 test from DivideTest
[RUN] DivideTest. DivideTestCase
[OK] DivideTest. DivideTestCase (0 MS)
[----------] 1 test from DivideTest (3 MS total)
[----------] Global test environment tear-down
[==========] 4 tests from 4 test cases ran. (8 MS total)
[PASSED] 4 tests.
If you need an xml file, you can do this.
First, open the generate function in the main function.
Testing: GTEST_FLAG (output) = "xml :";
Re-compile to generate the main executable file. Then perform the following operations.
$./Main -- gtest_output = xml
[=========] Running 4 tests from 4 test cases.
[----------] Global test environment set-up.
[----------] 1 test from AddTest
[RUN] AddTest. AddTestCase
[OK] AddTest. AddTestCase (0 MS)
[----------] 1 test from AddTest (1 MS total)
[----------] 1 test from MinusTest
[RUN] MinusTest. MinusTestCase
[OK] MinusTest. MinusTestCase (0 MS)
[----------] 1 test from MinusTest (1 MS total)
[----------] 1 test from MultiplyTest
[RUN] MultiplyTest. MultiplyTestCase
[OK] MultiplyTest. MultiplyTestCase (0 MS)
[----------] 1 test from MultiplyTest (1 MS total)
[----------] 1 test from DivideTest
[RUN] DivideTest. DivideTestCase
[OK] DivideTest. DivideTestCase (0 MS)
[----------] 1 test from DivideTest (0 MS total)
[----------] Global test environment tear-down
[==========] 4 tests from 4 test cases ran. (4 MS total)
[PASSED] 4 tests.
Let's take a look at the current directory and run the ls command: we can see that there is a main. xml file.
Reference:
Http://graybull.is-programmer.com/posts/37871.html
Http://www.cnblogs.com/coderzh/archive/2009/03/31/1426758.html
Https://code.google.com/p/googletest/