{"id":207250,"date":"2025-07-08T08:53:24","date_gmt":"2025-07-08T00:53:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207250\/"},"modified":"2025-07-08T08:53:24","modified_gmt":"2025-07-08T00:53:24","slug":"%e5%b0%9d%e8%af%95%e4%b8%8e-golang-testify-suite-%e5%b9%b6%e8%a1%8c%e8%bf%90%e8%a1%8c%e6%b5%8b%e8%af%95%e5%a4%b1%e8%b4%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207250\/","title":{"rendered":"\u5c1d\u8bd5\u4e0e golang testify\/suite \u5e76\u884c\u8fd0\u884c\u6d4b\u8bd5\u5931\u8d25"},"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>\u5c1d\u8bd5\u4e0e golang testify\/suite \u5e76\u884c\u8fd0\u884c\u6d4b\u8bd5\u5931\u8d25<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 09:03:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5c1d\u8bd5\u4e0e golang testify\/suite \u5e76\u884c\u8fd0\u884c\u6d4b\u8bd5\u5931\u8d25\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4f7f\u7528 <code>testify\/suite<\/code> \u5305\u8fdb\u884c\u4e86\u591a\u9879\u6d4b\u8bd5\uff0c\u5e76\u4e14\u6309\u5982\u4e0b\u65b9\u5f0f\u5e76\u884c\u6267\u884c\u5b83\u4eec<\/p>\n<pre>type integrationsuite struct {\n    suite.suite\n}\n\nfunc testintegrationsuite(t *testing.t) {\n    suite.run(t, &amp;integrationsuite{})\n}\n\nfunc (is *integrationsuite) testsomething() {\n    is.t().log(\"\\tintegration testing something\")\n    for i := range mytesttable {\n        i := i\n        is.t().run(\"testing \"+mytesttable[i].scenarioname, func(_ *testing.t) {\n            is.t().parallel()\n...\n\nfunc (is *integrationsuite) testsomethingelse() {\n    is.t().log(\"\\tintegration testing something else\")\n    for i := range myothertesttable {\n        i := i\n        is.t().run(\"testing \"+myothertesttable[i].scenarioname, func(_ *testing.t) {\n            is.t().parallel()\n...\n        })<\/pre>\n<p>\u4f46\u662f\u8fd9\u4f1a\u5f15\u8d77\u6050\u614c<\/p>\n<pre>panic: testing: t.Parallel called multiple times [recovered]\n        panic: testing: t.Parallel called multiple times<\/pre>\n<p>\u5982\u4f55\u5229\u7528\u7279\u5b9a\u5305\u7684\u5e76\u884c\u6027\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u5728 <code>testing.t<\/code> \u7684\u9519\u8bef\u5b9e\u4f8b\u4e0a\u8c03\u7528 <code>t.parallel()<\/code>\u3002<\/p>\n<p>\u60a8\u6b63\u5728\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u542f\u52a8\u591a\u4e2a<em>\u5b50\u6d4b\u8bd5<\/em>\uff1a<\/p>\n<pre>is.t().run(\"testing \"+myothertesttable[i].scenarioname, func(_ *testing.t) {...}<\/pre>\n<p>\u4f46\u662f\uff0c\u60a8\u4e0d\u662f\u5c06<em>\u5b50\u6d4b\u8bd5<\/em>\u6807\u8bb0\u4e3a\u5e76\u884c\uff0c\u800c\u662f\u5c06<em>\u5916\u90e8\u7236\u6d4b\u8bd5<\/em>\u6807\u8bb0\u4e3a\u60a8\u542f\u52a8\u7684\u6bcf\u4e2a\u5b50\u6d4b\u8bd5\u7684\u5e76\u884c\uff1a<\/p>\n<pre>is.t().parallel() \/\/ marks outer test as parallel<\/pre>\n<p>\u76f8\u53cd\uff0c\u5c06\u5b50\u6d4b\u8bd5\u51fd\u6570\u7684 <code>testing.t<\/code> \u53c2\u6570\u7ed1\u5b9a\u5230\u53d8\u91cf\u5e76\u5728\u5b50\u6d4b\u8bd5\u4e0a\u8c03\u7528 <code>parallel<\/code>\uff1a<\/p>\n<pre>is.T().Run(\"Testing \"+myOtherTestTable[i].scenarioName, func(t *testing.T) {\n            t.Parallel() \/\/ this is the subtest's T instance\n            \/\/ test code\n        })<\/pre>\n<p>\u8fd9\u5c06\u6240\u6709\u5b50\u6d4b\u8bd5\u6807\u8bb0\u4e3a\u7236\u6d4b\u8bd5\u4e2d\u7684\u5e76\u884c\u6d4b\u8bd5\u3002\u5982\u679c\u60a8\u8fd8\u60f3\u5c06\u7236\u6d4b\u8bd5\u6807\u8bb0\u4e3a\u4e0e\u5305\u4e2d\u7684\u5176\u4ed6\u6d4b\u8bd5\u5e76\u884c\uff0c\u8bf7\u5728 <code>testsomethingelse<\/code> \u5f00\u5934\u8c03\u7528 <code>is.t().parallel()<\/code> <em><strong>once<\/strong><\/em> (\u4e0d\u5728\u5b50\u6d4b\u8bd5\u5185\uff09<\/p>\n<p>\u6709\u5173\u5b50\u6d4b\u8bd5\u5e76\u884c\u6027\u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\uff1a<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0ctestify \u5957\u4ef6\u5f53\u524d\u4e0d\u662f\u201c\u7ebf\u7a0b\u5b89\u5168\u201d\u7684\uff0c\u56e0\u6b64\u4e0d\u652f\u6301\u5e76\u53d1\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5373\u4f7f\u6d4b\u8bd5\u5e94\u8be5\u5931\u8d25\uff0c\u4e5f\u4f1a\u6210\u529f\u901a\u8fc7\u3002<\/p>\n<p>\u8bf7\u5728\u6b64\u5904\u67e5\u627e\u5f53\u524d\u72b6\u6001\u4ee5\u53ca\u62a5\u544a\u7684\u95ee\u9898\uff1a<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5c1d\u8bd5\u4e0e golang testify\/suite \u5e76\u884c\u8fd0\u884c\u6d4b\u8bd5\u5931\u8d25\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207250","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207250"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207250\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}