{"id":76311,"date":"2026-02-22T07:25:29","date_gmt":"2026-02-21T23:25:29","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/76311.html"},"modified":"2026-02-22T07:25:29","modified_gmt":"2026-02-21T23:25:29","slug":"%e3%80%8apython-%e7%bc%96%e7%a8%8b%e5%85%a8%e6%99%af%e8%a7%a3%e6%9e%90%ef%bc%9a%e4%bb%8e%e6%a0%b8%e5%bf%83%e7%b2%be%e8%a6%81%e5%88%b0%e6%b5%8b%e8%af%95%e6%9b%bf%e8%ba%ab%ef%bc%88test-doubles%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/76311.html","title":{"rendered":"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790\uff1a\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab\uff08Test Doubles\uff09\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b"},"content":{"rendered":"<h2>\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790&#xff1a;\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab&#xff08;Test Doubles&#xff09;\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b<\/h2>\n<p>\u5927\u5bb6\u597d&#xff0c;\u6211\u662f\u8001\u9ec4\u3002\u5728\u591a\u5e74\u7684 IT \u6559\u5b66\u4e0e\u4e00\u7ebf\u5f00\u53d1\u5b9e\u6218\u4e2d&#xff0c;\u6211\u89c1\u8bc1\u4e86\u65e0\u6570\u6280\u672f\u7684\u5174\u8870&#xff0c;\u4f46 Python \u59cb\u7ec8\u662f\u6211\u6848\u5934\u6700\u950b\u5229\u7684\u745e\u58eb\u519b\u5200\u3002<\/p>\n<h3>2. \u5f00\u7bc7\u5f15\u5165&#xff1a;\u91cd\u5851\u7f16\u7a0b\u751f\u6001\u7684\u201c\u80f6\u6c34\u201d\u54f2\u5b66<\/h3>\n<p>\u56de\u671b Python \u7684\u53d1\u5c55\u5386\u7a0b&#xff0c;\u5b83\u4ece\u8bde\u751f\u4e4b\u521d\u51ed\u501f\u7b80\u6d01\u4f18\u96c5\u7684\u8bed\u6cd5\u4fd8\u83b7\u5f00\u53d1\u8005&#xff0c;\u5230\u5982\u4eca\u5728 Web \u5f00\u53d1\u3001\u6570\u636e\u79d1\u5b66\u3001\u4eba\u5de5\u667a\u80fd\u4e43\u81f3 3D \u6e38\u620f\u540e\u7aef&#xff08;\u5982\u914d\u5408 WebGL \u5f15\u64ce&#xff09;\u7b49\u9886\u57df\u904d\u5730\u5f00\u82b1&#xff0c;\u5b8c\u6210\u4e86\u4e00\u573a\u534e\u4e3d\u7684\u8715\u53d8\u3002Python \u771f\u6b63\u6539\u53d8\u4e86\u7f16\u7a0b\u751f\u6001&#xff0c;\u5b83\u4e0d\u4ec5\u662f\u6781\u5176\u4f18\u79c0\u7684\u201c\u80f6\u6c34\u8bed\u8a00\u201d&#xff0c;\u66f4\u662f\u81ea\u52a8\u5316\u8fd0\u7ef4\u3001\u540e\u7aef\u5fae\u670d\u52a1\u4e0e\u6d77\u91cf\u6570\u636e\u5904\u7406\u7684\u9996\u9009\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u4eca\u5929\u6211\u4eec\u8981\u6df1\u5ea6\u76d8\u70b9 Python \u5e76\u805a\u7126\u81ea\u52a8\u5316\u6d4b\u8bd5&#xff1f; \u5728\u65e5\u5e38\u7684\u6559\u5b66\u548c\u6821\u52a1\u7269\u6d41\u81ea\u52a8\u5316\u5f00\u53d1\u4e2d&#xff0c;\u6211\u53d1\u73b0\u5f88\u591a\u5f00\u53d1\u8005\u2014\u2014\u65e0\u8bba\u662f\u521a\u5165\u95e8\u7684\u65b0\u624b\u8fd8\u662f\u6709\u4e00\u5b9a\u7ecf\u9a8c\u7684\u719f\u624b\u2014\u2014\u5f80\u5f80\u80fd\u5feb\u901f\u5199\u51fa\u5b9e\u73b0\u529f\u80fd\u7684\u811a\u672c&#xff0c;\u4f46\u5728\u9762\u5bf9\u590d\u6742\u4e1a\u52a1\u903b\u8f91&#xff08;\u5982\u5bf9\u63a5\u7b2c\u4e09\u65b9 API\u3001\u5904\u7406\u5927\u89c4\u6a21\u5e76\u53d1&#xff09;\u65f6&#xff0c;\u4ee3\u7801\u7684\u5065\u58ee\u6027\u5374\u5927\u6253\u6298\u6263\u3002\u5229\u7528 Python \u6253\u9020\u9ad8\u8d28\u91cf\u4ea7\u54c1\u7684\u6838\u5fc3&#xff0c;\u4e0d\u4ec5\u4ec5\u5728\u4e8e\u638c\u63e1\u70ab\u9177\u7684\u8bed\u6cd5&#xff0c;\u66f4\u5728\u4e8e\u5982\u4f55\u7f16\u5199\u53ef\u6d4b\u8bd5\u3001\u6613\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u4eca\u5929&#xff0c;\u6211\u4eec\u5c06\u4ece Python \u7684\u5185\u5728\u9b45\u529b\u51fa\u53d1&#xff0c;\u4e00\u8def\u6df1\u5165\u5230\u9ad8\u7ea7\u7279\u6027&#xff0c;\u5e76\u91cd\u70b9\u5256\u6790\u9ad8\u7ea7\u81ea\u52a8\u5316\u6d4b\u8bd5\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684**\u201c\u6d4b\u8bd5\u66ff\u8eab&#xff08;Test Doubles&#xff09;\u7684\u4e94\u79cd\u6b66\u5668\u201d**&#xff0c;\u5e26\u4f60\u9886\u7565\u9ad8\u6548\u7f16\u7a0b\u7684\u6781\u81f4\u4f53\u9a8c\u3002<\/p>\n<hr \/>\n<h3>3. \u57fa\u7840\u90e8\u5206&#xff1a;Python \u8bed\u8a00\u7cbe\u8981<\/h3>\n<p>Python \u7684\u9b54\u529b\u5728\u4e8e\u5176\u51fa\u8272\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u4e0e\u52a8\u6001\u7c7b\u578b\u7684\u7075\u6d3b\u6027\u3002\u4efb\u4f55\u7a33\u56fa\u7684\u7cfb\u7edf&#xff0c;\u90fd\u5efa\u7acb\u5728\u624e\u5b9e\u7684\u57fa\u7840\u4e4b\u4e0a\u3002<\/p>\n<h4>\u6838\u5fc3\u8bed\u6cd5\u4e0e\u6570\u636e\u7c7b\u578b<\/h4>\n<p>Python \u63d0\u4f9b\u4e86\u5f00\u7bb1\u5373\u7528\u7684\u5f3a\u5927\u6570\u636e\u7ed3\u6784&#xff1a;<\/p>\n<ul>\n<li>\u5217\u8868&#xff08;List&#xff09;\u4e0e\u5143\u7ec4&#xff08;Tuple&#xff09;&#xff1a;\u5904\u7406\u6709\u5e8f\u6570\u636e&#xff0c;\u524d\u8005\u53ef\u53d8&#xff0c;\u540e\u8005\u4e0d\u53ef\u53d8\u4e14\u54c8\u5e0c\u5b89\u5168\u3002<\/li>\n<li>\u5b57\u5178&#xff08;Dictionary&#xff09;\u4e0e\u96c6\u5408&#xff08;Set&#xff09;&#xff1a;\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0&#xff0c;\u662f\u5feb\u901f\u68c0\u7d22\u548c\u53bb\u91cd\u7684\u795e\u5175\u5229\u5668\u3002<\/li>\n<\/ul>\n<p>\u914d\u5408\u7b80\u6d01\u7684 if-elif-else \u63a7\u5236\u6d41\u548c\u5f3a\u5927\u7684\u5f02\u5e38\u5904\u7406&#xff08;try-except-finally&#xff09;&#xff0c;\u6211\u4eec\u80fd\u7528\u6781\u5c11\u7684\u4ee3\u7801\u8868\u8fbe\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<h4>\u51fd\u6570\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b&#xff08;OOP&#xff09;<\/h4>\n<p>Python \u4e2d\u7684\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11&#xff0c;\u652f\u6301\u7075\u6d3b\u7684\u53c2\u6570\u4f20\u9012&#xff08;\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001*args\u3001**kwargs&#xff09;\u4ee5\u53ca\u533f\u540d\u51fd\u6570&#xff08;lambda&#xff09;\u3002\u5728\u67b6\u6784\u8bbe\u8ba1\u4e0a&#xff0c;Python \u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u652f\u6301\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001&#xff0c;\u8ba9\u4f60\u80fd\u9ad8\u5ea6\u62bd\u8c61\u4e1a\u52a1\u5b9e\u4f53\u3002<\/p>\n<p>\u7ecf\u5178\u7279\u6027&#xff1a;\u88c5\u9970\u5668&#xff08;Decorator&#xff09; \u88c5\u9970\u5668\u662f Python \u4f18\u96c5\u8bed\u6cd5\u7684\u4ee3\u8868\u4f5c&#xff0c;\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u524d\u63d0\u4e0b&#xff0c;\u52a8\u6001\u4e3a\u5176\u6ce8\u5165\u65b0\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u6211\u5728\u4f18\u5316\u9879\u76ee\u6027\u80fd\u65f6\u5e38\u7528\u7684\u4e00\u4e2a\u57fa\u7840\u5b9e\u8df5&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u793a\u4f8b&#xff1a;\u5229\u7528\u88c5\u9970\u5668\u8bb0\u5f55\u51fd\u6570\u8c03\u7528\u65f6\u95f4<\/span><br \/>\n<span class=\"token keyword\">import<\/span> time<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">timer<\/span><span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">wrapper<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kwargs<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        start <span class=\"token operator\">&#061;<\/span> time<span class=\"token punctuation\">.<\/span>time<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        result <span class=\"token operator\">&#061;<\/span> func<span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kwargs<span class=\"token punctuation\">)<\/span><br \/>\n        end <span class=\"token operator\">&#061;<\/span> time<span class=\"token punctuation\">.<\/span>time<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>func<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> \u82b1\u8d39\u65f6\u95f4&#xff1a;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>end <span class=\"token operator\">&#8211;<\/span> start<span class=\"token punctuation\">:<\/span><span class=\"token format-spec\">.4f<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\u79d2&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> result<br \/>\n    <span class=\"token keyword\">return<\/span> wrapper<\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;timer<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">compute_sum<\/span><span class=\"token punctuation\">(<\/span>n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token builtin\">sum<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span>n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>compute_sum<span class=\"token punctuation\">(<\/span><span class=\"token number\">1000000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<hr \/>\n<h3>4. \u9ad8\u7ea7\u6280\u672f\u4e0e\u5b9e\u6218\u8fdb\u9636<\/h3>\n<p>\u5f53\u6211\u4eec\u8d8a\u8fc7\u57fa\u7840\u7684\u95e8\u69db&#xff0c;Python \u7684\u9ad8\u7ea7\u7279\u6027\u5c06\u4e3a\u6211\u4eec\u6253\u5f00\u5168\u65b0\u7684\u89c6\u754c\u3002<\/p>\n<h4>\u5143\u7f16\u7a0b\u4e0e\u52a8\u6001\u751f\u6210<\/h4>\n<p>Python \u5141\u8bb8\u6211\u4eec\u5728\u8fd0\u884c\u65f6\u4fee\u6539\u4ee3\u7801\u81ea\u8eab\u7684\u7ed3\u6784\u3002\u901a\u8fc7 type() \u52a8\u6001\u521b\u5efa\u7c7b&#xff0c;\u6216\u8005\u5229\u7528 __new__ \u548c\u5143\u7c7b&#xff08;metaclass&#xff09;\u62e6\u622a\u7c7b\u7684\u521b\u5efa\u8fc7\u7a0b&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5f3a\u5927\u7684 ORM \u6846\u67b6\u6216\u81ea\u52a8\u5316\u7684 API \u6ce8\u518c\u673a\u5236\u3002<\/p>\n<h4>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e0e\u751f\u6210\u5668<\/h4>\n<p>\u9762\u5bf9\u6587\u4ef6 I\/O\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u8d44\u6e90\u654f\u611f\u64cd\u4f5c&#xff0c;with \u8bed\u53e5&#xff08;\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff09;\u662f\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u800c**\u751f\u6210\u5668&#xff08;Generator&#xff09;**\u901a\u8fc7 yield \u5173\u952e\u5b57\u5b9e\u73b0\u4e86\u6570\u636e\u7684\u60f0\u6027\u6c42\u503c&#xff08;Lazy Evaluation&#xff09;&#xff0c;\u5728\u5904\u7406\u8d85\u5927\u6587\u4ef6\u6216\u5b9e\u65f6\u6570\u636e\u6d41\u65f6&#xff0c;\u80fd\u5c06\u5185\u5b58\u5360\u7528\u964d\u81f3\u6700\u4f4e\u3002<\/p>\n<h4>\u5f02\u6b65\u7f16\u7a0b\u4e0e\u9ad8\u6027\u80fd\u8ba1\u7b97<\/h4>\n<p>\u5728 I\/O \u5bc6\u96c6\u578b\u573a\u666f&#xff08;\u5982\u7f51\u7edc\u722c\u866b\u3001\u5e76\u53d1 API \u8bf7\u6c42&#xff09;\u4e0b&#xff0c;\u4f20\u7edf\u7684\u540c\u6b65\u4ee3\u7801\u4f1a\u9020\u6210\u6781\u5927\u7684\u8d44\u6e90\u6d6a\u8d39\u3002Python \u7684 asyncio \u5e93\u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af\u4e0e async\/await \u8bed\u6cd5&#xff0c;\u5b9e\u73b0\u4e86\u6781\u81f4\u7684\u5e76\u53d1\u6027\u80fd\u3002\u7ed3\u5408\u751f\u6001\u4e2d\u7684 NumPy&#xff08;\u6570\u503c\u8ba1\u7b97&#xff09;\u3001Pandas&#xff08;\u6570\u636e\u5206\u6790&#xff09;\u4ee5\u53ca TensorFlow\/PyTorch&#xff08;\u6df1\u5ea6\u5b66\u4e60&#xff09;&#xff0c;Python \u771f\u6b63\u505a\u5230\u4e86\u5728\u5404\u9886\u57df\u7684\u901a\u5403\u3002<\/p>\n<hr \/>\n<h3>5. \u6848\u4f8b\u5b9e\u6218&#xff1a;\u6d4b\u8bd5\u66ff\u8eab&#xff08;Test Doubles&#xff09;\u7684\u4e94\u5927\u6b66\u5668<\/h3>\n<p>\u5728\u590d\u6742\u7684\u5de5\u7a0b\u5b9e\u8df5\u4e2d&#xff0c;\u6211\u4eec\u7684\u4ee3\u7801\u5f80\u5f80\u91cd\u5ea6\u4f9d\u8d56\u5916\u90e8\u7cfb\u7edf&#xff08;\u6570\u636e\u5e93\u3001\u7b2c\u4e09\u65b9\u652f\u4ed8 API\u3001\u90ae\u4ef6\u670d\u52a1\u7b49&#xff09;\u3002\u5982\u679c\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u771f\u5b9e\u8c03\u7528\u8fd9\u4e9b\u670d\u52a1&#xff0c;\u4e0d\u4ec5\u901f\u5ea6\u6781\u6162&#xff0c;\u800c\u4e14\u6781\u6613\u56e0\u4e3a\u7f51\u7edc\u6ce2\u52a8\u5bfc\u81f4\u6d4b\u8bd5\u201c\u859b\u5b9a\u8c14\u7684\u901a\u8fc7\u201d\u3002<\/p>\n<p>\u8fd9\u65f6&#xff0c;\u6211\u4eec\u9700\u8981\u5f15\u5165\u6d4b\u8bd5\u66ff\u8eab&#xff08;Test Doubles&#xff09;\u3002\u8457\u540d\u8f6f\u4ef6\u5de5\u7a0b\u4e13\u5bb6 Gerard Meszaros \u5c06\u5176\u7cbe\u51c6\u5206\u4e3a\u4e94\u7c7b&#xff1a;Dummy\u3001Stub\u3001Spy\u3001Mock\u3001Fake\u3002\u5b83\u4eec\u5206\u522b\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u8ba2\u5355\u5904\u7406\u7cfb\u7edf&#xff0c;\u6838\u5fc3\u51fd\u6570\u662f process_order(order, payment_gateway, logger)\u3002\u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u4e94\u628a\u6b66\u5668\u5982\u4f55\u5927\u663e\u8eab\u624b\u3002<\/p>\n<h4>1. Dummy&#xff08;\u5360\u4f4d\u5bf9\u8c61&#xff09;<\/h4>\n<p>\u5b9a\u4e49&#xff1a;Dummy \u5bf9\u8c61\u88ab\u4f20\u9012\u7ed9\u65b9\u6cd5&#xff0c;\u4f46\u7edd\u5bf9\u4e0d\u4f1a\u88ab\u771f\u6b63\u4f7f\u7528\u3002\u901a\u5e38\u53ea\u4e3a\u4e86\u586b\u5145\u53c2\u6570\u5217\u8868&#xff0c;\u6ee1\u8db3\u7c7b\u578b\u6216\u7b7e\u540d\u8981\u6c42\u3002 \u9002\u7528\u573a\u666f&#xff1a;\u6d4b\u8bd5\u903b\u8f91\u7684\u67d0\u6761\u5206\u652f\u6839\u672c\u4e0d\u9700\u8981\u5173\u5fc3\u8be5\u53c2\u6570\u65f6\u3002<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_order_creation_without_payment<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u6211\u4eec\u53ea\u6d4b\u8bd5\u8ba2\u5355\u521b\u5efa\u903b\u8f91&#xff0c;\u4e0d\u6d89\u53ca\u652f\u4ed8<\/span><br \/>\n    dummy_payment_gateway <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">None<\/span> <span class=\"token comment\"># \u6216\u8005\u4e00\u4e2a\u7a7a\u7684\u5360\u4f4d\u7c7b<\/span><br \/>\n    dummy_logger <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;I am a dummy string, not a real logger&#034;<\/span><\/p>\n<p>    order <span class=\"token operator\">&#061;<\/span> Order<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">id<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> total<span class=\"token operator\">&#061;<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u7cfb\u7edf\u5728\u9a8c\u8bc1\u9636\u6bb5\u5c31\u62e6\u622a\u4e86&#xff0c;\u6839\u672c\u7528\u4e0d\u5230\u540e\u9762\u7684\u53c2\u6570<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> order<span class=\"token punctuation\">.<\/span>is_valid<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token boolean\">True<\/span> <\/p>\n<h4>2. Stub&#xff08;\u6253\u6869\u5bf9\u8c61&#xff09;<\/h4>\n<p>\u5b9a\u4e49&#xff1a;Stub \u5bf9\u6d4b\u8bd5\u4e2d\u7684\u65b9\u6cd5\u8c03\u7528\u63d0\u4f9b\u9884\u8bbe\u7684\u786c\u7f16\u7801\u54cd\u5e94&#xff0c;\u901a\u5e38\u4e0d\u4f1a\u54cd\u5e94\u6d4b\u8bd5\u8981\u6c42\u4e4b\u5916\u7684\u4efb\u4f55\u4e8b\u60c5\u3002 \u9002\u7528\u573a\u666f&#xff1a;\u4f60\u9700\u8981\u5f3a\u5236\u7cfb\u7edf\u8fdb\u5165\u67d0\u79cd\u7279\u5b9a\u72b6\u6001&#xff08;\u5982\u6a21\u62df\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u3001\u6a21\u62df\u6570\u636e\u5e93\u8fd4\u56de\u7279\u5b9a\u6570\u636e&#xff09;\u3002<\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">StubPaymentGateway<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">charge<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> amount<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u65e0\u89c6\u8f93\u5165&#xff0c;\u6c38\u8fdc\u8fd4\u56de\u6210\u529f\u72b6\u6001<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;status&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;success&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;transaction_id&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;tx_123&#034;<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_process_order_success<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    stub_gateway <span class=\"token operator\">&#061;<\/span> StubPaymentGateway<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    order <span class=\"token operator\">&#061;<\/span> Order<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">id<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> total<span class=\"token operator\">&#061;<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    result <span class=\"token operator\">&#061;<\/span> process_order<span class=\"token punctuation\">(<\/span>order<span class=\"token punctuation\">,<\/span> gateway<span class=\"token operator\">&#061;<\/span>stub_gateway<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> result <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;Order Completed&#034;<\/span><\/p>\n<h4>3. Spy&#xff08;\u95f4\u8c0d\u5bf9\u8c61&#xff09;<\/h4>\n<p>\u5b9a\u4e49&#xff1a;Spy \u662f\u5728 Stub \u7684\u57fa\u7840\u4e0a&#xff0c;\u5077\u5077\u8bb0\u5f55\u4e0b\u81ea\u5df1\u88ab\u8c03\u7528\u7684\u5386\u53f2&#xff08;\u8c03\u7528\u6b21\u6570\u3001\u4f20\u5165\u7684\u53c2\u6570\u7b49&#xff09;&#xff0c;\u4ee5\u4fbf\u6d4b\u8bd5\u540e\u8fdb\u884c\u65ad\u8a00\u3002 \u9002\u7528\u573a\u666f&#xff1a;\u5f53\u4f60\u4e0d\u4ec5\u9700\u8981\u63d0\u4f9b\u5047\u6570\u636e&#xff0c;\u8fd8\u9700\u8981\u9a8c\u8bc1\u4ee3\u7801\u662f\u5426\u201c\u505a\u51fa\u4e86\u6b63\u786e\u7684\u52a8\u4f5c\u201d&#xff08;\u4f8b\u5982&#xff1a;\u662f\u5426\u7ed9\u7528\u6237\u53d1\u9001\u4e86\u90ae\u4ef6&#xff1f;\u53d1\u9001\u4e86\u51e0\u6b21&#xff1f;&#xff09;\u3002<\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">EmailServiceSpy<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>emails_sent <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>last_recipient <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">None<\/span><\/p>\n<p>    <span class=\"token keyword\">def<\/span> <span class=\"token function\">send<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> recipient<span class=\"token punctuation\">,<\/span> message<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>emails_sent <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>last_recipient <span class=\"token operator\">&#061;<\/span> recipient<br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">True<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_order_sends_confirmation<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    spy_mailer <span class=\"token operator\">&#061;<\/span> EmailServiceSpy<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    complete_order<span class=\"token punctuation\">(<\/span>order_id<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> mailer<span class=\"token operator\">&#061;<\/span>spy_mailer<span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">assert<\/span> spy_mailer<span class=\"token punctuation\">.<\/span>emails_sent <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> spy_mailer<span class=\"token punctuation\">.<\/span>last_recipient <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;customer&#064;example.com&#034;<\/span><\/p>\n<h4>4. Mock&#xff08;\u6a21\u62df\u5bf9\u8c61&#xff09;<\/h4>\n<p>\u5b9a\u4e49&#xff1a;Mock \u662f\u66f4\u9ad8\u7ea7\u7684 Spy\u3002\u5728\u4f7f\u7528\u524d&#xff0c;\u4f60\u9700\u8981\u5bf9\u5b83\u8bbe\u5b9a\u884c\u4e3a\u671f\u671b&#xff08;Expectations&#xff09;\u3002\u5b83\u80fd\u591f\u81ea\u52a8\u9a8c\u8bc1\u81ea\u8eab\u662f\u5426\u88ab\u4ee5\u6b63\u786e\u7684\u65b9\u5f0f\u8c03\u7528\u3002Python \u5185\u7f6e\u7684 unittest.mock \u5e93\u662f\u6700\u4f73\u5229\u5668\u3002 \u9002\u7528\u573a\u666f&#xff1a;\u91cd\u5ea6\u4f9d\u8d56\u884c\u4e3a\u9a8c\u8bc1&#xff0c;\u786e\u4fdd\u7279\u5b9a\u6a21\u5757\u4e4b\u95f4\u7684\u4ea4\u4e92\u534f\u8bae\u5b8c\u5168\u6b63\u786e\u3002<\/p>\n<p><span class=\"token keyword\">from<\/span> unittest<span class=\"token punctuation\">.<\/span>mock <span class=\"token keyword\">import<\/span> Mock<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_payment_gateway_called_correctly<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u521b\u5efa\u4e00\u4e2a Mock \u5bf9\u8c61<\/span><br \/>\n    mock_gateway <span class=\"token operator\">&#061;<\/span> Mock<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    order <span class=\"token operator\">&#061;<\/span> Order<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">id<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> total<span class=\"token operator\">&#061;<\/span><span class=\"token number\">250<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    process_order<span class=\"token punctuation\">(<\/span>order<span class=\"token punctuation\">,<\/span> gateway<span class=\"token operator\">&#061;<\/span>mock_gateway<span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># \u65ad\u8a00&#xff1a;\u7f51\u5173\u7684 charge \u65b9\u6cd5\u5fc5\u987b\u88ab\u8c03\u7528\u8fc7\u4e00\u6b21&#xff0c;\u4e14\u91d1\u989d\u5fc5\u987b\u662f 250<\/span><br \/>\n    mock_gateway<span class=\"token punctuation\">.<\/span>charge<span class=\"token punctuation\">.<\/span>assert_called_once_with<span class=\"token punctuation\">(<\/span><span class=\"token number\">250<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h4>5. Fake&#xff08;\u4f2a\u9020\u5bf9\u8c61&#xff09;<\/h4>\n<p>\u5b9a\u4e49&#xff1a;Fake \u5bf9\u8c61\u5177\u6709\u771f\u6b63\u6709\u6548\u7684\u5de5\u4f5c\u5b9e\u73b0&#xff0c;\u4f46\u901a\u5e38\u91c7\u53d6\u4e86\u4e00\u4e9b\u6377\u5f84&#xff0c;\u4f7f\u5176\u4e0d\u9002\u5408\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528\u3002\u6700\u7ecf\u5178\u7684\u4f8b\u5b50\u5c31\u662f\u5185\u5b58\u6570\u636e\u5e93\u3002 \u9002\u7528\u573a\u666f&#xff1a;\u9700\u8981\u771f\u5b9e\u7684\u4e1a\u52a1\u903b\u8f91\u4ea4\u4e92&#xff0c;\u4f46\u8981\u907f\u514d\u9ad8\u6602\u7684\u57fa\u7840\u8bbe\u65bd\u5f00\u9500\u3002<\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">FakeInMemoryDatabase<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>_data <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">def<\/span> <span class=\"token function\">save<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> record<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>_data<span class=\"token punctuation\">[<\/span>record<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">id<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> record<\/p>\n<p>    <span class=\"token keyword\">def<\/span> <span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> record_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> self<span class=\"token punctuation\">.<\/span>_data<span class=\"token punctuation\">.<\/span>get<span class=\"token punctuation\">(<\/span>record_id<span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_database_persistence<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    fake_db <span class=\"token operator\">&#061;<\/span> FakeInMemoryDatabase<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    user <span class=\"token operator\">&#061;<\/span> User<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">id<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">99<\/span><span class=\"token punctuation\">,<\/span> name<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;\u94ed\u6e0a&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    fake_db<span class=\"token punctuation\">.<\/span>save<span class=\"token punctuation\">(<\/span>user<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> fake_db<span class=\"token punctuation\">.<\/span>get<span class=\"token punctuation\">(<\/span><span class=\"token number\">99<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>name <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;\u94ed\u6e0a&#034;<\/span><\/p>\n<h4>\u6700\u4f73\u5b9e\u8df5\u4e0e\u5efa\u8bae<\/h4>\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d&#xff0c;\u4e0d\u8981\u6ee5\u7528 Mock\u3002\u8fc7\u5ea6\u4f7f\u7528 Mock \u4f1a\u5bfc\u81f4\u201c\u8106\u5f31\u7684\u6d4b\u8bd5\u201d&#xff08;\u4ee3\u7801\u7a0d\u5fae\u91cd\u6784&#xff0c;\u6d4b\u8bd5\u5168\u7ebf\u5d29\u6e83&#xff09;\u3002\u6838\u5fc3\u539f\u5219\u662f&#xff1a;\u5c3d\u91cf\u591a\u7528 Fake \u548c Stub \u6d4b\u8bd5\u72b6\u6001&#xff0c;\u5c11\u7528 Mock \u6d4b\u8bd5\u4ea4\u4e92\u884c\u4e3a\u3002 \u7ed3\u5408 PEP8 \u89c4\u8303\u4e0e\u6301\u7eed\u96c6\u6210&#xff08;CI\/CD&#xff09;&#xff0c;\u8fd9\u4e9b\u6d4b\u8bd5\u66ff\u8eab\u5c06\u4e3a\u4f60\u6784\u7b51\u6700\u575a\u56fa\u7684\u4ee3\u7801\u62a4\u57ce\u6cb3\u3002<\/p>\n<hr \/>\n<h3>6. \u524d\u6cbf\u89c6\u89d2\u4e0e\u672a\u6765\u5c55\u671b<\/h3>\n<p>\u7ad9\u5728\u6280\u672f\u53d1\u5c55\u7684\u6f6e\u5934&#xff0c;Python \u7684\u751f\u6001\u6b63\u8fce\u6765\u65b0\u7684\u7206\u53d1\u671f\u3002<\/p>\n<ul>\n<li>Web \u6846\u67b6\u7684\u6d17\u724c&#xff1a;\u57fa\u4e8e ASGI \u7684 FastAPI \u51ed\u501f\u6781\u9ad8\u7684\u6027\u80fd\u548c\u81ea\u52a8\u5316\u7684 OpenAPI \u6587\u6863\u751f\u6210&#xff0c;\u6b63\u5728\u8fc5\u901f\u53d6\u4ee3\u90e8\u5206\u4f20\u7edf\u6846\u67b6\u7684\u751f\u6001\u4f4d\u3002<\/li>\n<li>AI \u4e0e\u751f\u4ea7\u529b\u89e3\u653e&#xff1a;\u968f\u7740\u5927\u6a21\u578b\u7684\u666e\u53ca&#xff0c;Python \u5728\u4eba\u5de5\u667a\u80fd\u3001\u81ea\u52a8\u5316\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u4e0d\u53ef\u66ff\u4ee3\u3002\u501f\u52a9 Streamlit \u7b49\u6846\u67b6&#xff0c;\u5f00\u53d1\u8005\u751a\u81f3\u4e0d\u9700\u8981\u7f16\u5199\u524d\u7aef\u4ee3\u7801&#xff0c;\u5c31\u80fd\u5728\u51e0\u5341\u5206\u949f\u5185\u5c06\u4e00\u4e2a AI \u811a\u672c\u8f6c\u5316\u4e3a\u4ea4\u4e92\u5f0f\u7684 Web \u5e94\u7528\u3002<\/li>\n<li>\u8fb9\u7f18\u8ba1\u7b97\u4e0e\u7269\u8054\u7f51&#xff1a;MicroPython \u6b63\u5728\u8ba9 Python \u6e17\u900f\u8fdb\u667a\u80fd\u786c\u4ef6\u7684\u6bcf\u4e00\u4e2a\u89d2\u843d\u3002<\/li>\n<\/ul>\n<p>\u672a\u6765\u7684 Python&#xff0c;\u5fc5\u5c06\u5728\u4fdd\u8bc1\u5f00\u53d1\u6548\u7387\u7684\u540c\u65f6&#xff0c;\u501f\u7531\u5e95\u5c42\u7684 C\/Rust \u6269\u5c55&#xff08;\u5982 Pydantic V2&#xff09;\u8fdb\u4e00\u6b65\u7a81\u7834\u6027\u80fd\u7684\u5929\u82b1\u677f\u3002<\/p>\n<hr \/>\n<h3>7. \u603b\u7ed3\u4e0e\u4e92\u52a8<\/h3>\n<p>\u5728\u8fd9\u7bc7\u535a\u6587\u4e2d&#xff0c;\u6211\u4eec\u4ece Python \u7b80\u6d01\u5f3a\u5927\u7684\u6838\u5fc3\u8bed\u6cd5\u51fa\u53d1&#xff0c;\u8de8\u8d8a\u4e86\u5f02\u6b65\u4e0e\u5143\u7f16\u7a0b\u7684\u8fdb\u9636\u5730\u5e26&#xff0c;\u5e76\u91cd\u70b9\u62d4\u9664\u4e86\u4f01\u4e1a\u7ea7\u5f00\u53d1\u4e2d\u7684\u201c\u6bd2\u7624\u201d\u2014\u2014\u4e0d\u53ef\u6d4b\u8bd5\u7684\u4ee3\u7801&#xff0c;\u8be6\u7ec6\u5256\u6790\u4e86 Test Doubles \u7684\u4e94\u5927\u6b66\u5668\u3002<\/p>\n<p>\u7f16\u7a0b\u662f\u4e00\u573a\u65e0\u6b62\u5883\u7684\u4fee\u884c&#xff0c;\u8bed\u8a00\u53ea\u662f\u5de5\u5177&#xff0c;\u771f\u6b63\u7684\u6b66\u529f\u5728\u4e8e\u5bf9\u67b6\u6784\u548c\u8d28\u91cf\u7684\u628a\u63a7\u3002<\/p>\n<p>\u73b0\u5728&#xff0c;\u8f6e\u5230\u4f60\u4e86&#xff1a;<\/p>\n<ul>\n<li>\u201c\u4f60\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d&#xff0c;\u9047\u5230\u8fc7\u54ea\u4e9b\u56e0\u4e3a\u5916\u90e8\u4f9d\u8d56\u800c\u5bfc\u81f4\u4ee3\u7801\u6781\u5176\u96be\u6d4b\u8bd5\u7684\u2018\u5730\u72f1\u7ea7\u2019\u573a\u666f&#xff1f;\u4f60\u662f\u5982\u4f55\u5316\u89e3\u7684&#xff1f;\u201d<\/li>\n<li>\u201c\u9762\u5bf9\u5feb\u901f\u53d8\u5316\u7684\u6280\u672f\u751f\u6001&#xff0c;\u4f60\u8ba4\u4e3a Python \u5728\u672a\u6765 3-5 \u5e74\u5185&#xff0c;\u6700\u5927\u7684\u53d8\u9769\u4f1a\u53d1\u751f\u5728\u54ea\u4e2a\u9886\u57df&#xff1f;\u201d<\/li>\n<\/ul>\n<p>\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u4e0b\u4f60\u7684\u601d\u8003&#xff0c;\u5206\u4eab\u4f60\u7684\u5f00\u53d1\u7ecf\u9a8c\u3002\u8ba9\u6211\u4eec\u5728\u4ea4\u6d41\u4e2d\u5171\u540c\u6784\u5efa\u66f4\u79ef\u6781\u7684\u6280\u672f\u793e\u533a&#xff01;<\/p>\n<hr \/>\n<h3>8. \u9644\u5f55\u4e0e\u53c2\u8003\u8d44\u6599<\/h3>\n<ul>\n<li>\n<p>\u5b98\u65b9\u6587\u6863\u4e0e\u89c4\u8303&#xff1a;<\/p>\n<\/li>\n<li>\n<p>Python \u5b98\u65b9\u6587\u6863 (docs.python.org)<\/p>\n<\/li>\n<li>\n<p>unittest.mock \u5b98\u65b9\u6307\u5357<\/p>\n<\/li>\n<li>\n<p>PEP 8 \u2013 Style Guide for Python Code<\/p>\n<\/li>\n<li>\n<p>\u63a8\u8350\u4e66\u7c4d&#xff1a;<\/p>\n<\/li>\n<li>\n<p>\u300aPython\u7f16\u7a0b&#xff1a;\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\u300b<\/p>\n<\/li>\n<li>\n<p>\u300a\u6d41\u7545\u7684Python\u300b (Fluent Python)<\/p>\n<\/li>\n<li>\n<p>\u300axUnit Test Patterns\u300b (\u6df1\u5165\u7406\u89e3 Test Doubles \u6982\u5ff5\u7684\u5fc5\u8bfb\u795e\u4f5c)<\/p>\n<\/li>\n<li>\n<p>\u524d\u6cbf\u8d44\u8baf&#xff1a; \u63a8\u8350\u5728 GitHub \u4e0a\u5173\u6ce8 FastAPI\u3001Pytest \u7b49\u70ed\u95e8\u9879\u76ee\u7684\u66f4\u65b0&#xff0c;\u5e76\u8ba2\u9605\u6bcf\u5e74\u7684 PyCon \u5f00\u53d1\u8005\u5927\u4f1a\u8bae\u9898\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u4f60\u5f7b\u5e95\u5398\u6e05\u5404\u79cd\u6d4b\u8bd5\u66ff\u8eab\u7684\u533a\u522b&#xff01;\u4f60\u60f3\u8ba9\u6211\u4e3a\u4f60\u8be6\u7ec6\u6f14\u793a\u5982\u4f55\u5229\u7528 GitHub Actions \u642d\u5efa\u4e00\u4e2a\u96c6\u6210\u4e86\u8fd9\u4e9b\u9ad8\u7ea7\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316 CI \u7ba1\u9053\u5417&#xff1f;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790&#xff1a;\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab&#xff08;Test Doubles&#xff09;\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b<br \/>\n\u5927\u5bb6\u597d&#xff0c;\u6211\u662f\u8001\u9ec4\u3002\u5728\u591a\u5e74\u7684 IT \u6559\u5b66\u4e0e\u4e00\u7ebf\u5f00\u53d1\u5b9e\u6218\u4e2d&#xff0c;\u6211\u89c1\u8bc1\u4e86\u65e0\u6570\u6280\u672f\u7684\u5174\u8870&#xff0c;\u4f46 Python \u59cb\u7ec8\u662f\u6211\u6848\u5934\u6700\u950b\u5229\u7684\u745e\u58eb\u519b\u5200\u3002<br \/>\n2. \u5f00\u7bc7\u5f15\u5165&#xff1a;\u91cd\u5851\u7f16\u7a0b\u751f\u6001\u7684\u201c\u80f6\u6c34\u201d\u54f2\u5b66<br \/>\n\u56de\u671b Python \u7684\u53d1\u5c55\u5386\u7a0b&#xff0c;\u5b83\u4ece\u8bde\u751f\u4e4b\u521d\u51ed\u501f\u7b80\u6d01\u4f18\u96c5\u7684\u8bed\u6cd5\u4fd8\u83b7\u5f00\u53d1\u8005&#xff0c;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,190],"topic":[],"class_list":["post-76311","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790\uff1a\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab\uff08Test Doubles\uff09\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/76311.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790\uff1a\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab\uff08Test Doubles\uff09\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790&#xff1a;\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab&#xff08;Test Doubles&#xff09;\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b \u5927\u5bb6\u597d&#xff0c;\u6211\u662f\u8001\u9ec4\u3002\u5728\u591a\u5e74\u7684 IT \u6559\u5b66\u4e0e\u4e00\u7ebf\u5f00\u53d1\u5b9e\u6218\u4e2d&#xff0c;\u6211\u89c1\u8bc1\u4e86\u65e0\u6570\u6280\u672f\u7684\u5174\u8870&#xff0c;\u4f46 Python \u59cb\u7ec8\u662f\u6211\u6848\u5934\u6700\u950b\u5229\u7684\u745e\u58eb\u519b\u5200\u3002 2. \u5f00\u7bc7\u5f15\u5165&#xff1a;\u91cd\u5851\u7f16\u7a0b\u751f\u6001\u7684\u201c\u80f6\u6c34\u201d\u54f2\u5b66 \u56de\u671b Python \u7684\u53d1\u5c55\u5386\u7a0b&#xff0c;\u5b83\u4ece\u8bde\u751f\u4e4b\u521d\u51ed\u501f\u7b80\u6d01\u4f18\u96c5\u7684\u8bed\u6cd5\u4fd8\u83b7\u5f00\u53d1\u8005&#xff0c;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/76311.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-21T23:25:29+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76311.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/76311.html\",\"name\":\"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790\uff1a\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab\uff08Test Doubles\uff09\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-21T23:25:29+00:00\",\"dateModified\":\"2026-02-21T23:25:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/76311.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/76311.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76311.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790\uff1a\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab\uff08Test Doubles\uff09\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790\uff1a\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab\uff08Test Doubles\uff09\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/76311.html","og_locale":"zh_CN","og_type":"article","og_title":"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790\uff1a\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab\uff08Test Doubles\uff09\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790&#xff1a;\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab&#xff08;Test Doubles&#xff09;\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b \u5927\u5bb6\u597d&#xff0c;\u6211\u662f\u8001\u9ec4\u3002\u5728\u591a\u5e74\u7684 IT \u6559\u5b66\u4e0e\u4e00\u7ebf\u5f00\u53d1\u5b9e\u6218\u4e2d&#xff0c;\u6211\u89c1\u8bc1\u4e86\u65e0\u6570\u6280\u672f\u7684\u5174\u8870&#xff0c;\u4f46 Python \u59cb\u7ec8\u662f\u6211\u6848\u5934\u6700\u950b\u5229\u7684\u745e\u58eb\u519b\u5200\u3002 2. \u5f00\u7bc7\u5f15\u5165&#xff1a;\u91cd\u5851\u7f16\u7a0b\u751f\u6001\u7684\u201c\u80f6\u6c34\u201d\u54f2\u5b66 \u56de\u671b Python \u7684\u53d1\u5c55\u5386\u7a0b&#xff0c;\u5b83\u4ece\u8bde\u751f\u4e4b\u521d\u51ed\u501f\u7b80\u6d01\u4f18\u96c5\u7684\u8bed\u6cd5\u4fd8\u83b7\u5f00\u53d1\u8005&#xff0c;","og_url":"https:\/\/www.wsisp.com\/helps\/76311.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-21T23:25:29+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/76311.html","url":"https:\/\/www.wsisp.com\/helps\/76311.html","name":"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790\uff1a\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab\uff08Test Doubles\uff09\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-21T23:25:29+00:00","dateModified":"2026-02-21T23:25:29+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/76311.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/76311.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/76311.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u300aPython \u7f16\u7a0b\u5168\u666f\u89e3\u6790\uff1a\u4ece\u6838\u5fc3\u7cbe\u8981\u5230\u6d4b\u8bd5\u66ff\u8eab\uff08Test Doubles\uff09\u4e94\u5927\u6b66\u5668\u7684\u5b9e\u6218\u6dec\u70bc\u300b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=76311"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76311\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=76311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=76311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=76311"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=76311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}