{"id":2103,"date":"2018-10-30T16:41:56","date_gmt":"2018-10-30T08:41:56","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=2103"},"modified":"2018-10-30T16:41:56","modified_gmt":"2018-10-30T08:41:56","slug":"google-c%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e6%a1%86%e6%9e%b6-gtest%e6%a1%86%e6%9e%b6%e7%ae%80%e4%bb%8b%ef%bc%88%e8%af%91%e6%96%87%ef%bc%89-2","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/2103","title":{"rendered":"Google C++\u5355\u5143\u6d4b\u8bd5\u6846\u67b6&#8212;Gtest\u6846\u67b6\u7b80\u4ecb\uff08\u8bd1\u6587\uff09"},"content":{"rendered":"<div id=\"header\">\n<div id=\"navigator\"><\/div>\n<\/div>\n<div id=\"main\">\n<div id=\"mainContent\">\n<div class=\"forFlow\">\n<div id=\"post_detail\">\n<div id=\"topics\">\n<div class=\"post\">\n<h2 class=\"clear\">\u4e00\u3001\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684\u6d4b\u8bd5\u9879\u76ee<\/h2>\n<div class=\"postBody\">\n<div id=\"cnblogs_post_body\" class=\"blogpost-body\">\n<p>\u00a0\u00a0\u5728\u7528google test\u5199\u6d4b\u8bd5\u9879\u76ee\u4e4b\u524d\uff0c\u9700\u8981\u5148\u7f16\u8bd1gtest\u5230library\u5e93\u5e76\u5c06\u6d4b\u8bd5\u4e0e\u5176\u94fe\u63a5\u3002\u6211\u4eec\u4e3a\u4e00\u4e9b\u6d41\u884c\u7684\u6784\u5efa\u7cfb\u7edf\u63d0\u4f9b\u4e86\u6784\u5efa\u6587\u4ef6:\u00a0<code>msvc\/<\/code>\u00a0for Visual Studio,\u00a0<code>xcode\/<\/code>\u00a0for Mac Xcode,\u00a0<code>make\/<\/code>\u00a0for GNU make,\u00a0<code>codegear\/<\/code>\u00a0for Borland C++ Builder.<\/p>\n<p>\u5982\u679c\u4f60\u7684\u6784\u5efa\u7cfb\u7edf\u4e0d\u5728\u8fd9\u4e2a\u540d\u5355\u4e0a\uff0c\u5728googletest\u6839\u76ee\u5f55\u6709autotools\u7684\u811a\u672c\uff08\u4e0d\u63a8\u8350\u4f7f\u7528\uff09\u548c<code>CMakeLists.txt<\/code>CMake\uff08\u63a8\u8350\uff09\u3002\u4f60\u53ef\u4ee5\u770b\u770bmake \/ Makefile\u6765\u4e86\u89e3\u5982\u4f55\u7f16\u8bd1Google Test\uff08\u57fa\u672c\u4e0a\u4f60\u60f3\u5728\u5934\u6587\u4ef6\u4e2d\u4f7f\u7528GTEST_ROOT\u548cGTEST_ROOT \/ include\u6765\u7f16\u8bd1src \/ gtest-all.cc\u8def\u5f84\uff0c\u5176\u4e2dGTEST_ROOT\u662fGoogle\u6d4b\u8bd5\u6839\u76ee\u5f55\uff09\u3002<\/p>\n<p>\u4e00\u65e6\u4f60\u80fd\u591f\u7f16\u8bd1google test\u5e93\uff0c\u60a8\u5e94\u8be5\u4e3a\u60a8\u7684\u6d4b\u8bd5\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\u6216\u6784\u5efa\u76ee\u6807\u3002Make sure you have\u00a0<code>GTEST_ROOT\/include<\/code>\u00a0in the header search path so that the compiler can find\u00a0<code>\"gtest\/gtest.h\"<\/code>\u00a0when compiling your test.\u628agoogle test\u5e93\u52a0\u5230\u4f60\u7684\u6d4b\u8bd5\u9879\u76ee\u4e2d\uff08\u6bd4\u5982\uff1a\u5728VS\u4e2d\u5728gtest.vcproj\u4e0a\u6dfb\u52a0\u4f9d\u8d56\uff09\u3002<\/p>\n<h2 id=\"autoid-1-0-0\">\u4e8c\u3001\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5f53\u4f7f\u7528\u8c37\u6b4c\u6d4b\u8bd5\uff0c\u60a8\u9996\u5148\u8981\u5199\u65ad\u8a00\uff0c\u65ad\u8a00\u662f\u68c0\u67e5\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u7684\u8bed\u53e5\u3002\u4e00\u4e2a\u65ad\u8a00\u7684\u7ed3\u679c\u53ef\u4ee5\u662f\u6210\u529f\uff0c\u975e\u81f4\u547d\u6027\u5931\u8d25\uff0c\u6216\u81f4\u547d\u7684\u5931\u8d25\u3002\u5982\u679c\u4e00\u4e2a\u81f4\u547d\u5931\u8d25\u51fa\u73b0\uff0c\u5b83\u4f1a\u7ec8\u6b62\u5f53\u524d\u7684\u51fd\u6570\uff1b\u5426\u5219\u7a0b\u5e8f\u7ee7\u7eed\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p>\u6d4b\u8bd5\u4f7f\u7528\u65ad\u8a00\u9a8c\u8bc1\u4ee3\u7801\u7684\u884c\u4e3a\u3002\u5982\u679c\u4e00\u4e2a\u6d4b\u8bd5\u5d29\u6e83\u6216\u8005\u6709\u4e00\u4e2a\u5931\u8d25\u7684\u65ad\u8a00\uff0c\u90a3\u4e48\u5931\u8d25\uff1b\u5426\u5219\u6210\u529f\u3002<\/p>\n<p>\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u6d4b\u8bd5\u3002\u00a0\u60a8\u5e94\u8be5\u5c06\u6d4b\u8bd5\u5206\u7ec4\u4e3a\u53cd\u6620\u6d4b\u8bd5\u4ee3\u7801\u7ed3\u6784\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002\u5f53\u6d4b\u8bd5\u7528\u4f8b\u4e2d\u7684\u591a\u4e2a\u6d4b\u8bd5\u9700\u8981\u5171\u4eab\u516c\u5171\u7684\u5bf9\u8c61\u548c\u5b50\u7a0b\u5e8f\u65f6\uff0c\u4f60\u53ef\u4ee5\u628a\u5b83\u4eec\u653e\u8fdb\u4e00\u4e2a<em>test fixture\u00a0<\/em>class\uff08\u6d4b\u8bd5\u5939\u5177\u7c7b\uff09\u3002<\/p>\n<p>\u4e00\u4e2a\u6d4b\u8bd5\u7a0b\u5e8f\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u5c06\u89e3\u91ca\u5982\u4f55\u7f16\u5199\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u4ece\u5355\u4e2a\u65ad\u8a00\u7ea7\u522b\u5f00\u59cb\uff0c\u5e76\u6784\u5efa\u6d4b\u8bd5\u548c\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<h2 id=\"autoid-2-0-0\">\u4e09\u3001\u65ad\u8a00<\/h2>\n<p>Google Test\u65ad\u8a00\u662f\u7c7b\u4f3c\u4e8e\u51fd\u6570\u8c03\u7528\u7684\u5b8f\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5bf9\u5176\u884c\u4e3a\u8fdb\u884c\u65ad\u8a00\u6765\u6d4b\u8bd5\u7c7b\u6216\u51fd\u6570\u3002\u5f53\u65ad\u8a00\u5931\u8d25\u65f6\uff0cGoogle Test\u4f1a\u6253\u5370\u65ad\u8a00\u7684\u6e90\u6587\u4ef6\u548c\u884c\u53f7\u4f4d\u7f6e\u4ee5\u53ca\u5931\u8d25\u6d88\u606f\u3002\u60a8\u8fd8\u53ef\u4ee5\u63d0\u4f9b\u81ea\u5b9a\u4e49\u5931\u8d25\u6d88\u606f\uff0c\u8be5\u6d88\u606f\u5c06\u9644\u52a0\u5230Google\u6d4b\u8bd5\u7684\u4fe1\u606f\u4e2d\u3002<\/p>\n<p>\u65ad\u8a00\u662f\u6210\u5bf9\u7684\uff0c\u6d4b\u8bd5\u540c\u4e00\u4ef6\u4e8b\uff0c\u4f46\u5bf9\u5f53\u524d\u51fd\u6570\u6709\u4e0d\u540c\u7684\u5f71\u54cd\u3002 ASSERT_ *\u7248\u672c\u5728\u5931\u8d25\u65f6\u4f1a\u751f\u6210\u81f4\u547d\u9519\u8bef\uff0c\u5e76\u4e2d\u6b62\u5f53\u524d\u51fd\u6570\u3002<strong>\u00a0EXPECT_ *<\/strong>\u7248\u672c\u751f\u6210\u975e\u81f4\u547d\u6027\u6545\u969c\uff0c\u4e0d\u4f1a\u4e2d\u6b62\u5f53\u524d\u51fd\u6570\u3002\u901a\u5e38\u4f18\u5148\u4f7f\u7528EXPECT_ *\uff0c\u56e0\u4e3a\u5b83\u4eec\u5141\u8bb8\u5728\u6d4b\u8bd5\u4e2d\u62a5\u544a\u591a\u4e2a\u6545\u969c\u3002\u4f46\u662f\uff0c\u5982\u679c\u5931\u8d25\u65f6\u51fd\u6570\u7ee7\u7eed\u8fd0\u884c\u6ca1\u6709\u610f\u4e49\uff0c\u5219\u5e94\u4f7f\u7528ASSERT_ *\u3002<\/p>\n<p>\u56e0\u4e3a\u5931\u8d25\u7684ASSERT_ *\u7acb\u5373\u4ece\u5f53\u524d\u51fd\u6570\u8fd4\u56de\uff0c\u53ef\u80fd\u8df3\u8fc7\u5176\u540e\u7684\u6e05\u7406\u4ee3\u7801\uff0c\u5b83\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u3002\u6839\u636e\u6cc4\u6f0f\u7684\u6027\u8d28\uff0c\u5b83\u53ef\u80fd\u503c\u5f97\u4fee\u590d\u4e5f\u53ef\u80fd\u4e0d\u503c\u5f97\u4fee\u590d&#8211;\u6240\u4ee5\u628a\u8fd9\u4e2a\u8bb0\u5728\u5fc3\u91cc\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5806\u68c0\u6d4b\u9519\u8bef\u9700\u8981\u6ce8\u610f\u662f\u4ec0\u4e48\u5bfc\u81f4\u7684\u3002<\/p>\n<p>\u8981\u63d0\u4f9b\u81ea\u5b9a\u4e49<strong>\u5931\u8d25<\/strong>\u6d88\u606f\uff0c\u53ea\u9700\u4f7f\u7528&lt;&lt;\u8fd0\u7b97\u7b26\u6216\u4e00\u7cfb\u5217\u6b64\u7c7b\u8fd0\u7b97\u7b26\u5c06\u5176\u6d41\u5f0f\u4f20\u8f93\u5230\u5b8f\u4e2d\u5373\u53ef\u3002\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">ASSERT_EQ(x.size(), y.size()) &lt;&lt;\u00a0\"Vectors x and y are of unequal length\";\r\n\r\nfor\u00a0(int\u00a0i =\u00a00; i &lt; x.size(); ++i) {\r\n\u00a0\u00a0EXPECT_EQ(x[i], y[i]) &lt;&lt;\u00a0\"Vectors x and y differ at index \"\u00a0&lt;&lt; i;\r\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"post\">\n<div class=\"postBody\">\n<div id=\"cnblogs_post_body\" class=\"blogpost-body\">\n<p>\u3000\u3000\u793a\u4f8b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">EXPECT_EQ(0, strcmp(s.c_string(), kHelloString2)) &lt;&lt;\u00a0\"s.c_string:\"\u00a0&lt;&lt; s.c_string() &lt;&lt;\u00a0\" kHelloString:\"\u00a0&lt;&lt; +kHelloString ;<\/pre>\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_68601\" class=\"syntaxhighlighter  java\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2104\" src=\"https:\/\/damogame.cn:15443\/wordpress\/wp-content\/uploads\/2018\/10\/747969-20161112215131014-1888487665.png\" alt=\"\" width=\"644\" height=\"120\" \/><\/div>\n<\/div>\n<\/div>\n<p>\u4efb\u4f55\u53ef\u4ee5\u6d41\u5f0f\u4f20\u8f93\u5230ostream\u7684\u4e1c\u897f\u90fd\u53ef\u4ee5\u6d41\u5f0f\u4f20\u8f93\u5230\u65ad\u8a00\u5b8f\uff0c\u7279\u522b\u662fC\u5b57\u7b26\u4e32\u548c\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002 \u5982\u679c\u4e00\u4e2a\u5bbd\u5b57\u7b26\u4e32\uff08Windows\u4e0a\u7684wchar_t *\uff0cTCHAR *\u5728UNICODE\u6a21\u5f0f\u4e0b\uff0c\u6216\u8005std :: wstring\uff09\u88ab\u6d41\u5316\u5230\u4e00\u4e2a\u65ad\u8a00\uff0c\u5f53\u6253\u5370\u65f6\u5b83\u5c06\u88ab\u8f6c\u6362\u4e3aUTF-8\u3002<\/p>\n<h2 id=\"autoid-3-0-0\">\u56db\u3001\u57fa\u672c\u65ad\u8a00<\/h2>\n<p>\u8fd9\u4e9b\u65ad\u8a00\u505a\u57fa\u672c\u7684\u771f\/\u5047\u6761\u4ef6\u6d4b\u8bd5\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">Fatal assertion<\/th>\n<th align=\"left\">Nonfatal assertion<\/th>\n<th align=\"left\">Verifies<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\"><code>ASSERT_TRUE(<\/code><em>condition<\/em><code>)<\/code>;<\/td>\n<td align=\"left\"><code>EXPECT_TRUE(<\/code><em>condition<\/em><code>)<\/code>;<\/td>\n<td align=\"left\"><em>condition<\/em>\u00a0is true<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>ASSERT_FALSE(<\/code><em>condition<\/em><code>)<\/code>;<\/td>\n<td align=\"left\"><code>EXPECT_FALSE(<\/code><em>condition<\/em><code>)<\/code>;<\/td>\n<td align=\"left\"><em>condition<\/em>\u00a0is false<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8bb0\u4f4f\uff0c\u5f53\u5b83\u4eec\u5931\u8d25\u65f6\uff0cASSERT_ *\u4ea7\u751f\u81f4\u547d\u5931\u8d25\u5e76\u4ece\u5f53\u524d\u51fd\u6570\u8fd4\u56de\uff0c\u800cEXPECT_ *\u4ea7\u751f\u975e\u81f4\u547d\u5931\u8d25\uff0c\u5141\u8bb8\u51fd\u6570\u7ee7\u7eed\u8fd0\u884c\u3002 \u5728\u4efb\u4e00\u60c5\u51b5\u4e0b\uff0c\u65ad\u8a00\u5931\u8d25\u610f\u5473\u7740\u5176\u5305\u542b\u7684\u6d4b\u8bd5\u5931\u8d25\u3002<\/p>\n<h2 id=\"autoid-4-0-0\">\u4e94\u3001\u4e8c\u8fdb\u5236\u6bd4\u8f83<\/h2>\n<p>\u672c\u8282\u4ecb\u7ecd\u6bd4\u8f83\u4e24\u4e2a\u503c\u7684\u65ad\u8a00\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">Fatal assertion<\/th>\n<th align=\"left\">Nonfatal assertion<\/th>\n<th align=\"left\">Verifies<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\"><code>ASSERT_EQ(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_EQ(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><em>val1<\/em>\u00a0<code>==<\/code>\u00a0<em>val2<\/em><\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>ASSERT_NE(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_NE(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><em>val1<\/em>\u00a0<code>!=<\/code>\u00a0<em>val2<\/em><\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>ASSERT_LT(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_LT(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><em>val1<\/em>\u00a0<code>&lt;<\/code>\u00a0<em>val2<\/em><\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>ASSERT_LE(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_LE(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><em>val1<\/em>\u00a0<code>&lt;=<\/code>\u00a0<em>val2<\/em><\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>ASSERT_GT(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_GT(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><em>val1<\/em>\u00a0<code>&gt;<\/code>\u00a0<em>val2<\/em><\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>ASSERT_GE(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_GE(<\/code><em>val1<\/em><code>,<\/code><em>val2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><em>val1<\/em>\u00a0<code>&gt;=<\/code>\u00a0<em>val2<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u53d1\u751f\u6545\u969c\u65f6\uff0cGoogle\u6d4b\u8bd5\u540c\u65f6\u6253\u5370val1\u548cval2\u3002<\/p>\n<p>\u503c\u53c2\u6570\u901a\u8fc7\u65ad\u8a00\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5fc5\u987b\u53ef\u4ee5\u6bd4\u8f83\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u7f16\u8bd1\u9519\u8bef\u3002\u6211\u4eec\u66fe\u7ecf\u8981\u6c42\u53c2\u6570\u652f\u6301&lt;&lt;\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u6d41\u4f20\u8f93\u5230ostream\uff0c\u4f46\u4ecev1.6.0\u5b83\u4e0d\u518d\u9700\u8981\uff08\u5982\u679c\u652f\u6301&lt;&lt;\uff0c\u5219\u4f1a\u5728\u65ad\u8a00\u5931\u8d25\u65f6\u8c03\u7528\u5b83\u6765\u6253\u5370\u53c2\u6570;\u5426\u5219Google Test\u5c06\u5c1d\u8bd5\u4ee5\u6700\u4f73\u65b9\u5f0f\u6253\u5370\u5b83\u4eec\u3002\u6709\u5173\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u548c\u5982\u4f55\u81ea\u5b9a\u4e49\u53c2\u6570\u7684\u6253\u5370\uff0c\u8bf7\u53c2\u9605\u6b64Google Mock\u00a0<a href=\"https:\/\/github.com\/google\/googletest\/blob\/master\/googlemock\/docs\/CookBook.md#teaching-google-mock-how-to-print-your-values\">recipe<\/a>.\u3002<\/p>\n<p>\u8fd9\u4e9b\u65ad\u8a00\u53ef\u4ee5\u4f7f\u7528\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u4f46\u662f\u53ea\u6709\u5f53\u4f60\u5b9a\u4e49\u4e86\u76f8\u5e94\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\u4f8b\u5982==\uff0c&lt;\uff0cetc\uff09\u3002\u5982\u679c\u5b9a\u4e49\u4e86\u76f8\u5e94\u7684\u64cd\u4f5c\u7b26\uff0c\u5219\u66f4\u559c\u6b22\u4f7f\u7528ASSERT _ *\uff08\uff09\u5b8f\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u4ec5\u4f1a\u6253\u5370\u6bd4\u8f83\u7ed3\u679c\uff0c\u800c\u4e14\u8fd8\u4f1a\u6253\u5370\u51fa\u4e24\u4e2a\u64cd\u4f5c\u6570\u3002<\/p>\n<p>\u53c2\u6570\u603b\u662f\u53ea\u8ba1\u7b97\u4e00\u6b21\u3002\u56e0\u6b64\uff0c\u53c2\u6570\u6709\u526f\u4f5c\u7528\u6ca1\u5173\u7cfb\u3002\u7136\u800c\uff0c\u4e0e\u4efb\u4f55\u666e\u901a\u7684C \/ C ++\u51fd\u6570\u4e00\u6837\uff0c\u53c2\u6570\u7684\u6c42\u503c\u987a\u5e8f\u662f\u672a\u5b9a\u4e49\u7684\uff08<strong>\u5373\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u4efb\u4f55\u987a\u5e8f<\/strong>\uff09\uff0c\u4f60\u7684\u4ee3\u7801\u4e0d\u5e94\u8be5\u4f9d\u8d56\u4e8e\u4efb\u4f55\u7279\u5b9a\u7684\u53c2\u6570\u6c42\u503c\u987a\u5e8f\u3002<\/p>\n<p>ASSERT_EQ\uff08\uff09\u6307\u9488\u7684\u6307\u9488\u76f8\u7b49\u3002\u5982\u679c\u5728\u4e24\u4e2aC\u5b57\u7b26\u4e32\u4e0a\u4f7f\u7528\uff0c\u5b83\u4f1a\u6d4b\u8bd5\u5b83\u4eec\u662f\u5426\u5728\u540c\u4e00\u4e2a\u5185\u5b58\u4f4d\u7f6e\uff0c\u800c\u4e0d\u662f\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u503c\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u6bd4\u8f83C\u5b57\u7b26\u4e32\uff08\u4f8b\u5982const char *\uff09\u7684\u503c\uff0c\u4f7f\u7528ASSERT_STREQ\uff08\uff09\uff0c\u7a0d\u540e\u5c06\u4f1a\u63cf\u8ff0\u3002\u7279\u522b\u5730\uff0c\u8981\u65ad\u8a00C\u5b57\u7b26\u4e32\u4e3aNULL\uff0c\u8bf7\u4f7f\u7528ASSERT_STREQ\uff08NULL\uff0cc_string\uff09\u3002\u4f46\u662f\uff0c\u8981\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u5e94\u8be5\u4f7f\u7528ASSERT_EQ\u3002<\/p>\n<p>\u672c\u8282\u4e2d\u7684\u5b8f\u9002\u7528\u4e8e\u7a84\u548c\u5bbd\u5b57\u7b26\u4e32\u5bf9\u8c61\uff08string\u548cwstring\uff09\u3002<\/p>\n<p>\u5386\u53f2\u8bb0\u5f55\uff1a2016\u5e742\u6708\u4e4b\u524d* _EQ\u6709\u4e00\u4e2a\u7ea6\u5b9a\uff0c\u79f0\u4e3aASSERT_EQ\uff08expected,actual\uff09\uff0c\u6240\u4ee5\u5f88\u591a\u73b0\u6709\u7684\u4ee3\u7801\u4f7f\u7528\u8fd9\u4e2a\u987a\u5e8f\u3002 \u73b0\u5728* _EQ\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5904\u7406\u8fd9\u4e24\u4e2a\u53c2\u6570\u3002<\/p>\n<h2 id=\"autoid-5-0-0\">\u516d\u3001\u5b57\u7b26\u4e32\u6bd4\u8f83<\/h2>\n<p>\u8be5\u7ec4\u4e2d\u7684\u65ad\u8a00\u6bd4\u8f83\u4e24\u4e2aC\u5b57\u7b26\u4e32\u7684\u503c\u3002 \u5982\u679c\u8981\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u8bf7\u6539\u7528EXPECT_EQ\uff0cEXPECT_NE\u548cetc\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">Fatal assertion<\/th>\n<th align=\"left\">Nonfatal assertion<\/th>\n<th align=\"left\">Verifies<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\"><code>ASSERT_STREQ(<\/code><em>str1<\/em><code>,<\/code><em>str2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_STREQ(<\/code><em>str1<\/em><code>,<\/code>_str_2<code>);<\/code><\/td>\n<td align=\"left\">the two C strings have the same content<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>ASSERT_STRNE(<\/code><em>str1<\/em><code>,<\/code><em>str2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_STRNE(<\/code><em>str1<\/em><code>,<\/code><em>str2<\/em><code>);<\/code><\/td>\n<td align=\"left\">the two C strings have different content<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>ASSERT_STRCASEEQ(<\/code><em>str1<\/em><code>,<\/code><em>str2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_STRCASEEQ(<\/code><em>str1<\/em><code>,<\/code><em>str2<\/em><code>);<\/code><\/td>\n<td align=\"left\">the two C strings have the same content, ignoring case(\u5ffd\u7565\u5927\u5c0f\u5199)<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>ASSERT_STRCASENE(<\/code><em>str1<\/em><code>,<\/code><em>str2<\/em><code>);<\/code><\/td>\n<td align=\"left\"><code>EXPECT_STRCASENE(<\/code><em>str1<\/em><code>,<\/code><em>str2<\/em><code>);<\/code><\/td>\n<td align=\"left\">the two C strings have different content, ignoring case<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>\u6ce8\u610f\uff0c\u65ad\u8a00\u540d\u4e2d\u7684\u201cCASE\u201d\u8868\u793a\u5ffd\u7565\u5927\u5c0f\u5199\u3002<\/p>\n<p>* STREQ *\u548c* STRNE *\u4e5f\u63a5\u53d7\u5bbdC\u5b57\u7b26\u4e32\uff08wchar_t *\uff09\u3002 \u5982\u679c\u4e24\u4e2a\u5bbd\u5b57\u7b26\u4e32\u7684\u6bd4\u8f83\u5931\u8d25\uff0c\u5b83\u4eec\u7684\u503c\u5c06\u6253\u5370\u4e3aUTF-8\u7a84\u5b57\u7b26\u4e32\u3002<\/p>\n<p>NULL\u6307\u9488\u548c\u7a7a\u5b57\u7b26\u4e32\u88ab\u8ba4\u4e3a\u662f\u4e0d\u540c\u7684\u3002<\/p>\n<p>\u53ef\u7528\u6027\uff1aLinux\uff0cWindows\uff0cMac\u3002<\/p>\n<p>\u53e6\u8bf7\u53c2\u9605\uff1a\u6709\u5173\u66f4\u591a\u5b57\u7b26\u4e32\u6bd4\u8f83\u6280\u5de7\uff08\u4f8b\u5982\uff0c\u5b50\u5b57\u7b26\u4e32\uff0c\u524d\u7f00\uff0c\u540e\u7f00\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff09\uff0c\u8bf7\u53c2\u89c1\u9ad8\u7ea7Google\u6d4b\u8bd5\u6307\u5357(<a href=\"https:\/\/github.com\/google\/googletest\/blob\/master\/googletest\/docs\/AdvancedGuide.md\">Advanced Google Test Guide<\/a>.)\u3002<\/p>\n<h2 id=\"autoid-6-0-0\">\u4e03\u3001\u7b80\u5355\u6d4b\u8bd5<\/h2>\n<p>\u521b\u5efa\u6d4b\u8bd5\uff1a<\/p>\n<p>1.\u4f7f\u7528TEST\uff08\uff09\u5b8f\u6765\u5b9a\u4e49\u548c\u547d\u540d\u6d4b\u8bd5\u51fd\u6570\uff0c\u8fd9\u4e9b\u662f\u4e0d\u8fd4\u56de\u503c\u7684\u666e\u901aC++\u51fd\u6570\u3002<br \/>\n2.\u5728\u6b64\u51fd\u6570\u4e2d\uff0c\u8fde\u540c\u8981\u5305\u62ec\u7684\u4efb\u4f55\u6709\u6548\u7684C++\u8bed\u53e5\uff0c\u4f7f\u7528\u5404\u79cdGoogle Test\u65ad\u8a00\u6765\u68c0\u67e5\u503c\u3002<br \/>\n3.\u6d4b\u8bd5\u7684\u7ed3\u679c\u7531\u65ad\u8a00\u786e\u5b9a; \u5982\u679c\u6d4b\u8bd5\u4e2d\u7684\u4efb\u4f55\u65ad\u8a00\u5931\u8d25\uff08\u81f4\u547d\u6216\u975e\u81f4\u547d\uff09\uff0c\u6216\u8005\u5982\u679c\u6d4b\u8bd5\u5d29\u6e83\uff0c\u5219 \u00a0 \u00a0 \u00a0 \u00a0\u6574\u4e2a\u6d4b\u8bd5\u5931\u8d25\u3002 \u5426\u5219\uff0c\u5b83\u6210\u529f\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">TEST(test_case_name, test_name) {\r\n... test body ...\r\n}<\/pre>\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_449264\" class=\"syntaxhighlighter  java\"><\/div>\n<\/div>\n<\/div>\n<p>TEST\uff08\uff09\u53c2\u6570\u4ece\u4e00\u822c\u5230\u7279\u5b9a\u3002 \u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6d4b\u8bd5\u7528\u4f8b\u7684\u540d\u79f0\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6d4b\u8bd5\u7528\u4f8b\u4e2d\u7684\u6d4b\u8bd5\u540d\u79f0\u3002 \u8fd9\u4e24\u4e2a\u540d\u79f0\u5fc5\u987b\u662f\u6709\u6548\u7684C ++\u6807\u8bc6\u7b26\uff0c\u5e76\u4e14\u5b83\u4eec\u4e0d\u5e94\u5305\u542b\u4e0b\u5212\u7ebf\uff08_\uff09\u3002 \u6d4b\u8bd5\u7684\u5168\u540d\u7531\u5176\u5305\u542b\u7684\u6d4b\u8bd5\u7528\u4f8b\u53ca\u5176\u4e2a\u4eba\u540d\u79f0\u7ec4\u6210\u3002\u6765\u81ea\u4e0d\u540c\u6d4b\u8bd5\u7528\u4f8b\u7684\u6d4b\u8bd5\u53ef\u4ee5\u5177\u6709\u76f8\u540c\u7684\u4e2a\u4eba\u540d\u79f0\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u6574\u6570\u51fd\u6570\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">int\u00a0Factorial(int\u00a0n);\u00a0\/\/ Returns the factorial of n\uff1bn\uff01<\/pre>\n<p>\u6b64\u51fd\u6570\u7684\u6d4b\u8bd5\u7528\u4f8b\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/ Tests factorial of 0.\r\nTEST(FactorialTest, HandlesZeroInput) {\r\nEXPECT_EQ(1, Factorial(0));\r\n}\r\n\r\n\/\/ Tests factorial of positive numbers.\r\nTEST(FactorialTest, HandlesPositiveInput) {\r\nEXPECT_EQ(1, Factorial(1));\r\nEXPECT_EQ(2, Factorial(2));\r\nEXPECT_EQ(6, Factorial(3));\r\nEXPECT_EQ(40320, Factorial(8));\r\n}<\/pre>\n<div id=\"cnblogs_post_body\" class=\"blogpost-body\">\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_750581\" class=\"syntaxhighlighter  java\"><\/div>\n<\/div>\n<\/div>\n<p>Google Test\u901a\u8fc7\u6d4b\u8bd5\u7528\u4f8b\u5bf9\u6d4b\u8bd5\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\uff0c\u56e0\u6b64\u903b\u8f91\u76f8\u5173\u7684\u6d4b\u8bd5\u5e94\u8be5\u5728\u540c\u4e00\u6d4b\u8bd5\u7528\u4f8b\u4e2d; \u6362\u53e5\u8bdd\u8bf4\uff0c\u5b83\u4eec\u7684TEST\uff08\uff09\u7684<strong>\u7b2c\u4e00\u4e2a\u53c2\u6570\u5e94\u8be5\u662f\u76f8\u540c\u7684<\/strong>\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u6d4b\u8bd5\uff0cHandlesZeroInput\u548cHandlesPositiveInput\uff0c\u5c5e\u4e8e\u540c\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8bFactorialTest\u3002<\/p>\n<h2 id=\"autoid-7-0-0\">\u516b\u3001\u6d4b\u8bd5\u5939\u5177(Test Fixtures)\uff1a\u5bf9\u591a\u4e2a\u6d4b\u8bd5\u4f7f\u7528\u76f8\u540c\u7684\u6570\u636e\u914d\u7f6e<\/h2>\n<p>\u5982\u679c\u4f60\u53d1\u73b0\u81ea\u5df1\u5199\u4e86\u4e24\u4e2a\u6216\u66f4\u591a\u7684\u6d4b\u8bd5\u6765\u64cd\u4f5c\u7c7b\u4f3c\u7684\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6d4b\u8bd5\u5939\u5177\u3002\u5b83\u5141\u8bb8\u60a8\u4e3a\u51e0\u4e2a\u4e0d\u540c\u7684\u6d4b\u8bd5\u91cd\u590d\u4f7f\u7528\u76f8\u540c\u7684\u5bf9\u8c61\u914d\u7f6e\u3002<\/p>\n<p>\u8981\u521b\u5efa\u5939\u5177\uff0c\u53ea\u9700\uff1a<\/p>\n<p>1.\u4ece:: testing :: Test\u6d3e\u751f\u4e00\u4e2a\u7c7b\u3002 \u4f7f\u7528protected\uff1a\u6216public\uff1a\u5f00\u59cb\u5b83\u7684\u4e3b\u4f53\uff0c\u56e0\u4e3a\u6211\u4eec\u60f3\u4ece\u5b50\u7c7b \u00a0 \u00a0 \u8bbf\u95eefixture\u6210\u5458\u3002<br \/>\n2.\u5728\u7c7b\u4e2d\uff0c\u58f0\u660e\u4f60\u6253\u7b97\u4f7f\u7528\u7684\u4efb\u4f55\u5bf9\u8c61\u3002<br \/>\n3.\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u7f16\u5199\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6216SetUp\uff08\uff09\u51fd\u6570\u6765\u4e3a\u6bcf\u4e2a\u6d4b\u8bd5\u51c6\u5907\u5bf9\u8c61\u3002 \u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u662f \u00a0 \u00a0 \u62fc\u5199SetUp\uff08\uff09\u4e3aSetup\uff08\uff09\u4e0e\u4e00\u4e2a\u5c0fu &#8212; \u4e0d\u8981\u8ba9\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\u5728\u4f60\u8eab\u4e0a\u3002<br \/>\n4.\u5982\u679c\u9700\u8981\uff0c\u5199\u4e00\u4e2a\u6790\u6784\u51fd\u6570\u6216TearDown\uff08\uff09\u51fd\u6570\u6765\u91ca\u653e\u4f60\u5728SetUp\uff08\uff09\u4e2d\u5206\u914d\u7684\u4efb\u4f55\u8d44\u6e90\u3002 \u8981 \u00a0 \u00a0 \u5b66\u4e60\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u6784\u9020\u51fd\u6570\/\u6790\u6784\u51fd\u6570\uff0c\u5f53\u4f60\u5e94\u8be5\u4f7f\u7528SetUp\uff08\uff09\/ TearDown\uff08\uff09\u65f6\uff0c\u8bf7\u9605\u8bfb\u8fd9\u4e2a\u00a0<a href=\"https:\/\/github.com\/google\/googletest\/blob\/master\/googletest\/docs\/FAQ.md#should-i-use-the-constructordestructor-of-the-test-fixture-or-the-set-uptear-down-function\">FAQ entry<\/a>.\u3002<br \/>\n5.\u5982\u679c\u9700\u8981\uff0c\u5b9a\u4e49\u8981\u5206\u4eab\u7684\u6d4b\u8bd5\u7684\u5b50\u7a0b\u5e8f\u3002<\/p>\n<p>\u5f53\u4f7f\u7528\u5939\u5177\u65f6\uff0c\u4f7f\u7528TEST_F\uff08\uff09\u800c\u4e0d\u662fTEST\uff08\uff09\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u60a8\u8bbf\u95ee\u6d4b\u8bd5\u5939\u5177\u4e2d\u7684\u5bf9\u8c61\u548c\u5b50\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">TEST_F(test_case_name, test_name) {\r\n... test body ...\r\n}<\/pre>\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_244693\" class=\"syntaxhighlighter  java\"><\/div>\n<\/div>\n<\/div>\n<p>\u548cTEST\uff08\uff09\u4e00\u6837\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6d4b\u8bd5\u7528\u4f8b\u540d\uff0c<\/p>\n<p>\u4f46\u662f\u5bf9\u4e8eTEST_F\uff08\uff09\u7b2c\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u662f<strong>\u6d4b\u8bd5\u5939\u5177\u7c7b\u7684\u540d\u79f0<\/strong>\u3002 \u4f60\u53ef\u80fd\u731c\u5230\u4e86\uff1a_F\u662f\u5939\u5177\u3002<\/p>\n<p>\u4e0d\u5e78\u7684\u662f\uff0cC ++\u5b8f\u7cfb\u7edf\u4e0d\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u5904\u7406\u4e24\u79cd\u7c7b\u578b\u7684\u6d4b\u8bd5\u7684\u5b8f\u3002 \u4f7f\u7528\u9519\u8bef\u7684\u5b8f\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u9519\u8bef\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5728TEST_F\uff08\uff09\u4e2d\u4f7f\u7528\u5b83\u4e4b\u524d\uff0c\u4f60\u5fc5\u987b\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u6d4b\u8bd5\u5939\u5177\u7c7b\uff0c\u5426\u5219\u5c06\u5f97\u5230\u7f16\u8bd1\u5668\u9519\u8bef\u201cvirtual outside class declaration\u201d\u3002<\/p>\n<p>\u5bf9\u4e8e\u4f7f\u7528TEST_F\uff08\uff09\u5b9a\u4e49\u7684\u6bcf\u4e2a\u6d4b\u8bd5\uff0cGoogle Test\u5c06\uff1a<\/p>\n<p>1.\u5728\u8fd0\u884c\u65f6\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6d4b\u8bd5\u5939\u5177<br \/>\n2.\u7acb\u5373\u901a\u8fc7SetUp\uff08\uff09\u521d\u59cb\u5316\uff0c<br \/>\n3.\u8fd0\u884c\u6d4b\u8bd5<br \/>\n4.\u901a\u8fc7\u8c03\u7528TearDown\uff08\uff09\u6e05\u9664<br \/>\n5.\u5220\u9664\u6d4b\u8bd5\u5939\u5177\u3002\u00a0<strong>\u8bf7\u6ce8\u610f<\/strong>\uff0c\u540c\u4e00\u6d4b\u8bd5\u7528\u4f8b\u4e2d\u7684\u4e0d\u540c\u6d4b\u8bd5\u5177\u6709\u4e0d\u540c\u7684\u6d4b\u8bd5\u5939\u5177\u5bf9\u8c61\uff0cGoogle\u6d4b\u8bd5\u59cb \u00a0 \u00a0 \u7ec8\u4f1a\u5220\u9664\u6d4b\u8bd5\u5939\u5177\uff0c\u7136\u540e\u518d\u521b\u5efa\u4e0b\u4e00\u4e2a\u6d4b\u8bd5\u5939\u5177\u3002 Google\u6d4b\u8bd5\u4e0d\u4f1a\u4e3a\u591a\u4e2a\u6d4b\u8bd5\u91cd\u590d\u4f7f\u7528\u76f8\u540c\u7684 \u00a0 \u00a0 \u00a0 \u6d4b\u8bd5\u5939\u5177\u3002<strong>\u4e00\u4e2a\u6d4b\u8bd5\u5bf9\u5939\u5177\u7684\u4efb\u4f55\u66f4\u6539\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u6d4b\u8bd5<\/strong>\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u4e3a\u540d\u4e3aQueue\u7684FIFO\u961f\u5217\u7c7b\u7f16\u5199\u6d4b\u8bd5\uff0c\u5b83\u6709\u4ee5\u4e0b\u63a5\u53e3\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">template &lt;typename E&gt;\u00a0\/\/ E is the element type.\r\nclass\u00a0Queue {\r\npublic:\r\nQueue();\r\nvoid\u00a0Enqueue(const\u00a0E&amp; element);\r\nE* Dequeue();\u00a0\/\/ Returns NULL if the queue is empty.\r\nsize_t size()\u00a0const;\r\n...\r\n};<\/pre>\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_17609\" class=\"syntaxhighlighter  java\"><\/div>\n<\/div>\n<\/div>\n<p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5939\u5177\u7c7b\u3002\u6309\u7167\u60ef\u4f8b\uff0c\u4f60\u5e94\u8be5\u7ed9\u5b83\u540d\u79f0FooTest\uff0c\u5176\u4e2dFoo\u662f\u88ab\u6d4b\u8bd5\u7684\u7c7b\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class\u00a0QueueTest :\u00a0public\u00a0::testing::Test {\r\nprotected:\r\nvirtual\u00a0void\u00a0SetUp() {\r\nq1_.Enqueue(1);\r\nq2_.Enqueue(2);\r\nq2_.Enqueue(3);\r\n}\r\n\r\n\/\/ virtual void TearDown() {}\r\n\r\nQueue&lt;int&gt; q0_;\r\nQueue&lt;int&gt; q1_;\r\nQueue&lt;int&gt; q2_;\r\n};<\/pre>\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_376038\" class=\"syntaxhighlighter  java\"><\/div>\n<\/div>\n<\/div>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0d\u9700\u8981TearDown\uff08\uff09\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5fc5\u5728\u6bcf\u6b21\u6d4b\u8bd5\u540e\u6e05\u7406\uff0c\u9664\u4e86\u6790\u6784\u51fd\u6570\u5df2\u7ecf\u505a\u4e86\u4ec0\u4e48\u3002<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u5c06\u4f7f\u7528TEST_F\uff08\uff09\u548c\u8fd9\u4e2a\u5939\u5177\u7f16\u5199\u6d4b\u8bd5\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">TEST_F(QueueTest, IsEmptyInitially) {\r\nEXPECT_EQ(0, q0_.size());\r\n}\r\n\r\nTEST_F(QueueTest, DequeueWorks) {\r\nint* n = q0_.Dequeue();\r\nEXPECT_EQ(NULL, n);\r\n\r\nn = q1_.Dequeue();\r\nASSERT_TRUE(n != NULL);\r\nEXPECT_EQ(1, *n);\r\nEXPECT_EQ(0, q1_.size());\r\ndelete n;\r\n\r\nn = q2_.Dequeue();\r\nASSERT_TRUE(n != NULL);\r\nEXPECT_EQ(2, *n);\r\nEXPECT_EQ(1, q2_.size());\r\ndelete n;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e0a\u9762\u4f7f\u7528ASSERT_ *\u548cEXPECT_ *\u65ad\u8a00\u3002\u00a0\u7ecf\u9a8c\u6cd5\u5219(\u00a0The rule of thumb\u00a0)\u662f\u5f53\u4f60\u5e0c\u671b\u6d4b\u8bd5\u5728\u65ad\u8a00\u5931\u8d25\u540e\u7ee7\u7eed\u663e\u793a\u66f4\u591a\u9519\u8bef\u65f6\u4f7f\u7528EXPECT_ *\uff0c\u6216\u662f\u5728\u5931\u8d25\u540e\u7ee7\u7eed\u4f7f\u7528ASSERT_ *\u6ca1\u6709\u610f\u4e49\u3002 \u4f8b\u5982\uff0cDequeue\u6d4b\u8bd5\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u65ad\u8a00\u662fASSERT_TRUE\uff08n\uff01= NULL\uff09\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u7a0d\u540e\u89e3\u5f15\u7528\u6307\u9488n\uff0c\u8fd9\u5c06\u5bfc\u81f4n\u4e3aNULL\u65f6\u7684segfault\u3002<\/p>\n<p>\u5f53\u8fd9\u4e9b\u6d4b\u8bd5\u8fd0\u884c\u65f6\uff0c\u4f1a\u53d1\u751f\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<p>1.Google Test\u6784\u9020\u4e86\u4e00\u4e2aQueueTest\u5bf9\u8c61\uff08\u6211\u4eec\u79f0\u4e4b\u4e3at1\uff09\u3002<br \/>\n2.t1.SetUp\uff08\uff09\u521d\u59cb\u5316t1\u3002<br \/>\n3.\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\uff08IsEmptyInitially\uff09\u5728t1\u4e0a\u8fd0\u884c\u3002<br \/>\n4.t1.TearDown\uff08\uff09\u5728\u6d4b\u8bd5\u5b8c\u6210\u540e\u6e05\u7406\u3002<br \/>\n5.t1\u88ab\u6790\u6784\u3002<br \/>\n6.\u4ee5\u4e0a\u6b65\u9aa4\u5728\u53e6\u4e00\u4e2aQueueTest\u5bf9\u8c61\u4e0a\u91cd\u590d\uff0c\u8fd9\u6b21\u8fd0\u884cDequeueWorks\u6d4b\u8bd5\u3002<\/p>\n<h2 id=\"autoid-8-0-0\">\u4e5d\u3001\u8c03\u7528\u6d4b\u8bd5<\/h2>\n<p>TEST\uff08\uff09\u548cTEST_F\uff08\uff09\u7528Google Test\u9690\u5f0f\u6ce8\u518c\u4ed6\u4eec\u7684\u6d4b\u8bd5\u3002 \u56e0\u6b64\uff0c\u4e0e\u8bb8\u591a\u5176\u4ed6C ++\u6d4b\u8bd5\u6846\u67b6\u4e0d\u540c\uff0c\u60a8\u4e0d\u5fc5\u91cd\u65b0\u5217\u51fa\u6240\u6709\u5b9a\u4e49\u7684\u6d4b\u8bd5\u4ee5\u8fd0\u884c\u5b83\u4eec\u3002<\/p>\n<p>\u5b9a\u4e49\u6d4b\u8bd5\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528RUN_ALL_TESTS\uff08\uff09\u8fd0\u884c\u5b83\u4eec\uff0c\u5982\u679c\u6240\u6709\u6d4b\u8bd5\u6210\u529f\u5219\u8fd4\u56de0\uff0c\u5426\u5219\u8fd4\u56de1\u3002 \u8bf7\u6ce8\u610f\uff0c<strong>RUN_ALL_TESTS\uff08\uff09\u8fd0\u884c\u94fe\u63a5\u5355\u5143\u4e2d\u7684\u6240\u6709\u6d4b\u8bd5\u00a0<\/strong>&#8211; \u5b83\u4eec\u53ef\u4ee5\u6765\u81ea\u4e0d\u540c\u7684\u6d4b\u8bd5\u7528\u4f8b\uff0c\u751a\u81f3\u662f\u4e0d\u540c\u7684\u6e90\u6587\u4ef6\u3002<\/p>\n<p>\u8c03\u7528\u65f6\uff0cRUN_ALL_TESTS\uff08\uff09\u5b8f\uff1a<\/p>\n<p>1. \u4fdd\u5b58\u6240\u6709Google\u6d4b\u8bd5\u6807\u8bb0\u7684\u72b6\u6001\u3002<br \/>\n2. \u4e3a\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\u521b\u5efa\u6d4b\u8bd5\u5939\u5177\u5bf9\u8c61\u3002<br \/>\n3. \u901a\u8fc7SetUp\uff08\uff09\u521d\u59cb\u5316\u5b83\u3002<br \/>\n4. \u5728fixture\u5bf9\u8c61\u4e0a\u8fd0\u884c\u6d4b\u8bd5\u3002<br \/>\n5. \u901a\u8fc7TearDown\uff08\uff09\u6e05\u9664\u5939\u5177\u3002<br \/>\n6. \u5220\u9664\u5939\u5177\u3002<br \/>\n7. \u6062\u590d\u6240\u6709Google\u6d4b\u8bd5\u6807\u5fd7\u7684\u72b6\u6001\u3002<br \/>\n8. \u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u8fdb\u884c\u4e0b\u4e00\u4e2a\u6d4b\u8bd5\uff0c\u76f4\u5230\u6240\u6709\u6d4b\u8bd5\u8fd0\u884c\u7ed3\u675f\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5982\u679c\u6d4b\u8bd5\u5939\u5177\u7684\u6784\u9020\u51fd\u6570\u5728\u6b65\u9aa42\u4e2d\u4ea7\u751f\u81f4\u547d\u6545\u969c\uff0c\u5219\u6b65\u9aa43-5\u6ca1\u6709\u610f\u4e49\uff0c\u56e0\u6b64\u5b83\u4eec\u88ab\u8df3\u8fc7\u3002 \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u6b65\u9aa43\u4ea7\u751f\u81f4\u547d\u6545\u969c\uff0c\u5219\u5c06\u8df3\u8fc7\u6b65\u9aa44\u3002<\/p>\n<p>\u91cd\u8981\uff1a\u60a8\u4e0d\u80fd\u5ffd\u7565RUN_ALL_TESTS\uff08\uff09\u7684\u8fd4\u56de\u503c\uff0c\u5426\u5219gcc\u5c06\u7ed9\u60a8\u4e00\u4e2a\u7f16\u8bd1\u5668\u9519\u8bef\u3002 \u6b64\u8bbe\u8ba1\u7684\u57fa\u672c\u539f\u7406\u662f\u81ea\u52a8\u6d4b\u8bd5\u670d\u52a1\u57fa\u4e8e\u5176\u9000\u51fa\u4ee3\u7801\u800c\u4e0d\u662f\u5176stdout \/ stderr\u8f93\u51fa\u6765\u786e\u5b9a\u6d4b\u8bd5\u662f\u5426\u5df2\u901a\u8fc7; \u56e0\u6b64\u60a8\u7684<strong>main\uff08\uff09\u51fd\u6570\u5fc5\u987b\u8fd4\u56deRUN_ALL_TESTS\uff08\uff09\u7684\u503c<\/strong>\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u60a8\u5e94\u8be5\u53ea\u8c03\u7528\u4e00\u6b21RUN_ALL_TESTS\uff08\uff09\u3002 \u591a\u6b21\u8c03\u7528\u5b83\u4f1a\u4e0e\u4e00\u4e9b\u9ad8\u7ea7Google\u6d4b\u8bd5\u529f\u80fd\uff08\u4f8b\u5982\u7ebf\u7a0b\u5b89\u5168\u6b7b\u4ea1\u6d4b\u8bd5\uff09\u51b2\u7a81\uff0c\u56e0\u6b64\u4e0d\u53d7\u652f\u6301\u3002<\/p>\n<h2 id=\"autoid-9-0-0\">\u00a0\u5341\u3001\u5199Main\u51fd\u6570<\/h2>\n<p>\u4f60\u53ef\u4ee5\u4ece\u8fd9\u4e2a\u6837\u677f\u5f00\u59cb\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#include\u00a0\"this\/package\/foo.h\"\r\n#include\u00a0\"gtest\/gtest.h\"\r\n\r\nnamespace {\r\n\/\/ The fixture for testing class Foo.\r\nclass\u00a0FooTest :\u00a0public\u00a0::testing::Test {\r\nprotected:\r\n\/\/ You can remove any or all of the following functions if its body\r\n\/\/ is empty.\r\n\r\nFooTest() {\r\n\/\/ You can do set-up work for each test here.\r\n}\r\n\r\nvirtual ~FooTest() {\r\n\/\/ You can do clean-up work that doesn't throw exceptions here.\r\n}\r\n\r\n\/\/ If the constructor and destructor are not enough for setting up\r\n\/\/ and cleaning up each test, you can define the following methods:\r\n\r\nvirtual\u00a0void\u00a0SetUp() {\r\n\/\/ Code here will be called immediately after the constructor (right\r\n\/\/ before each test).\r\n}\r\n\r\nvirtual\u00a0void\u00a0TearDown() {\r\n\/\/ Code here will be called immediately after each test (right\r\n\/\/ before the destructor).\r\n}\r\n\r\n\/\/ Objects declared here can be used by all tests in the test case for Foo.\r\n};\r\n\r\n\/\/ Tests that the Foo::Bar() method does Abc.\r\nTEST_F(FooTest, MethodBarDoesAbc) {\r\nconst\u00a0string input_filepath =\u00a0\"this\/package\/testdata\/myinputfile.dat\";\r\nconst\u00a0string output_filepath =\u00a0\"this\/package\/testdata\/myoutputfile.dat\";\r\nFoo f;\r\nEXPECT_EQ(0, f.Bar(input_filepath, output_filepath));\r\n}\r\n\r\n\/\/ Tests that Foo does Xyz.\r\nTEST_F(FooTest, DoesXyz) {\r\n\/\/ Exercises the Xyz feature of Foo.\r\n}\r\n\r\n}\u00a0\u00a0\/\/ namespace\r\n\r\nint\u00a0main(int\u00a0argc,\u00a0char\u00a0**argv) {\r\n::testing::InitGoogleTest(&amp;argc, argv);\r\nreturn\u00a0RUN_ALL_TESTS();\r\n}<\/pre>\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_825816\" class=\"syntaxhighlighter  java\"><\/div>\n<\/div>\n<\/div>\n<p>:: testing :: InitGoogleTest\uff08\uff09\u51fd\u6570\u89e3\u6790Google\u6d4b\u8bd5\u6807\u5fd7\u7684\u547d\u4ee4\u884c\uff0c\u5e76\u5220\u9664\u6240\u6709\u5df2\u8bc6\u522b\u7684\u6807\u5fd7\u3002 \u8fd9\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u5404\u79cd\u6807\u5fd7\u63a7\u5236\u6d4b\u8bd5\u7a0b\u5e8f\u7684\u884c\u4e3a\uff0c\u6211\u4eec\u5c06\u5728<a href=\"https:\/\/github.com\/google\/googletest\/blob\/master\/googletest\/docs\/AdvancedGuide.md\">AdvancedGuide<\/a>\u4e2d\u4ecb\u7ecd\u3002 \u5728\u8c03\u7528RUN_ALL_TESTS\uff08\uff09\u4e4b\u524d\u5fc5\u987b\u8c03\u7528\u6b64\u51fd\u6570\uff0c\u5426\u5219\u6807\u5fd7\u5c06\u65e0\u6cd5\u6b63\u786e\u521d\u59cb\u5316\u3002<\/p>\n<p>\u5728Windows\u4e0a\uff0cInitGoogleTest\uff08\uff09\u4e5f\u9002\u7528\u4e8e\u5bbd\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u5b83\u4e5f\u53ef\u4ee5\u5728\u4ee5UNICODE\u6a21\u5f0f\u7f16\u8bd1\u7684\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u4f46\u4e5f\u8bb8\u4f60\u8ba4\u4e3a\u7f16\u5199\u6240\u6709\u8fd9\u4e9bmain\uff08\uff09\u51fd\u6570\u662f\u592a\u591a\u7684\u5de5\u4f5c\uff1f \u6211\u4eec\u5b8c\u5168\u540c\u610f\u4f60\u7684\u770b\u6cd5\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48Google Test\u63d0\u4f9b\u4e86main\uff08\uff09\u7684\u57fa\u672c\u5b9e\u73b0\u3002 \u5982\u679c\u5b83\u9002\u5408\u4f60\u7684\u9700\u8981\uff0c\u7136\u540e\u53ea\u662f\u94fe\u63a5\u4f60\u7684\u6d4b\u8bd5\u4e0egtest_main\u5e93\u3002<\/p>\n<h2 id=\"autoid-10-0-0\">\u5341 \u4e00\u3001Visual C ++\u7528\u6237\u7684\u91cd\u8981\u8bf4\u660e<\/h2>\n<p>\u5982\u679c\u4f60\u628a\u4f60\u7684\u6d4b\u8bd5\u653e\u5165\u4e00\u4e2a\u5e93\uff0c\u4f60\u7684main\uff08\uff09\u51fd\u6570\u5728\u4e0d\u540c\u7684\u5e93\u6216\u5728\u4f60\u7684 .exe\u6587\u4ef6\u4e2d\uff0c\u8fd9\u4e9b\u6d4b\u8bd5\u5c06\u4e0d\u4f1a\u8fd0\u884c\u3002 \u539f\u56e0\u662fVisual C ++\u4e2d\u7684\u4e00\u4e2a\u9519\u8bef\u3002 \u5f53\u60a8\u5b9a\u4e49\u6d4b\u8bd5\u65f6\uff0cGoogle\u6d4b\u8bd5\u4f1a\u521b\u5efa\u4e00\u4e9b\u9759\u6001\u5bf9\u8c61\u6765\u6ce8\u518c\u5b83\u4eec\u3002 \u8fd9\u4e9b\u5bf9\u8c61\u6ca1\u6709\u4ece\u5176\u4ed6\u5730\u65b9\u5f15\u7528\uff0c\u4f46\u5b83\u4eec\u7684\u6784\u9020\u51fd\u6570\u4ecd\u7136\u5e94\u8be5\u8fd0\u884c\u3002 \u5f53Visual C ++\u94fe\u63a5\u5668\u53d1\u73b0\u5e93\u4e2d\u6ca1\u6709\u4ece\u5176\u4ed6\u5730\u65b9\u5f15\u7528\u65f6\uff0c\u5b83\u4f1a\u629b\u51fa\u8be5\u5e93\u3002 \u60a8\u5fc5\u987b\u901a\u8fc7\u4e3b\u7a0b\u5e8f\u4e2d\u7684\u6d4b\u8bd5\u6765\u5f15\u7528\u60a8\u7684\u5e93\uff0c\u4ee5\u9632\u6b62\u94fe\u63a5\u5668\u4e22\u5f03\u5b83\u3002 \u8fd9\u91cc\u662f\u5982\u4f55\u505a\u5230\u7684\u3002 \u5728\u4f60\u7684\u5e93\u4ee3\u7801\u4e2d\u58f0\u660e\u4e00\u4e2a\u51fd\u6570\uff1a<\/p>\n<p>\u3002\u3002\u3002\u3002\u3002\u7701\u7565\u5566\uff0c\u5982\u679c\u7528VC\u518d\u6765\u770b\u3002<\/p>\n<h2 id=\"autoid-11-0-0\">\u5341\u4e8c\u3001\u4ece\u8fd9\u91cc\u5230\u54ea\u91cc\u53bb<\/h2>\n<p>\u606d\u559c\uff01 \u60a8\u5df2\u5b66\u4e60\u4e86Google\u6d4b\u8bd5\u57fa\u7840\u77e5\u8bc6\u3002 \u60a8\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199\u548c\u8fd0\u884cGoogle Test\u6d4b\u8bd5\uff0c\u9605\u8bfb\u4e00\u4e9b\u793a\u4f8b\uff0c\u6216\u7ee7\u7eed\u9605\u8bfb<a href=\"https:\/\/github.com\/google\/googletest\/blob\/master\/googletest\/docs\/AdvancedGuide.md\">AdvancedGuide<\/a>\uff0c\u5176\u4e2d\u4ecb\u7ecd\u4e86\u66f4\u591a\u6709\u7528\u7684Google\u6d4b\u8bd5\u529f\u80fd\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"autoid-12-0-0\">\u5341\u4e09\u3001\u5df2\u77e5\u7684\u9650\u5236<\/h2>\n<p>Google\u6d4b\u8bd5\u65e8\u5728\u7ebf\u7a0b\u5b89\u5168\u3002 \u5728pthreads\u5e93\u53ef\u7528\u7684\u7cfb\u7edf\u4e0a\uff0c\u5b9e\u73b0\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002 \u76ee\u524d\uff0c\u5728\u5176\u4ed6\u7cfb\u7edf\uff08\u4f8b\u5982Windows\uff09\u4e0a\u540c\u65f6\u4f7f\u7528\u4e24\u4e2a\u7ebf\u7a0b\u7684Google Test\u65ad\u8a00\u662f\u4e0d\u5b89\u5168\u7684\u3002 \u5728\u5927\u591a\u6570\u6d4b\u8bd5\u4e2d\uff0c\u8fd9\u4e0d\u662f\u4e00\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u901a\u5e38\u65ad\u8a00\u662f\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u5b8c\u6210\u7684\u3002 \u5982\u679c\u4f60\u60f3\u5e2e\u52a9\uff0c\u4f60\u53ef\u4ee5\u5fd7\u613f\u4e3a\u60a8\u7684\u5e73\u53f0\u5728gtest-port.h\u4e2d\u5b9e\u73b0\u5fc5\u8981\u7684\u540c\u6b65\u539f\u8bed\u3002<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684\u6d4b\u8bd5\u9879\u76ee \u00a0\u00a0\u5728\u7528goo<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-2103","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=2103"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2103\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=2103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=2103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=2103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}