我搜尋了谷歌測試使用 muon,但看起來 ubuntu 沒有它的軟體包。我需要使用來源安裝嗎?
答案1
新的資訊:
值得注意的是 libgtest0 不再存在。截至 2013 年左右(我不確定更改的日期),請參閱此問題:
2012年之前的舊答案:
它位於 Ubuntu 儲存庫中
sudo apt-get install libgtest0 libgtest-dev
也可以看看man gtest 配置
答案2
最小可運行範例
由於 Debian/Ubuntu 拒絕打包預建置版本,如下所述:為什麼沒有安裝用於谷歌測試的庫檔案?我將自己克隆並構建它(或者在實際專案中,將其添加為子模組):
git clone https://github.com/google/googletest
cd googletest
git checkout b1fbd33c06cdb0024c67733c6fdec2009d17b384
mkdir build
cd build
cmake ..
make -j`nproc`
cd ../..
然後我將它與我的測試文件一起使用main.cpp
:
g++ \
-Wall \
-Werror \
-Wextra \
-pedantic \
-O0 \
-I googletest/googletest/include \
-std=c++11 \
-o main.out \
main.cpp \
googletest/build/lib/libgtest.a \
-lpthread \
;
主機程式
#include <gtest/gtest.h>
int myfunc(int n) {
return n + 1;
}
TEST(asdfTest, HandlesPositiveInput) {
EXPECT_EQ(myfunc(1), 2);
EXPECT_EQ(myfunc(2), 3);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
得到預期的輸出:
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from asdfTest
[ RUN ] asdfTest.HandlesPositiveInput
[ OK ] asdfTest.HandlesPositiveInput (0 ms)
[----------] 1 test from asdfTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
或者,您也可以從文件main
中刪除該函數main.cpp
,而不是使用由提供的預設值libgtest_main.a
:
g++ \
-Wall \
-Werror \
-Wextra \
-pedantic \
-O0 \
-I googletest/googletest/include \
-std=c++11 \
-o main.out \
main.cpp \
googletest/build/lib/libgtest.a \
googletest/build/lib/libgtest_main.a \
-lpthread \
;
在 Ubuntu 20.04 上測試。