在TeamCity中執行gtest單元測試
阿新 • • 發佈:2018-12-06
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