{"id":42695,"date":"2024-12-01T12:07:24","date_gmt":"2024-12-01T04:07:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42695\/"},"modified":"2024-12-01T12:07:24","modified_gmt":"2024-12-01T04:07:24","slug":"%e5%a6%82%e4%bd%95%e4%bb%8e-go-test-cover-%e8%8e%b7%e5%8f%96%e6%80%bb%e4%bd%93%e8%a6%86%e7%9b%96%e7%8e%87%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42695\/","title":{"rendered":"\u5982\u4f55\u4ece go test -cover \u83b7\u53d6\u603b\u4f53\u8986\u76d6\u7387\u503c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u4ece go test -cover \u83b7\u53d6\u603b\u4f53\u8986\u76d6\u7387\u503c\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u4ece go test -cover \u83b7\u53d6\u603b\u4f53\u8986\u76d6\u7387\u503c\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 13:27:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u4ece go test -cover \u83b7\u53d6\u603b\u4f53\u8986\u76d6\u7387\u503c\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4f7f\u7528<code>go test<\/code>\u7684<code>-cover<\/code>\u9009\u9879\uff0c\u4f8b\u5982<\/p>\n<pre>go test .\/... -covermode=atomic -coverprofile coverage.out<\/pre>\n<p><code>coverage.out<\/code> \u5305\u542b\u6bcf\u4e2a\u6587\u4ef6\u7684\u5927\u91cf\u8f93\u51fa\uff0c\u4f46\u6211\u60f3\u8981\u4e00\u4e2a\u6574\u4f53\u8986\u76d6\u7387\u7684\u6570\u5b57\uff08\u53ef\u7528\u4e8e\u901a\u8fc7\/\u5931\u8d25\u8986\u76d6\u7387\uff09\u3002<\/p>\n<p>\u6211\u5728\u672c\u6587\u4e2d\u627e\u5230\u4e86\u4e00\u4e2a\u5408\u7406\u7684\uff08\u5c3d\u7ba1\u662f\u9519\u8bef\u7684\uff01\uff09\u811a\u672c\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<pre>cat coverage.out | \\\nawk 'BEGIN {cov=0; stat=0;} \\\n    $3!=\"\" { cov+=($3==1?$2:0); stat+=$2; } \\\n    END {printf(\"Total coverage: %.2f%% of statements\\n\", (cov\/stat)*100);}'<\/pre>\n<p>\u4f46\u662f\u6709\u6ca1\u6709\u4e00\u79cd\u975e\u811a\u672c\u7684 <code>go<\/code> \u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528 <code>gotestsum<\/code>\u3002\u5b83\u662f <code>go test<\/code> \u7684\u66ff\u4ee3\u54c1\uff0c\u5177\u6709\u9644\u52a0\u529f\u80fd\uff0c\u4f8b\u5982\u4ee5 JUnit XML \u683c\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p>\u4f7f\u7528\u8fd9\u6837\u7684\u7b2c\u4e09\u65b9\u5de5\u5177\u7684\u7f3a\u70b9\u662f\uff0c\u60a8\u7684\u5f00\u53d1\u4eba\u5458\u548c CI \u9700\u8981\u5b89\u88c5\u5b83\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5982\u4f55\u4ece go test -cover \u83b7\u53d6\u603b\u4f53\u8986\u76d6\u7387\u503c\uff1f \u5982\u4f55\u4ece go test -cover \u83b7\u53d6\u603b\u4f53\u8986\u76d6\u7387\u503c\uff1f \u6765\u6e90\uff1astackoverflow 2024-04-25 13:27:37 0\u6d4f\u89c8 \u6536\u85cf \u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u4ece go test -cover \u83b7\u53d6\u603b\u4f53\u8986\u76d6\u7387\u503c\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01 \u95ee\u9898\u5185\u5bb9 \u4f7f\u7528go test\u7684-cover\u9009\u9879\uff0c\u4f8b\u5982 go test .\/&#8230; -covermode=atomic -coverprofile coverage.out coverage.out \u5305\u542b\u6bcf\u4e2a\u6587\u4ef6\u7684\u5927\u91cf\u8f93\u51fa\uff0c\u4f46\u6211\u60f3\u8981\u4e00\u4e2a\u6574\u4f53\u8986\u76d6\u7387\u7684\u6570\u5b57\uff08\u53ef\u7528\u4e8e\u901a\u8fc7\/\u5931\u8d25\u8986\u76d6\u7387\uff09\u3002 \u6211\u5728\u672c\u6587\u4e2d\u627e\u5230\u4e86\u4e00\u4e2a\u5408\u7406\u7684\uff08\u5c3d\u7ba1\u662f\u9519\u8bef\u7684\uff01\uff09\u811a\u672c\u89e3\u51b3\u65b9\u6848\uff1a cat coverage.out | \\ awk &#8216;BEGIN {cov=0; stat=0;} \\ $3!=&#8221;&#8221; { cov+=($3==1?$2:0); stat+=$2; } \\ END {printf(&#8220;Total coverage: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42695","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42695"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42695\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}