在TeamCity中执行gtest单元测试

1. 在Visual Studio 2017中新建一个gtest项目 Sample-Test1。这个项目会自动安装“Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn”这个nuget包。我们打开nuget管理工具,然后添加一个“teamcity-gtest-streamer”包

 2.在pch.h中添加头文件应用

#include "gtest/gtest.h"
#include "teamcity_gtest.h"

3. 修改test.cpp为以下内容

#include "pch.h"

TEST(TestCaseName, TestName) {
  EXPECT_EQ(1, 1);
  EXPECT_TRUE(true);
}

TEST(TestCaseName1, TestName1) {
    EXPECT_EQ(2, 2);
    EXPECT_TRUE(3 == 3);
}
TEST(TestCaseName1, TestName2) {
    EXPECT_EQ(2, 3);
    EXPECT_TRUE(3 == 3);
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);

    //if (jetbrains::teamcity::underTeamcity()) {
    //    ::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();
    //    // Add unique flowId parameter if you want to run test processes in parallel
    //    // See http://confluence.jetbrains.net/display/TCD6/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-MessageFlowId
    //    listeners.Append(new jetbrains::teamcity::TeamcityGoogleTestEventListener());
    //}

    //if (jetbrains::teamcity::underTeamcity()) {
        ::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();
        // Add unique flowId parameter if you want to run test processes in parallel
        // See http://confluence.jetbrains.net/display/TCD6/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-MessageFlowId
        listeners.Append(new GTest::TeamCityAdapter());
    //}

    return RUN_ALL_TESTS();
}

4. 在teamcity中新增一个项目,项目的配置如下

5. 等待运行结果,如下图所示,两个执行成功,一个执行失败

一起都已经完成。后续集成更多功能和单元测试即可。

 具体代码可以参照 https://github.com/jhy871167495/teamcity_gtest_demo

相关参考:teamcity-gtest-streamer bitbucket地址

https://github.com/JetBrains/teamcity-cpp

原文地址:https://www.cnblogs.com/Martianhh/p/10078429.html