{"id":6685,"date":"2021-09-12T02:58:00","date_gmt":"2021-09-11T18:58:00","guid":{"rendered":"https:\/\/vip.fxxkpython.com\/?p=6685"},"modified":"2021-09-29T20:14:25","modified_gmt":"2021-09-29T12:14:25","slug":"%e4%bd%bf%e7%94%a8-python-%e4%bb%8e-0-%e5%bc%80%e5%8f%91%e5%b1%9e%e4%ba%8e%e4%bd%a0%e7%9a%84%e4%b8%aa%e4%ba%ba%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f-25-%e5%a6%82%e4%bd%95%e5%9c%a8%e4%bd%a0%e7%9a%84","status":"publish","type":"post","link":"https:\/\/vip.fxxkpython.com\/?p=6685","title":{"rendered":"\u4f7f\u7528 Python \u4ece 0 \u5f00\u53d1\u5c5e\u4e8e\u4f60\u7684\u4e2a\u4eba\u7ba1\u7406\u7cfb\u7edf 25 | \u5982\u4f55\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u6d4b\u8bd5\u7528\u4f8b"},"content":{"rendered":"\n<p>\u5f53\u4f60\u5728\u9879\u76ee\u5199\u4e86\u4e00\u4e2a\u53c8\u4e00\u4e2a\u7684\u529f\u80fd\u6216\u51fd\u6570\u540e\uff0c\u4f60\u8fd8\u9700\u8981\u505a\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\uff0c\u90a3\u5c31\u662f<strong>\u6d4b\u8bd5<\/strong>\uff0c\u5728 Python \u4e2d\uff0c\u6709\u4e00\u4e2a\u6d4b\u8bd5\u6846\u67b6\u53eb unittest \uff0c\u5b83\u5c31\u662f\u4e13\u95e8\u7528\u6765\u5199\u6d4b\u8bd5\u7684\u3002<\/p>\n\n\n\n<h2>unittest<\/h2>\n\n\n\n<p>\u5b83\u4e3b\u8981\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u529f\u80fd\uff1a<\/p>\n\n\n\n<h3>test case<\/h3>\n\n\n\n<p>\u6700\u5c0f\u7684\u5355\u5143\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5\u628a\u5177\u4f53\u7684\u6bcf\u4e2a\u529f\u80fd\u5199\u6210\u5355\u5143\u6d4b\u8bd5\u7528\u4f8b\uff0c\u8fd9\u6837\u7684\u6bcf\u4e2a\u5355\u5143\u6d4b\u8bd5\u7528\u4f8b\u53ef\u4ee5\u68c0\u67e5\u7279\u5b9a\u7684\u529f\u80fd\u54cd\u5e94\u3002<\/p>\n\n\n\n<h3>test fixture<\/h3>\n\n\n\n<p>\u6709\u4e9b\u6d4b\u8bd5\u9700\u8981\u73af\u5883\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u8fde\u63a5\u7684\u64cd\u4f5c\uff0c\u5728\u6d4b\u8bd5\u5b8c\u6210\u540e\u4e5f\u9700\u8981\u6e05\u7406\u64cd\u4f5c\uff0ctest fixture \u5c31\u63d0\u4f9b\u4e86\u4f60\u5728\u6d4b\u8bd5\u524d\u540e\u7684\u51c6\u5907\u64cd\u4f5c\uff0c\u5176\u4e2d\u4e3b\u8981\u7528\u5230 setUP \u548c tearDown \u65b9\u6cd5\uff0c\u5206\u522b\u5728\u6d4b\u8bd5\u88ab\u6267\u884c\u524d\u540e\u88ab\u81ea\u52a8\u8c03\u7528\u3002<\/p>\n\n\n\n<h3>test suite<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u6709\u5f88\u591a\u6d4b\u8bd5\u90fd\u9700\u8981\u7528\u5230\u540c\u6837\u7684\u73af\u5883\uff0c\u6bd4\u5982\u6bcf\u4e2a test fixture \u53ea\u5bf9\u5e94\u6bcf\u4e2a test case\uff0c\u4f60\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u6d4b\u8bd5\u7528\u4f8b\u6dfb\u52a0\u6210\u4e00\u4e2a\u6d4b\u8bd5\u96c6\u5408\uff0c\u7136\u540e\u5728\u8fd9\u91cc\u7edf\u4e00\u6267\u884c\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<h3>test runner<\/h3>\n\n\n\n<p>\u987e\u540d\u601d\u4e49\uff0c\u8fd9\u662f\u6d4b\u8bd5\u7684\u8fd0\u884c\u7ec4\u4ef6\uff0c\u901a\u8fc7\u5b83\u6765\u6267\u884c\u6d4b\u8bd5\u7528\u4f8b\uff0c\u5b83\u4f1a\u8fd4\u56de\u6d4b\u8bd5\u7ed3\u679c\u7ed9\u6211\u4eec\u770b\u3002<\/p>\n\n\n\n<h2>\u5982\u4f55\u4f7f\u7528 unittest<\/h2>\n\n\n\n<p>\u4e0a\u9762\u7684\u8fd9\u4e9b\u5982\u4f55\u8fd0\u7528\u5230\u6211\u4eec\u7684\u9879\u76ee\u4e2d\u5462\uff1f<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u5c31\u7ed9\u4f60\u8bb2\u4e00\u4e0b\u3002<\/p>\n\n\n\n<p>\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u521b\u5efa test \u5305\u6765\u7ba1\u7406\u4f60\u8981\u6d4b\u8bd5\u7684\u6a21\u5757\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>tests<\/p><p>\u251c\u2500\u2500 <strong>init<\/strong>.py<\/p><p>\u251c\u2500\u2500 test_todo.py<\/p><p>\u2514\u2500\u2500 test_user.py<\/p><\/blockquote>\n\n\n\n<p>\u6d4b\u8bd5\u6587\u4ef6\u9700\u8981\u7528 test \u5f00\u5934\u547d\u540d\uff0c\u6d4b\u8bd5\u65b9\u6cd5\u4e5f\u662f\uff0c\u8fd9\u6837 unittest \u624d\u4f1a<div style=\"text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:#FF6666;\">\u672c\u6587\u9690\u85cf\u5185\u5bb9 <a href=\"https:\/\/vip.fxxkpython.com\/wp-login.php?redirect_to=https%3A%2F%2Fvip.fxxkpython.com%2F%3Fp%3D6685\">\u767b\u9646<\/a> \u540e\u624d\u53ef\u4ee5\u6d4f\u89c8<\/div>\u8fd8\u53ef\u4ee5\u5b9a\u4e49 flask \u547d\u4ee4\u6765\u8fd0\u884c\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49 test \u65b9\u6cd5\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">@app.cli.command()\ndef test():\n    tests = unittest.TestLoader().discover(&quot;tests&quot;)\n    unittest.TextTestRunner(verbosity=2).run(tests)\n    \n<\/pre><\/div>\n\n\n\n<p>\u8fd9\u6837\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528 flask test \u5c31\u53ef\u4ee5\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u6267\u884c\u663e\u793a OK \u8bf4\u660e\u6d4b\u8bd5\u901a\u8fc7\u3002<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">test_signup_user (tests.test_user.UserTestCase) ... ok\n\n----------------------------------------------------------------------\nRan 1 test in 0.024s\n\nOK\n<\/pre><\/div>\n\n\n\n<h2>\u6d4b\u8bd5\u5408\u96c6<\/h2>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5c06\u6d4b\u8bd5\u7528\u4f8b\u96c6\u5408\u5230\u4e00\u8d77\u6d4b\u8bd5\uff0c\u4f7f\u7528 test_suit\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def suite():\n    suite = unittest.TestSuite()\n    suite.addTest(WidgetTestCase('test_default_widget_size'))\n    suite.addTest(WidgetTestCase('test_widget_resize'))\n    return suite\n\nif __name__ == '__main__':\n    runner = unittest.TextTestRunner()\n    runner.run(suite())\n<\/pre><\/div>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c06 TestCase \u6dfb\u52a0\u5230 suite \u4e2d\uff0c\u7136\u540e\u4f7f\u7528 runner \u6765\u6d4b\u8bd5\u5b83\u4eec\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f60\u5728\u9879\u76ee\u5199\u4e86\u4e00\u4e2a\u53c8\u4e00\u4e2a\u7684\u529f\u80fd\u6216\u51fd\u6570\u540e\uff0c\u4f60\u8fd8\u9700\u8981\u505a\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\uff0c\u90a3\u5c31\u662f\u6d4b\u8bd5\uff0c\u5728 Python \u4e2d\uff0c\u6709\u4e00\u4e2a\u6d4b\u8bd5\u6846\u67b6\u53eb unittest \uff0c\u5b83\u5c31\u662f\u4e13\u95e8\u7528\u6765\u5199\u6d4b\u8bd5\u7684\u3002 unittest \u5b83\u4e3b\u8981\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u529f\u80fd\uff1a test case \u6700\u5c0f\u7684\u5355\u5143\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[99],"tags":[240,625,241],"_links":{"self":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6685"}],"collection":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6685"}],"version-history":[{"count":3,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6685\/revisions"}],"predecessor-version":[{"id":6695,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6685\/revisions\/6695"}],"wp:attachment":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}