{"id":206,"date":"2021-06-07T13:35:30","date_gmt":"2021-06-07T05:35:30","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=206"},"modified":"2025-10-09T13:56:21","modified_gmt":"2025-10-09T05:56:21","slug":"cpp-unit-testing-with-google-test","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/07\/cpp-unit-testing-with-google-test\/","title":{"rendered":"\u8f6f\u4ef6\u6d4b\u8bd5 01 \u4f7f\u7528Google Test\u8fdb\u884cC++\u5355\u5143\u6d4b\u8bd5"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001\u5982\u4f55\u4f7f\u7528Gtest<\/h2>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;VS2019\u5185\u7f6e\u4e86Gtest\uff0c\u53ea\u9700\u8981\u5728\u521b\u5efa\u9879\u76ee\u65f6\u9009\u62e9Gtest\u9879\u76ee\u5373\u53ef\u3002<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\u5b98\u65b9\u6307\u5357\uff1a<a href=\"https:\/\/google.github.io\/googletest\/\" title=\"GoogleTest User\u2019s Guide\" target=\"_blank\"  rel=\"nofollow\" >GoogleTest User\u2019s Guide<\/a><\/p>\n<h2>\u4e8c\u3001\u5feb\u901f\u4e0a\u624b<\/h2>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u6211\u4eec\u9700\u8981\u7684\u5934\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-cpp\">#include &quot;gtest\/gtest.h&quot;<\/code><\/pre>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u4f7f\u7528\u6a21\u677fvs\u4f1a\u4e3a\u4f60\u521b\u5efapch.h\u548cpch.cpp\u6587\u4ef6\uff0c\u4e0a\u8ff0\u5f15\u7528\u5df2\u5305\u542b\uff0c\u540c\u65f6\u5df2\u7ecf\u4e3a\u4f60\u751f\u6210test.cpp\u6587\u4ef6\u3002\u4e0b\u9762\u8ba9\u6211\u4eec\u5f00\u59cb\u5199\u6211\u4eec\u7684\u6d4b\u8bd5\u7a0b\u5e8f\uff1a<\/p>\n<h3>2.1 \u6d4b\u8bd5\u4ee3\u7801\u7684\u4e3b\u51fd\u6570<\/h3>\n<pre><code class=\"language-cpp\">int main(int argc, char* argv[])\n{\n    testing::InitGoogleTest(&amp;argc, argv);\n    return RUN_ALL_TESTS();\n}<\/code><\/pre>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u4e0a\u9762\u5c31\u662f\u6211\u4eec\u6d4b\u8bd5\u7a0b\u5e8f\u4e3b\u51fd\u6570\u8fd0\u884c\u9700\u8981\u7684\u57fa\u672c\u51fd\u6570\uff0c\u4e00\u4e2a\u521d\u59cb\u5316\u3001\u4e00\u4e2a\u8fd4\u56de\uff08\u8fd0\u884c\u6d4b\u8bd5\uff09\u3002\u4e0b\u9762\u8ba9\u6211\u6765\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u88ab\u6d4b\u51fd\u6570\u3002<\/p>\n<h3>2.2 \u88ab\u6d4b\u51fd\u6570<\/h3>\n<pre><code class=\"language-cpp\">int Plus5(const int&amp; p)\n{\n    return p + 5;\n}<\/code><\/pre>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u5728\u5b8c\u6210\u8fd9\u4e00\u6b65\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u5199\u4e00\u4e2a\u6d4b\u8bd5\u51fd\u6570\u6765\u6d4b\u8bd5\u6211\u4eec\u7684\u88ab\u6d4b\u4ee3\u7801\u3002<\/p>\n<h3>2.3 \u6d4b\u8bd5\u7a0b\u5e8f<\/h3>\n<pre><code class=\"language-cpp\">TEST(TestCaseName, TestName)\n{\n    int ParaNum(5), ExpNum(10);\n    EXPECT_EQ(ExpNum, Plus5(ParaNum));\n}<\/code><\/pre>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u5173\u4e8eAssert\u548cExpect\u7684\u90e8\u5206\u5c06\u653e\u5728\u6587\u7ae0\u6700\u540e\uff0c\u4e0b\u9762\u662f\u5b83\u4eec\u5b8c\u6574\u7684\u6837\u5b50\uff1a<\/p>\n<pre><code class=\"language-cpp\">#include &quot;pch.h&quot;\n\nint Plus5(const int&amp; p)\n{\n    return p + 5;\n}\n\nTEST(TestCaseName, TestName)\n{\n    int ParaNum(5), ExpNum(10);\n    EXPECT_EQ(ExpNum, Plus5(ParaNum));\n}\n\nint main(int argc, char* argv[])\n{\n    testing::InitGoogleTest(&amp;argc, argv);\n    return RUN_ALL_TESTS();\n}<\/code><\/pre>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u63a5\u4e0b\u6765\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\uff0c\u8f93\u51fa\u5982\u4e0a\u56fe\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-cpp\">[==========] Running 1 test from 1 test case.\n[----------] Global test environment set-up.\n[----------] 1 test from TestCaseName\n[ RUN      ] TestCaseName.TestName\n[       OK ] TestCaseName.TestName (0 ms)\n[----------] 1 test from TestCaseName (0 ms total)\n\n[----------] Global test environment tear-down\n[==========] 1 test from 1 test case ran. (18 ms total)\n[  PASSED  ] 1 test.<\/code><\/pre>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u5b8c\u6210\u4e86\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u3002\u53ef\u89c6\u5316\u65b9\u6848\uff1a\u6253\u5f00\u83dc\u5355 \u6d4b\u8bd5-&gt;Windows-&gt;\u6d4b\u8bd5\u8d44\u6e90\u7ba1\u7406\u5668\u3002<\/p>\n<h2>\u4e09\u3001Gtest\u7684\u65ad\u8a00<\/h2>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;gtest \u4f7f\u7528\u4e00\u7cfb\u5217\u65ad\u8a00\u7684\u5b8f\u6765\u68c0\u67e5\u503c\u662f\u5426\u7b26\u5408\u9884\u671f\uff0c\u4e3b\u8981\u5206\u4e3a\u4e24\u7c7b\uff1aASSERT \u548c EXPECT\u3002\u533a\u522b\u5728\u4e8e ASSERT \u4e0d\u901a\u8fc7\u7684\u65f6\u5019\u4f1a\u8ba4\u4e3a\u662f\u4e00\u4e2a fatal \u7684\u9519\u8bef\uff0c\u9000\u51fa\u5f53\u524d\u51fd\u6570\uff08\u53ea\u662f\u51fd\u6570\uff09\u3002\u800c EXPECT \u5931\u8d25\u7684\u8bdd\u4f1a\u7ee7\u7eed\u8fd0\u884c\u5f53\u524d\u51fd\u6570\uff0c\u6240\u4ee5\u5bf9\u4e8e\u51fd\u6570\u5185\u51e0\u4e2a\u5931\u8d25\u53ef\u4ee5\u540c\u65f6\u62a5\u544a\u51fa\u6765\u3002\u901a\u5e38\u6211\u4eec\u7528 EXPECT \u7ea7\u522b\u7684\u65ad\u8a00\u5c31\u597d\uff0c\u9664\u975e\u4f60\u8ba4\u4e3a\u5f53\u524d\u68c0\u67e5\u70b9\u5931\u8d25\u540e\u51fd\u6570\u7684\u540e\u7eed\u68c0\u67e5\u6ca1\u6709\u610f\u4e49\u3002<\/p>\n<h3>3.1 \u57fa\u7840\u65ad\u8a00<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">Fatal Assertion<\/th>\n<th style=\"text-align: center;\">Nonfatal assertion<\/th>\n<th style=\"text-align: center;\">Verifies<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">ASSERT_TRUE(condition);<\/td>\n<td style=\"text-align: center;\">EXPECT_TRUE(condition);<\/td>\n<td style=\"text-align: center;\">condition is true<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ASSERT_FALSE(condition);<\/td>\n<td style=\"text-align: center;\">EXPECT_FALSE(condition);<\/td>\n<td style=\"text-align: center;\">condition is false<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3.2 \u4e8c\u5143\u6bd4\u8f83<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">Fatal Assertion<\/th>\n<th style=\"text-align: center;\">Nonfatal assertion<\/th>\n<th style=\"text-align: center;\">Verifies<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">ASSERT_EQ(val1, val2);<\/td>\n<td style=\"text-align: center;\">EXPECT_EQ(val1, val2);<\/td>\n<td style=\"text-align: center;\">val1 == val2<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ASSERT_NE(val1, val2);<\/td>\n<td style=\"text-align: center;\">EXPECT_NE(val1, val2);<\/td>\n<td style=\"text-align: center;\">val1 != val2<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ASSERT_LT(val1, val2);<\/td>\n<td style=\"text-align: center;\">EXPECT_LT(val1, val2);<\/td>\n<td style=\"text-align: center;\">val1 &lt; val2<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ASSERT_LE(val1, val2);<\/td>\n<td style=\"text-align: center;\">EXPECT_LE(val1, val2);<\/td>\n<td style=\"text-align: center;\">val1 &lt;= val2<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ASSERT_GT(val1, val2);<\/td>\n<td style=\"text-align: center;\">EXPECT_GT(val1, val2);<\/td>\n<td style=\"text-align: center;\">val1 &gt; val2<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ASSERT_GE(val1, val2);<\/td>\n<td style=\"text-align: center;\">EXPECT_GE(val1, val2);<\/td>\n<td style=\"text-align: center;\">val1 &gt;= val2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3.3 \u5b57\u7b26\u4e32\u6bd4\u8f83<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">Fatal Assertion<\/th>\n<th style=\"text-align: center;\">Nonfatal assertion<\/th>\n<th style=\"text-align: center;\">Verifies<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">ASSERT_STREQ(str1,str2);<\/td>\n<td style=\"text-align: center;\">EXPECT_STREQ(str1,str2);<\/td>\n<td style=\"text-align: center;\">the two C strings have the same content<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ASSERT_STRNE(str1,str2);<\/td>\n<td style=\"text-align: center;\">EXPECT_STRNE(str1,str2);<\/td>\n<td style=\"text-align: center;\">the two C strings have different content<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ASSERT_STRCASEEQ(str1,str2);<\/td>\n<td style=\"text-align: center;\">EXPECT_STRCASEEQ(str1,str2);<\/td>\n<td style=\"text-align: center;\">the two C strings have the same content, ignoring case<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">ASSERT_STRCASENE(str1,str2);<\/td>\n<td style=\"text-align: center;\">EXPECT_STRCASENE(str1,str2);<\/td>\n<td style=\"text-align: center;\">the two C strings have different contents, ignoring case<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5982\u4f55\u4f7f\u7528Gtest &nbsp;&nbsp;&nbsp;&nbsp;VS2019\u5185\u7f6e\u4e86Gtest\uff0c\u53ea\u9700\u8981\u5728\u521b\u5efa\u9879\u76ee\u65f6\u9009\u62e9Gte &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/206"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":1042,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/206\/revisions\/1042"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}