gtest的Linux使用(Google test)

來源:互聯網
上載者:User

標籤:

GTest是Google開發的跨平台而且開源的C++單元測試架構,很好很強大。

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

關於GTest在Windows下使用,CoderZh給出了十分詳盡的使用指南:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 。

 

我的工作環境:Ubuntu12.04, python 2.7, Makefile,SVN等

  • 一、下載gtest檔案

我用的是svn下載的。

svn checkout http://googletest.googlecode.com/svn/trunk/ gtest-svn

得到最新版的gtest,成功可以看到檔案夾gtest-svn。

  • 二、產生gtest靜態庫

先進入檔案夾: 

$cd  gtest-svn

編譯產生gtest-all.o檔案(注意-I後無空格):

$g++ -I./include -I./  -c ./src/gtest-all.cc

再產生.a靜態庫檔案:

$ ar -rv libgtest.a gtest-all.o

若成功,會在目前的目錄下產生libgtest.a庫。可拷貝它到C++單元測試項目中去,以便使用。

  • 三、編寫簡易功能函數
  1. 建立一個項目(目錄)GTestApp,包含兩個代碼檔案: functions.h, functions.cpp。

實現兩個int變數的加減乘除功能。

//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、編寫單元測試代碼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、編寫測試代碼TestAll.cpp

//TestAll.cpp#include "gtest/gtest.h"#include <iostream>using namespace std;int main(int argc,char* argv[]){        //testing::GTEST_FLAG(output) = "xml:"; //若要產生xml結果檔案        testing::InitGoogleTest(&argc,argv); //初始化        RUN_ALL_TESTS();                     //跑單元測試        return 0;}
  • 五、編譯與運行測試

1、編譯

1) 複製gtest庫檔案

在GTestApp目錄下建立lib目錄,並複製libgtest.a到其中。

$mkdir lib

$cp <the path>/libgtest.a lib

2) 複製gtest標頭檔

gtest1.6.0目錄下的include包含了需使用到的標頭檔。把include複製到GTestApp中來。

3)編譯和連結

$ 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

連結:

$ g++ -o main *.o -I./include -L./lib -lgtest -lpthread      #注意不是-libgtest,同時需要用到 libpthread這個庫

最後可以得到一個main的可執行檔

2、運行與測試

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

 

如果需要得到xml檔案的話,可以這麼做

首先先在main函數開啟產生函數

testing::GTEST_FLAG(output) = "xml:"; 

再重新編譯,產生main可執行檔。再執行下面操作。

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

 

讓我們再看下目前的目錄,用ls命令:可以看到有一個main.xml的檔案,大功告成。

 

本文參考:

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使用(Google test)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.