Gtest Linux Use (Google test)

Source: Internet
Author: User

Gtest is the cross-platform and open source C + + unit Testing framework developed by Google, which is very good and powerful.

: https://code.google.com/p/googletest/.

With regard to the use of gtest under Windows, Coderzh gives a very detailed guide to use: http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html.

My working environment: Ubuntu12.04, Python 2.7, MAKEFILE,SVN, etc.

    • First, download gtest file

I'm using svn to download it.

SVN checkout HTTP://GOOGLETEST.GOOGLECODE.COM/SVN/TRUNK/GTEST-SVN

Get the latest version of Gtest, you can see the folder gtest-svn successfully.

    • Second, generate gtest static library

Enter the folder first:

$CD GTEST-SVN

Compile build GTEST-ALL.O file (Note-I with no spaces ):

$g + +-i./include-i./-C/src/gtest-all.cc

Regenerate. A static library file:

$ AR-RV libgtest.a GTEST-ALL.O

If successful, the LIBGTEST.A library is generated in the current directory. You can copy it to a C + + unit test project for use.

    • Third, write simple function function
    1. Create a new project (directory) Gtestapp, containing two code files: Functions.h, Functions.cpp.

Implements the subtraction function of two int variables.

 // functions.h   #ifndef _functions_h   #define  _functions_h int  Add (int  One, int   int  Myminus (int  one,int   int  multiply (int  one,int   int  Divide (int  one,int    #endif  
//Functions.cpp#include"Functions.h"intAddintOneintBoth ) {        returnOne+both ;}intMyminus (intOneintBoth ) {        returnone-both ;}intMultiplyintOneintBoth ) {        returnone*both ;}intDivideintOneintBoth ) {        returnone/both ;}
    • Iv. Writing Unit test code

1. Write 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 (Ten, Myminus ( -, the));} TEST (multiplytest,mutilplytestcase) {assert_eq ( A, 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 namespacestd;intMainintargcChar*argv[]) {        //testing::gtest_flag (Output) = "xml:";//to generate an XML result fileTesting::initgoogletest (&AMP;ARGC,ARGV);//InitializeRun_all_tests ();//Run Unit Test        return 0;}
    • V. Compiling and running tests

1. Compiling

1) Copy the Gtest library file

Create a new Lib directory under the Gtestapp directory and copy the LIBGTEST.A into it.

$mkdir Lib

$CP <the path>/libgtest.a Lib

2) Copy gtest header file

The Include in the gtest1.6.0 directory contains the header files that need to be used. Copy the include to Gtestapp.

3) Compiling and linking

$ 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 #注意不是-libgtest and need to use libpthread this library

Finally, you can get a main executable file

2. Operation and Testing

$./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.

You can do this if you need to get an XML file.

First open the build function in the main function



Then recompile to generate the main executable file. Then perform the following operation.

$./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 look at the current directory, with the LS command: You can see a main.xml file, done.

This article refers to:

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/

Gtest Linux Use (Google test)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.