- 安裝 cmake
$ apt-get install -y cmake
$ cmake --version
- 安裝 google test
$ apt-get install -y libgtest-dev (或者 github上下載)
$ mkdir build
$ cd build
$ cmake
$ make
$ make install
- 使用 google test
編寫代碼:test_target.cc
#include <iostream>
#include <gtest/gtest.h>
using namespace std;
?
int Foo(int a, int b)
{
return a + b;
}
?
TEST(FooTest, ZeroEqual)
{
ASSERT_EQ(0, 0);
}
?
TEST(FooTest, HandleNoneZeroInput)
{
EXPECT_EQ(12, Foo(4, 10));
EXPECT_EQ(6, Foo(30, 18));
}
?
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
makefile 文件
TARGET=test_target
all:
g++ -g test_target.cc -I /usr/include -L /usr/lib -lgtest -lgtest_main -lpthread -o $(TARGET)
clean:
rm -rf *.o $(TARGET)
- 編寫 task.json 和 launch.json 文件(略)
- 運行查看結(jié)果
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from FooTest
[ RUN ] FooTest.ZeroEqual
[ OK ] FooTest.ZeroEqual (0 ms)
[ RUN ] FooTest.HandleNoneZeroInput
test_target.cc:17: Failure
Expected: 12
To be equal to: Foo(4, 10)
Which is: 14
test_target.cc:18: Failure
Expected: 6
To be equal to: Foo(30, 18)
Which is: 48
[ FAILED ] FooTest.HandleNoneZeroInput (1 ms)
[----------] 2 tests from FooTest (1 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
[ FAILED ] 1 test, listed below:
[ FAILED ] FooTest.HandleNoneZeroInput
1 FAILED TEST
-
使用 GoogleTest Adapter
安裝 GoogleTest Adapter 后猎拨, 再插件安裝界面辖试,選擇對應(yīng)的 Launch 項目趟据,
會有如下界面:
可以更直觀的查看運行結(jié)果妇菱,和單獨運行某個測試竣稽。