VSCode + Google test 使用 (ubuntu环

2020-04-17  本文已影响0人  苹果农
  1. 安装 cmake
 $ apt-get install -y cmake 
 $ cmake --version
  1. 安装 google test
$ apt-get install -y libgtest-dev (或者 github上下载)
$ mkdir build
$ cd build
$ cmake 
$ make
$ make install
  1. 使用 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)
  1. 编写 task.json 和 launch.json 文件(略)
  2. 运行查看结果
[==========] 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
  1. 使用 GoogleTest Adapter
    安装 GoogleTest Adapter 后, 再插件安装界面,选择对应的 Launch 项目,
    会有如下界面:


    image-20200414100314508.png

    可以更直观的查看运行结果,和单独运行某个测试。

上一篇下一篇

猜你喜欢

热点阅读