gtest編寫第乙個測試用例出錯及其解決過程

2021-09-30 12:12:45 字數 2325 閱讀 7377

安裝好gtest後,編寫第乙個測試案例test_main.cpp

#include #include using namespace std;

int foo(int a,int b)

test(footest, zeroequal)

test(footest, handlenonezeroinput)

int main(int argc, char* ar**)

按照gtest的介紹makefile檔案為

target=test_main

all:

gtest-config --min-version=1.0 || echo "insufficient google test version."

g++ $(gtest-config --cppflags --cxxflags) -o $(target).o -c test_main.cpp

g++ $(gtest-config --ldflags --libs) -o $(target) $(target).o

clean:

rm -rf *.o $(target)

但是編譯的時候,出現錯誤

cxy-/home/chenxueyou/gtest$ make

gtest-config --min-version=1.0 || echo "insufficient google test version."

g++ -o test_main.o -c test_main.cpp

g++ -o test_main test_main.o

test_main.o: in function `footest_zeroequal_test::testbody()':

test_main.cpp:(.text+0x9e): undefined reference to `testing::internal::asserthelper::asserthelper(testing::testpartresult::type, char const*, int, char const*)'

...

省略了部分錯誤資訊,看到了undefined reference,編譯通過,但是鏈結失敗,可以猜測是沒有找到對應的庫。再仔細看實際執行時列印的命令為

g++  -o test_main.o -c test_main.cpp

g++ -o test_main test_main.o

很顯然,沒有引入gtest的標頭檔案,也沒有載入gtest對應的庫。

執行命令

>echo $(gtest-config --cppflags --cxxflags)

echo $(gtest-config --ldflags --libs)

可以得到gtest配置的標頭檔案路徑和庫檔案路徑。

cxy-/home/chenxueyou/gtest$ echo $(gtest-config --cppflags --cxxflags)

-i/usr/include -pthread

cxy-/home/chenxueyou/gtest$ echo $(gtest-config --ldflags --libs)

-l/usr/lib64 -lgtest -pthread

而在我們的makefile中執行時上面兩個命令的結果為空。所以修改makefile,手動指定標頭檔案路徑和庫檔案路徑,makefile為

target=test_main

all:

gtest-config --min-version=1.0 || echo "insufficient google test version."

g++ -i/usr/include -pthread -o $(target).o -c test_main.cpp

g++ -l/usr/lib64 -lgtest -pthread -o $(target) $(target).o

clean:

rm -rf *.o $(target)

這樣,我們的第乙個gtest測試檔案就能編譯通過了。

1.makefile實際執行的命令可能與預想的命令不一樣,要仔細檢視。

2.gtest通過標頭檔案和庫的方式引入工程,要指定其標頭檔案和庫檔案的位置

3.gtest-config命令能夠幫助我們找到對應的路徑

編寫乙個JPA測試用例

整合了jpa和mysql需要來測試一下是否可以成功對資料庫進行操作,這裡寫乙個簡單的單元測試來對之前配置的正確性進行驗證。依賴匯入 首先匯入需要的springtest包和junit單元測試包。org.springframework.bootgroupid spring boot starter te...

編寫第乙個VBA

1 vba過程結構 sub 過程名稱 注釋 end sub 2 插入模組 過程 函式 編輯器 插入 過程函式 這個自己手寫不是更好,雞肋功能?問題 1 報錯 excel 此文件中包含巨集 activex 控制項 xml擴充套件包資訊或 web元件,其中可能包含個人資訊,並且這些資訊不能通過 文件檢查...

編寫乙個簡單的單元測試用例

開發乙個簡單的計算器,用於計算兩個數的加減乘除,示例 1 class calculator 2 實現簡單的加減乘除 3 def init self,a,b 4 self.a int a 5 self.b int b 67 defadd self 8return self.a self.b910 de...