{"id":2962,"date":"2023-10-08T13:27:46","date_gmt":"2023-10-08T04:27:46","guid":{"rendered":"https:\/\/edu.ujhb.org\/?p=2962"},"modified":"2023-10-08T13:27:46","modified_gmt":"2023-10-08T04:27:46","slug":"pytest%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/edu.ujhb.org\/?p=2962","title":{"rendered":"pytest\u5165\u95e8"},"content":{"rendered":"\n<p>\u6211\u8981\u505apytest\u7684\u4e2d\u6587\u6559\u7a0b\uff0c\u8bf7\u5148\u7ed9\u51fa\u8be5\u6559\u7a0b\u7684\u5927\u7eb2ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u521b\u5efa\u4e00\u4e2a\u4e2d\u6587\u6559\u7a0b\u5173\u4e8e<code>pytest<\/code>\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u60f3\u6cd5\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u90a3\u4e9b\u6bcd\u8bed\u662f\u4e2d\u6587\u7684IT\u5b66\u4e60\u8005\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u53ef\u80fd\u7684\u5927\u7eb2\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pytest\u4e2d\u6587\u6559\u7a0b\u5927\u7eb2<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u5f15\u8a00<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ec0\u4e48\u662f<code>pytest<\/code><\/li>\n\n\n\n<li><code>pytest<\/code>\u4e0e\u5176\u4ed6\u6d4b\u8bd5\u6846\u67b6\u7684\u6bd4\u8f83<\/li>\n\n\n\n<li>\u4e3a\u4ec0\u4e48\u9009\u62e9<code>pytest<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u5b89\u88c5\u4e0e\u914d\u7f6e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u4f55\u5b89\u88c5<code>pytest<\/code><\/li>\n\n\n\n<li>\u521d\u59cb\u5316\u4e00\u4e2a<code>pytest<\/code>\u9879\u76ee<\/li>\n\n\n\n<li>\u914d\u7f6e\u6587\u4ef6\u89e3\u91ca<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u57fa\u7840\u6982\u5ff5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6d4b\u8bd5\u7528\u4f8b\uff08Test Cases\uff09<\/li>\n\n\n\n<li>\u6d4b\u8bd5\u5939\u5177\uff08Fixtures\uff09<\/li>\n\n\n\n<li>\u65ad\u8a00\uff08Assertions\uff09<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u7f16\u5199\u4f60\u7684\u7b2c\u4e00\u4e2a\u6d4b\u8bd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570<\/li>\n\n\n\n<li>\u7f16\u5199\u6d4b\u8bd5<\/li>\n\n\n\n<li>\u8fd0\u884c\u6d4b\u8bd5<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5. \u4f7f\u7528Fixtures<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ec0\u4e48\u662fFixtures<\/li>\n\n\n\n<li>\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528Fixtures<\/li>\n\n\n\n<li>Fixtures\u7684\u4f5c\u7528\u57df<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">6. \u53c2\u6570\u5316\u6d4b\u8bd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ec0\u4e48\u662f\u53c2\u6570\u5316\u6d4b\u8bd5<\/li>\n\n\n\n<li>\u5982\u4f55\u8fdb\u884c\u53c2\u6570\u5316\u6d4b\u8bd5<\/li>\n\n\n\n<li>\u53c2\u6570\u5316Fixtures<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">7. \u6807\u8bb0\u4e0e\u6807\u7b7e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528<code>pytest.mark<\/code>\u8fdb\u884c\u6807\u8bb0<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u6807\u8bb0<\/li>\n\n\n\n<li>\u8fd0\u884c\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">8. \u5904\u7406\u5f02\u5e38<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528<code>pytest.raises<\/code>\u5904\u7406\u5f02\u5e38<\/li>\n\n\n\n<li>\u4f7f\u7528<code>pytest.warns<\/code>\u5904\u7406\u8b66\u544a<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">9. \u63d2\u4ef6\u4e0e\u6269\u5c55<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5e38\u7528\u7684<code>pytest<\/code>\u63d2\u4ef6<\/li>\n\n\n\n<li>\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528\u63d2\u4ef6<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">10. \u9ad8\u7ea7\u4e3b\u9898<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5e76\u884c\u6d4b\u8bd5<\/li>\n\n\n\n<li>\u4f7f\u7528\u547d\u4ee4\u884c\u9009\u9879<\/li>\n\n\n\n<li>\u96c6\u6210\u5230CI\/CD\u7ba1\u9053<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">11. \u6700\u4f73\u5b9e\u8df5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ee3\u7801\u7ec4\u7ec7<\/li>\n\n\n\n<li>\u6d4b\u8bd5\u8986\u76d6\u7387<\/li>\n\n\n\n<li>\u65e5\u5fd7\u548c\u62a5\u544a<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">12. \u7ed3\u8bba\u4e0e\u4e0b\u4e00\u6b65<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u603b\u7ed3<\/li>\n\n\n\n<li>\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u8d44\u6e90<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u9644\u5f55<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u7b54<\/li>\n\n\n\n<li>\u53c2\u8003\u8d44\u6599<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e2a\u5927\u7eb2\u662f\u6bd4\u8f83\u57fa\u7840\u7684\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u53d7\u4f17\u548c\u76ee\u7684\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5f00\u59cb\u4f60\u7684<code>pytest<\/code>\u4e2d\u6587\u6559\u7a0b\u9879\u76ee\uff01\u5982\u679c\u4f60\u8ba1\u5212\u5c06\u8fd9\u4e2a\u6559\u7a0b\u53d1\u5e03\u5728YouTube\u4e0a\uff0c\u8fd9\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u65b9\u5f0f\u6765\u5438\u5f15\u66f4\u591a\u7684IT\u5b66\u4e60\u8005\u548c\u6f5c\u5728\u5ba2\u6237\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u5f15\u8a00\u7684\u4ec0\u4e48\u662fpytestChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4e0b\u9762\u662f\u5bf9&#8221;\u4ec0\u4e48\u662fpytest&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\uff0c\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u7684&#8221;\u5f15\u8a00&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ec0\u4e48\u662f pytest<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p><code>pytest<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e Python \u7f16\u7a0b\u8bed\u8a00\u7684\u5f00\u6e90\u6d4b\u8bd5\u6846\u67b6\u3002\u5b83\u7528\u4e8e\u7f16\u5199\u7b80\u5355\u548c\u53ef\u6269\u5c55\u7684\u6d4b\u8bd5\u7528\u4f8b\uff0c\u5e76\u4e14\u5177\u6709\u4e30\u5bcc\u7684\u63d2\u4ef6\u4f53\u7cfb\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u7279\u70b9<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7b80\u6d01\u6027<\/strong>: \u4f7f\u7528<code>pytest<\/code>\uff0c\u4f60\u53ef\u4ee5\u907f\u514d\u5927\u91cf\u7684\u6837\u677f\u4ee3\u7801\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528\u7b80\u5355\u7684<code>assert<\/code>\u8bed\u53e5\u548c\u81ea\u52a8\u53d1\u73b0\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u6027<\/strong>: <code>pytest<\/code>\u53ef\u4ee5\u7528\u4e8e\u5355\u5143\u6d4b\u8bd5\u3001\u96c6\u6210\u6d4b\u8bd5\u3001\u7aef\u5230\u7aef\u6d4b\u8bd5\u7b49\uff0c\u51e0\u4e4e\u53ef\u4ee5\u6d4b\u8bd5\u6240\u6709\u7c7b\u578b\u7684\u8f6f\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u4e30\u5bcc\u7684\u63d2\u4ef6\u4f53\u7cfb<\/strong>: \u6709\u5927\u91cf\u7684\u7b2c\u4e09\u65b9\u63d2\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5f00\u53d1\u81ea\u5df1\u7684\u63d2\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u6d4b\u8bd5\u7c7b\u578b<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5355\u5143\u6d4b\u8bd5<\/strong>: \u6d4b\u8bd5\u6700\u5c0f\u7684\u53ef\u6d4b\u8bd5\u90e8\u4ef6\uff08\u901a\u5e38\u662f\u51fd\u6570\u6216\u65b9\u6cd5\uff09\u3002<\/li>\n\n\n\n<li><strong>\u529f\u80fd\u6d4b\u8bd5<\/strong>: \u6d4b\u8bd5\u8f6f\u4ef6\u529f\u80fd\u7684\u67d0\u4e2a\u65b9\u9762\u3002<\/li>\n\n\n\n<li><strong>\u96c6\u6210\u6d4b\u8bd5<\/strong>: \u6d4b\u8bd5\u591a\u4e2a\u7ec4\u4ef6\u6216\u7cfb\u7edf\u7684\u96c6\u6210\u3002<\/li>\n\n\n\n<li><strong>\u7aef\u5230\u7aef\u6d4b\u8bd5<\/strong>: \u6d4b\u8bd5\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5728\u5b9e\u9645\u60c5\u51b5\u4e0b\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e0e\u5176\u4ed6\u6d4b\u8bd5\u6846\u67b6\u7684\u6bd4\u8f83<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u76f8\u5bf9\u4e8e<code>unittest<\/code>\u6216<code>nose<\/code>\uff0c<code>pytest<\/code>\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684API\u548c\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528\u573a\u666f<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Web\u5e94\u7528\u6d4b\u8bd5<\/li>\n\n\n\n<li>\u6570\u636e\u79d1\u5b66\/\u673a\u5668\u5b66\u4e60\u6a21\u578b\u9a8c\u8bc1<\/li>\n\n\n\n<li>\u81ea\u52a8\u5316\u811a\u672c\u9a8c\u8bc1<\/li>\n\n\n\n<li>API\u6d4b\u8bd5<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793e\u7fa4\u4e0e\u652f\u6301<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pytest<\/code>\u6709\u4e00\u4e2a\u6d3b\u8dc3\u7684\u5f00\u6e90\u793e\u7fa4\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u6e20\u9053\uff08\u5982GitHub\u3001Stack Overflow\u3001\u4e13\u95e8\u7684\u8bba\u575b\u7b49\uff09\u83b7\u53d6\u652f\u6301\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u89e3\u91ca\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u7684\u6559\u7a0b\u7684\u4e00\u90e8\u5206\uff0c\u5e2e\u52a9\u521d\u5b66\u8005\u7406\u89e3<code>pytest<\/code>\u7684\u57fa\u7840\u6982\u5ff5\u548c\u5e94\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u5f15\u8a00\u7684pytest\u4e0e\u5176\u4ed6\u6d4b\u8bd5\u6846\u67b6\u7684\u6bd4\u8f83ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u8fd9\u91cc\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u89e3\u91ca\uff0c\u7528\u4e8e\u6bd4\u8f83<code>pytest<\/code>\u4e0e\u5176\u4ed6\u4e3b\u8981\u7684Python\u6d4b\u8bd5\u6846\u67b6\u3002\u8fd9\u4e2a\u5185\u5bb9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b&#8221;\u5f15\u8a00&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">pytest\u4e0e\u5176\u4ed6\u6d4b\u8bd5\u6846\u67b6\u7684\u6bd4\u8f83<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. unittest<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5<\/strong>: <code>unittest<\/code>\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u7ec4\u7ec7\u6d4b\u8bd5\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u66f4\u591a\u7684\u6837\u677f\u4ee3\u7801\u3002<code>pytest<\/code>\u5219\u5141\u8bb8\u66f4\u7b80\u6d01\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\u3002<\/li>\n\n\n\n<li><strong>\u65ad\u8a00<\/strong>: <code>unittest<\/code>\u6709\u4e13\u95e8\u7684\u65ad\u8a00\u65b9\u6cd5\u5982<code>assertEqual()<\/code>, <code>assertTrue()<\/code>\u7b49\uff0c\u800c<code>pytest<\/code>\u5141\u8bb8\u4f7f\u7528\u66f4\u81ea\u7136\u7684<code>assert<\/code>\u8bed\u53e5\u3002<\/li>\n\n\n\n<li><strong>\u6d4b\u8bd5\u53d1\u73b0<\/strong>: <code>pytest<\/code>\u53ef\u4ee5\u81ea\u52a8\u53d1\u73b0\u4ee5<code>test_<\/code>\u5f00\u5934\u7684\u51fd\u6570\uff0c\u800c<code>unittest<\/code>\u9700\u8981\u624b\u52a8\u6dfb\u52a0\u6d4b\u8bd5\u5230\u6d4b\u8bd5\u5957\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. nose\/nose2<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7ef4\u62a4<\/strong>: <code>nose<\/code>\u5df2\u7ecf\u4e0d\u518d\u7ef4\u62a4\uff0c\u800c<code>pytest<\/code>\u6301\u7eed\u5f97\u5230\u826f\u597d\u7684\u7ef4\u62a4\u548c\u66f4\u65b0\u3002<\/li>\n\n\n\n<li><strong>\u63d2\u4ef6<\/strong>: <code>nose<\/code>\u4e5f\u6709\u4e30\u5bcc\u7684\u63d2\u4ef6\uff0c\u4f46<code>pytest<\/code>\u7684\u63d2\u4ef6\u4f53\u7cfb\u66f4\u4e3a\u6d3b\u8dc3\u548c\u591a\u6837\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u6027<\/strong>: <code>pytest<\/code>\u652f\u6301\u66f4\u591a\u9ad8\u7ea7\u7279\u6027\uff0c\u5982\u53c2\u6570\u5316\u6d4b\u8bd5\u3001\u66f4\u590d\u6742\u7684\u5939\u5177\uff08fixtures\uff09\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. doctest<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9002\u7528\u6027<\/strong>: <code>doctest<\/code>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6d4b\u8bd5\u573a\u666f\uff0c\u901a\u5e38\u5d4c\u5165\u5728\u6587\u6863\u4e2d\u3002<code>pytest<\/code>\u5219\u9002\u7528\u4e8e\u66f4\u590d\u6742\u3001\u66f4\u5168\u9762\u7684\u6d4b\u8bd5\u9700\u6c42\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u6269\u5c55\u6027<\/strong>: <code>doctest<\/code>\u5728\u8bbe\u8ba1\u4e0a\u6ca1\u6709\u8003\u8651\u5230\u53ef\u6269\u5c55\u6027\uff0c\u800c<code>pytest<\/code>\u5219\u53ef\u4ee5\u901a\u8fc7\u63d2\u4ef6\u548c\u5185\u7f6e\u529f\u80fd\u8f7b\u677e\u6269\u5c55\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. TestNG\/JUnit\uff08Java\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u8a00\u9650\u5236<\/strong>: TestNG\u548cJUnit\u662fJava\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u800c<code>pytest<\/code>\u662f\u4e13\u4e3aPython\u8bbe\u8ba1\u7684\u3002<\/li>\n\n\n\n<li><strong>\u6613\u7528\u6027<\/strong>: \u76f8\u5bf9\u4e8eJava\u6d4b\u8bd5\u6846\u67b6\uff0c<code>pytest<\/code>\u901a\u5e38\u66f4\u6613\u4e8e\u8bbe\u7f6e\u548c\u4f7f\u7528\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u4e0d\u719f\u6089Java\u751f\u6001\u7cfb\u7edf\u7684\u4eba\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5. QUnit\/Mocha\uff08JavaScript\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u8a00\u9650\u5236<\/strong>: \u8fd9\u4e9b\u662fJavaScript\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u4e0e<code>pytest<\/code>\u670d\u52a1\u4e8e\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\u3002<\/li>\n\n\n\n<li><strong>\u524d\u540e\u7aef\u5206\u79bb<\/strong>: \u5982\u679c\u4f60\u5728\u4e00\u4e2a\u5168\u6808\u9879\u76ee\u4e2d\u5de5\u4f5c\uff0c\u53ef\u80fd\u9700\u8981\u540c\u65f6\u4f7f\u7528\u524d\u7aef\u548c\u540e\u7aef\u7684\u6d4b\u8bd5\u6846\u67b6\u3002<code>pytest<\/code>\u662f\u540e\u7aef\uff08Python\uff09\u7684\u4f18\u79c0\u9009\u62e9\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u6bd4\u8f83\u53ef\u4ee5\u5e2e\u52a9\u521d\u5b66\u8005\u66f4\u5168\u9762\u5730\u4e86\u89e3<code>pytest<\/code>\u4e0e\u5176\u4ed6\u6d4b\u8bd5\u6846\u67b6\u7684\u4f18\u7f3a\u70b9\uff0c\u4ece\u800c\u505a\u51fa\u66f4\u52a0\u660e\u667a\u7684\u9009\u62e9\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684<code>pytest<\/code>\u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u5f15\u8a00\u7684\u4e3a\u4ec0\u4e48\u9009\u62e9pytestChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u4e3a\u4ec0\u4e48\u9009\u62e9pytest&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b&#8221;\u5f15\u8a00&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u91cd\u8981\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u9009\u62e9 pytest<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u7b80\u6d01\u7684\u8bed\u6cd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pytest<\/code>\u5141\u8bb8\u4f60\u4f7f\u7528\u7b80\u5355\u7684<code>assert<\/code>\u8bed\u53e5\u8fdb\u884c\u65ad\u8a00\uff0c\u65e0\u9700\u8bb0\u4f4f\u590d\u6742\u7684\u65ad\u8a00\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u81ea\u52a8\u6d4b\u8bd5\u53d1\u73b0<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u9700\u4e00\u4e2a\u7b80\u5355\u7684<code>pytest<\/code>\u547d\u4ee4\uff0c\u5c31\u53ef\u4ee5\u81ea\u52a8\u53d1\u73b0\u5e76\u8fd0\u884c\u6240\u6709\u4ee5<code>test_<\/code>\u5f00\u5934\u7684\u6d4b\u8bd5\u51fd\u6570\uff0c\u65e0\u9700\u624b\u52a8\u7ec4\u7ec7\u6d4b\u8bd5\u5957\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u5f3a\u5927\u7684\u5939\u5177\uff08Fixture\uff09\u7cfb\u7edf<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pytest<\/code>\u7684\u5939\u5177\u7cfb\u7edf\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bbe\u7f6e\u6d4b\u8bd5\u73af\u5883\uff0c\u8fd9\u5728\u8fdb\u884c\u6570\u636e\u5e93\u6d4b\u8bd5\u3001\u6587\u4ef6\u64cd\u4f5c\u6d4b\u8bd5\u7b49\u65b9\u9762\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u53c2\u6570\u5316\u6d4b\u8bd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pytest<\/code>\u652f\u6301\u53c2\u6570\u5316\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u7528\u5355\u4e2a\u6d4b\u8bd5\u51fd\u6570\u6765\u6267\u884c\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff0c\u63d0\u9ad8\u4ee3\u7801\u91cd\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5. \u4e30\u5bcc\u7684\u63d2\u4ef6\u4f53\u7cfb<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6709\u5927\u91cf\u7684\u7b2c\u4e09\u65b9\u63d2\u4ef6\u53ef\u7528\uff0c\u4e5f\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5f00\u53d1\u81ea\u5df1\u7684\u63d2\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">6. \u8de8\u5e73\u53f0\u4e0e\u6301\u7eed\u96c6\u6210\u652f\u6301<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pytest<\/code>\u53ef\u4ee5\u5728\u51e0\u4e4e\u6240\u6709\u64cd\u4f5c\u7cfb\u7edf\u548cPython\u73af\u5883\u4e0b\u8fd0\u884c\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f7b\u677e\u5730\u96c6\u6210\u5230\u591a\u6570CI\/CD\u5de5\u5177\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">7. \u793e\u7fa4\u652f\u6301<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pytest<\/code>\u6709\u4e00\u4e2a\u5e9e\u5927\u548c\u6d3b\u8dc3\u7684\u793e\u7fa4\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u8f7b\u6613\u5730\u627e\u5230\u89e3\u51b3\u65b9\u6848\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">8. \u591a\u79cd\u6d4b\u8bd5\u7c7b\u578b<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ece\u5355\u5143\u6d4b\u8bd5\u5230\u96c6\u6210\u6d4b\u8bd5\uff0c<code>pytest<\/code>\u90fd\u662f\u4e00\u4e2a\u5168\u80fd\u9009\u624b\u3002\u65e0\u8bba\u4f60\u662f\u5728\u8fdb\u884cAPI\u6d4b\u8bd5\u8fd8\u662fWeb UI\u6d4b\u8bd5\uff0c<code>pytest<\/code>\u90fd\u80fd\u80dc\u4efb\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">9. \u6613\u4e8e\u4e0a\u624b<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e\u65b0\u624b\u548c\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c<code>pytest<\/code>\u90fd\u662f\u6613\u4e8e\u4e0a\u624b\u7684\u3002\u5176\u6587\u6863\u548c\u6559\u7a0b\u8d44\u6e90\u4e5f\u975e\u5e38\u4e30\u5bcc\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">10. \u5546\u4e1a\u4e0e\u5f00\u6e90\u9879\u76ee\u90fd\u9002\u7528<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u65e0\u8bba\u662f\u5c0f\u578b\u7684\u5f00\u6e90\u9879\u76ee\u8fd8\u662f\u5927\u578b\u7684\u5546\u4e1a\u5e94\u7528\uff0c<code>pytest<\/code>\u90fd\u662f\u4e00\u4e2a\u53ef\u9760\u7684\u9009\u62e9\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e9b\u7406\u7531\u7efc\u5408\u8d77\u6765\uff0c\u4f7f\u5f97<code>pytest<\/code>\u6210\u4e3aPython\u6d4b\u8bd5\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u4f18\u79c0\u9009\u62e9\u3002\u901a\u8fc7\u8fd9\u4e2a\u5b50\u8282\uff0c\u4f60\u7684\u89c2\u4f17\u5c06\u80fd\u66f4\u6e05\u6670\u5730\u4e86\u89e3\u4e3a\u4ec0\u4e48<code>pytest<\/code>\u503c\u5f97\u5b66\u4e60\u548c\u4f7f\u7528\u3002\u5e0c\u671b\u8fd9\u5bf9\u4f60\u7684\u6559\u7a0b\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u5b89\u88c5\u4e0e\u914d\u7f6e\u7684\u5982\u4f55\u5b89\u88c5pytestChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u5982\u4f55\u5b89\u88c5pytest&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b&#8221;\u5b89\u88c5\u4e0e\u914d\u7f6e&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5173\u952e\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u5b89\u88c5 pytest<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u7cfb\u7edf\u8981\u6c42<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u5df2\u7ecf\u5b89\u88c5\u4e86Python\uff083.6+\u7248\u672c\u63a8\u8350\uff09\u548cpip\uff08Python\u5305\u7ba1\u7406\u5668\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u4f7f\u7528pip\u5b89\u88c5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6253\u5f00\u7ec8\u7aef\uff08Windows\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u63d0\u793a\u7b26\u6216PowerShell\uff0cMac\u548cLinux\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u7ec8\u7aef\uff09\u3002<\/li>\n\n\n\n<li>\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5e76\u6267\u884c\uff1aCopy code<code>pip install pytest <\/code>\u8fd9\u5c06\u4f1a\u4ecePython\u5305\u7d22\u5f15\uff08PyPI\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684<code>pytest<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u9a8c\u8bc1\u5b89\u88c5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1acssCopy code<code>pytest --version <\/code>\u5982\u679c\u770b\u5230<code>pytest<\/code>\u7684\u7248\u672c\u4fe1\u606f\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. \uff08\u53ef\u9009\uff09\u5b89\u88c5\u63d2\u4ef6<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pytest<\/code>\u6709\u8bb8\u591a\u53ef\u7528\u7684\u63d2\u4ef6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1acssCopy code<code>pip install pytest-[\u63d2\u4ef6\u540d]<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5. \uff08\u53ef\u9009\uff09\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u865a\u62df\u73af\u5883\uff08\u5982<code>venv<\/code>\u6216<code>conda<\/code>\uff09\uff0c\u786e\u4fdd\u9996\u5148\u6fc0\u6d3b\u4f60\u7684\u865a\u62df\u73af\u5883\uff0c\u7136\u540e\u518d\u6267\u884c\u4e0a\u8ff0\u7684<code>pip<\/code>\u547d\u4ee4\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">6. IDE\u96c6\u6210<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5927\u591a\u6570\u73b0\u4ee3IDE\uff08\u5982PyCharm, VSCode\u7b49\uff09\u90fd\u652f\u6301<code>pytest<\/code>\uff0c\u4f60\u53ea\u9700\u5728IDE\u7684\u8bbe\u7f6e\u6216\u914d\u7f6e\u4e2d\u9009\u62e9<code>pytest<\/code>\u4f5c\u4e3a\u6d4b\u8bd5\u6846\u67b6\u5373\u53ef\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u5b89\u88c5<code>pytest<\/code>\uff0c\u5305\u62ec\u53ef\u9009\u6b65\u9aa4\u548c\u4e00\u4e9b\u989d\u5916\u7684\u4fe1\u606f\uff0c\u4ee5\u5e2e\u52a9\u521d\u5b66\u8005\u66f4\u5bb9\u6613\u5730\u5f00\u59cb\u4f7f\u7528\u8fd9\u4e2a\u6d4b\u8bd5\u6846\u67b6\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684<code>pytest<\/code>\u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u5b89\u88c5\u4e0e\u914d\u7f6e\u7684\u521d\u59cb\u5316\u4e00\u4e2apytest\u9879\u76eeChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u521d\u59cb\u5316\u4e00\u4e2apytest \u9879\u76ee&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b&#8221;\u5b89\u88c5\u4e0e\u914d\u7f6e&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u91cd\u8981\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u521d\u59cb\u5316\u4e00\u4e2a pytest \u9879\u76ee<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u521b\u5efa\u9879\u76ee\u76ee\u5f55<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u4f60\u7684\u5de5\u4f5c\u533a\u57df\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\u4f5c\u4e3a\u4f60\u7684 pytest \u9879\u76ee\u7684\u6839\u76ee\u5f55\u3002bashCopy code<code>mkdir my_pytest_project cd my_pytest_project<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u521d\u59cb\u5316\u865a\u62df\u73af\u5883\uff08\u53ef\u9009\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4f60\u60f3\u5728\u4e00\u4e2a\u9694\u79bb\u7684\u73af\u5883\u4e2d\u7ba1\u7406\u4f9d\u8d56\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\u3002\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 venv\uff08\u63a8\u8350\uff09<\/strong>Copy code<code>python3 -m venv venv<\/code>\n<ul class=\"wp-block-list\">\n<li>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a\n<ul class=\"wp-block-list\">\n<li><strong>Windows<\/strong>mathematicaCopy code<code>.\\venv\\Scripts\\Activate<\/code><\/li>\n\n\n\n<li><strong>Mac\/Linux<\/strong>bashCopy code<code>source venv\/bin\/activate<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 conda<\/strong>luaCopy code<code>conda create --name my_pytest_env python=3.8 conda activate my_pytest_env<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u5b89\u88c5 pytest<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5 pytest\uff0c\u73b0\u5728\u662f\u65f6\u5019\u5b89\u88c5\u5b83\u4e86\u3002Copy code<code>pip install pytest<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u521b\u5efa\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Python \u6587\u4ef6\uff0c\u901a\u5e38\u4ee5 <code>test_<\/code> \u5f00\u5934\uff0c\u4f8b\u5982 <code>test_example.py<\/code>\u3002pythonCopy code<code># \u5185\u5bb9\u793a\u4f8b def test_addition(): assert 1 + 1 == 2<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5. \u8fd0\u884c\u6d4b\u8bd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e2d\u6253\u5f00\u7ec8\u7aef\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1aCopy code<code>pytest <\/code>\u5982\u679c\u4e00\u5207\u8bbe\u7f6e\u6b63\u786e\uff0c\u4f60\u5e94\u8be5\u770b\u5230\u6d4b\u8bd5\u8fd0\u884c\u5e76\u901a\u8fc7\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">6. \u6dfb\u52a0 pytest \u914d\u7f6e\u6587\u4ef6\uff08\u53ef\u9009\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a <code>pytest.ini<\/code> \u6216 <code>pyproject.toml<\/code> \u6587\u4ef6\u6765\u5b58\u50a8\u9879\u76ee\u7279\u5b9a\u7684 pytest \u914d\u7f6e\u3002\n<ul class=\"wp-block-list\">\n<li><strong>pytest.ini \u793a\u4f8b<\/strong>iniCopy code<code>[pytest] addopts = --verbose<\/code><\/li>\n\n\n\n<li><strong>pyproject.toml \u793a\u4f8b<\/strong>tomlCopy code<code>[tool.pytest.ini_options] addopts = \"--verbose\"<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">7. IDE \u96c6\u6210\uff08\u53ef\u9009\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4f60\u4f7f\u7528 IDE\uff08\u5982 PyCharm \u6216 VSCode\uff09\uff0c\u786e\u4fdd\u5728\u8bbe\u7f6e\u4e2d\u9009\u62e9 pytest \u4f5c\u4e3a\u6d4b\u8bd5\u6846\u67b6\uff0c\u5e76\u6307\u5411\u76f8\u5e94\u7684\u865a\u62df\u73af\u5883\uff08\u5982\u679c\u6709\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u63d0\u4f9b\u4e86\u4e00\u6b65\u6b65\u7684\u6307\u5bfc\uff0c\u4ece\u521b\u5efa\u9879\u76ee\u76ee\u5f55\u5230\u8fd0\u884c\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\uff0c\u4ee5\u53ca\u5982\u4f55\u8fdb\u884c\u4e00\u4e9b\u53ef\u9009\u7684\u9ad8\u7ea7\u914d\u7f6e\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u7684 pytest \u9879\u76ee\u3002\u5e0c\u671b\u8fd9\u5bf9\u4f60\u7684\u6559\u7a0b\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u5b89\u88c5\u4e0e\u914d\u7f6e\u7684\u914d\u7f6e\u6587\u4ef6\u89e3\u91caChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u914d\u7f6e\u6587\u4ef6\u89e3\u91ca&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u5185\u5bb9\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b&#8221;\u5b89\u88c5\u4e0e\u914d\u7f6e&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u914d\u7f6e\u6587\u4ef6\u89e3\u91ca<\/h3>\n\n\n\n<p>\u5728 <code>pytest<\/code> \u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u81ea\u5b9a\u4e49\u6d4b\u8bd5\u884c\u4e3a\u3002\u6700\u5e38\u7528\u7684\u914d\u7f6e\u6587\u4ef6\u662f <code>pytest.ini<\/code>\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>pyproject.toml<\/code> \u6216 <code>setup.cfg<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. pytest.ini<\/h4>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u4e13\u7528\u4e8e <code>pytest<\/code> \u7684\u914d\u7f6e\u6587\u4ef6\u3002\u5b83\u4f7f\u7528 INI \u683c\u5f0f\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u793a\u4f8b<\/strong>iniCopy code<code>[pytest] addopts = --verbose markers = slow: marks tests as slow (deselect with '-m \"not slow\"')<\/code><\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u9009\u9879<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>addopts<\/code>: \u6dfb\u52a0\u9ed8\u8ba4\u7684\u547d\u4ee4\u884c\u9009\u9879\u3002<\/li>\n\n\n\n<li><code>markers<\/code>: \u81ea\u5b9a\u4e49\u6807\u8bb0\uff0c\u7528\u4e8e\u5206\u7c7b\u6216\u6807\u7b7e\u6d4b\u8bd5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. pyproject.toml<\/h4>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u66f4\u901a\u7528\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u7528\u4e8e Python \u9879\u76ee\u7684\u5404\u79cd\u5de5\u5177\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u793a\u4f8b<\/strong>tomlCopy code<code>[tool.pytest.ini_options] addopts = \"--verbose\" markers = [\"slow: marks tests as slow\"]<\/code><\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u9009\u9879<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u540c <code>pytest.ini<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. setup.cfg<\/h4>\n\n\n\n<p>\u8fd9\u4e5f\u662f\u4e00\u4e2a\u901a\u7528\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u4f46\u8f83\u65e7\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u793a\u4f8b<\/strong>cfgCopy code<code>[tool:pytest] addopts = --verbose markers = slow: marks tests as slow<\/code><\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u9009\u9879<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u540c <code>pytest.ini<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u5e38\u7528\u914d\u7f6e\u9009\u9879<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>testpaths<\/code>: \u6307\u5b9a\u6d4b\u8bd5\u641c\u7d22\u7684\u76ee\u5f55\u3002<\/li>\n\n\n\n<li><code>python_files<\/code>: \u6307\u5b9a\u6d4b\u8bd5\u6587\u4ef6\u7684\u6a21\u5f0f\u3002<\/li>\n\n\n\n<li><code>python_classes<\/code>: \u6307\u5b9a\u6d4b\u8bd5\u7c7b\u7684\u6a21\u5f0f\u3002<\/li>\n\n\n\n<li><code>python_functions<\/code>: \u6307\u5b9a\u6d4b\u8bd5\u51fd\u6570\u7684\u6a21\u5f0f\u3002<\/li>\n\n\n\n<li><code>xfail_strict<\/code>: \u4e25\u683c\u5904\u7406\u9884\u671f\u5931\u8d25\u7684\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><code>junit_family<\/code>: \u6307\u5b9aJUnit\u62a5\u544a\u7684\u683c\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u914d\u7f6e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>filterwarnings<\/code>: \u63a7\u5236\u8b66\u544a\u7684\u663e\u793a\u3002<\/li>\n\n\n\n<li><code>log_format<\/code>: \u81ea\u5b9a\u4e49\u65e5\u5fd7\u683c\u5f0f\u3002<\/li>\n\n\n\n<li><code>log_level<\/code>: \u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u89e3\u91ca\u4e86\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u914d\u7f6e\u6587\u4ef6\u4ee5\u53ca\u5404\u79cd\u53ef\u7528\u7684\u914d\u7f6e\u9009\u9879\u3002\u8fd9\u5c06\u5e2e\u52a9\u521d\u5b66\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u81ea\u5b9a\u4e49\u4ed6\u4eec\u7684 <code>pytest<\/code> \u6d4b\u8bd5\u73af\u5883\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 <code>pytest<\/code> \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91capytest\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u63d2\u4ef6\uff0c\u548csnapshot-warn-unused\u53c2\u6570\u76f8\u5173\u7684\u63d2\u4ef6\u662f\u54ea\u4e2aChatGPT<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u7528\u7684 pytest \u63d2\u4ef6<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. pytest-cov<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u7528\u4e8e\u6d4b\u91cf\u4ee3\u7801\u8986\u76d6\u7387\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-cov<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: <code>pytest --cov=myproject<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. pytest-xdist<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u7528\u4e8e\u5e76\u884c\u8fd0\u884c\u6d4b\u8bd5\uff0c\u63d0\u9ad8\u6d4b\u8bd5\u901f\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-xdist<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: <code>pytest -n 4<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. pytest-django<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u7528\u4e8e\u6d4b\u8bd5 Django \u9879\u76ee\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-django<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: \u5728 Django \u9879\u76ee\u4e2d\u76f4\u63a5\u4f7f\u7528 <code>pytest<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. pytest-mock<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 mock \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-mock<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: \u5728\u6d4b\u8bd5\u51fd\u6570\u4e2d\u4f7f\u7528 <code>mocker<\/code> fixture\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5. pytest-html<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u751f\u6210 HTML \u683c\u5f0f\u7684\u6d4b\u8bd5\u62a5\u544a\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-html<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: <code>pytest --html=report.html<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">6. pytest-bdd<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u652f\u6301 Behavior-Driven Development (BDD)\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-bdd<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: \u4f7f\u7528 Gherkin \u8bed\u8a00\u7f16\u5199\u6d4b\u8bd5\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">7. pytest-selenium<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u7528\u4e8e Selenium \u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-selenium<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: \u5728\u6d4b\u8bd5\u4e2d\u4f7f\u7528 <code>selenium<\/code> fixture\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">8. pytest-flask<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u7528\u4e8e\u6d4b\u8bd5 Flask \u5e94\u7528\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-flask<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: \u5728 Flask \u9879\u76ee\u4e2d\u76f4\u63a5\u4f7f\u7528 <code>pytest<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">9. pytest-asyncio<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u7528\u4e8e\u6d4b\u8bd5\u5f02\u6b65\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-asyncio<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: \u4f7f\u7528 <code>@pytest.mark.asyncio<\/code> \u88c5\u9970\u5668\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">10. pytest-snapshots<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u7528\u4e8e\u521b\u5efa\u548c\u6bd4\u8f83\u6d4b\u8bd5\u7684\u5feb\u7167\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-snapshots<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: \u4f7f\u7528 <code>snapshot<\/code> fixture\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u4e8e <code>snapshot-warn-unused<\/code> \u53c2\u6570<\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u53c2\u6570\u4e0e <code>pytest-snapshots<\/code> \u63d2\u4ef6\u76f8\u5173\u3002\u5f53\u4f60\u4f7f\u7528\u8fd9\u4e2a\u63d2\u4ef6\u521b\u5efa\u6d4b\u8bd5\u5feb\u7167\u540e\uff0c\u5982\u679c\u6709\u672a\u4f7f\u7528\u7684\u5feb\u7167\uff0c\u4f7f\u7528 <code>--snapshot-warn-unused<\/code> \u53c2\u6570\u5c06\u4f1a\u5728\u6d4b\u8bd5\u8f93\u51fa\u4e2d\u7ed9\u51fa\u8b66\u544a\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-snapshots<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: <code>pytest --snapshot-warn-unused<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e2a\u53c2\u6570\u6709\u52a9\u4e8e\u8bc6\u522b\u548c\u6e05\u7406\u4e0d\u518d\u9700\u8981\u7684\u6d4b\u8bd5\u5feb\u7167\uff0c\u4ece\u800c\u4fdd\u6301\u6d4b\u8bd5\u5957\u4ef6\u7684\u6574\u6d01\u3002<\/p>\n\n\n\n<p>\u5e0c\u671b\u8fd9\u4e9b\u4fe1\u606f\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 <code>pytest<\/code> \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u5185\u5bb9\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>ERROR: Could not find a version that satisfies the requirement pytest-snapshots (from versions: none)ChatGPT<\/p>\n\n\n\n<p>\u62b1\u6b49\uff0c\u6211\u72af\u4e86\u4e00\u4e2a\u9519\u8bef\u3002<code>pytest-snapshots<\/code> \u5e76\u4e0d\u662f\u4e00\u4e2a\u5b9e\u9645\u5b58\u5728\u7684\u5b98\u65b9 pytest \u63d2\u4ef6\u3002\u5982\u679c\u4f60\u60f3\u8fdb\u884c\u5feb\u7167\u6d4b\u8bd5\uff0c\u4f60\u53ef\u80fd\u4f1a\u5bf9 <code>pytest-snapshot<\/code>\uff08\u6ce8\u610f\u6ca1\u6709\u6700\u540e\u7684 &#8216;s&#8217;\uff09\u6216\u8005 <code>snapshottest<\/code> \u611f\u5174\u8da3\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. pytest-snapshot<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u7528\u4e8e\u521b\u5efa\u548c\u6bd4\u8f83\u6d4b\u8bd5\u7684\u5feb\u7167\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install pytest-snapshot<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: \u4f7f\u7528 <code>snapshot<\/code> fixture\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. snapshottest<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>: \u7528\u4e8e\u521b\u5efa\u548c\u6bd4\u8f83\u6d4b\u8bd5\u7684\u5feb\u7167\uff0c\u7279\u522b\u662f\u7528\u4e8e Django\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u88c5<\/strong>: <code>pip install snapshottest<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>: \u7ee7\u626d\u81ea <code>snapshottest.TestCase<\/code> \u5e76\u4f7f\u7528 <code>self.assertMatchSnapshot()<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5bf9\u4e8e <code>snapshot-warn-unused<\/code> \u8fd9\u6837\u7684\u7279\u5b9a\u53c2\u6570\uff0c\u8fd9\u4e9b\u5e93\u53ef\u80fd\u6709\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u5177\u4f53\u7684\u53c2\u6570\u540d\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u4f60\u53ef\u4ee5\u67e5\u9605\u8fd9\u4e9b\u5e93\u7684\u6587\u6863\u4ee5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u518d\u6b21\u5bf9\u4e4b\u524d\u7684\u9519\u8bef\u8868\u793a\u62b1\u6b49\uff0c\u5e0c\u671b\u8fd9\u6b21\u7684\u4fe1\u606f\u66f4\u51c6\u786e\u548c\u6709\u7528\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>ImportError: cannot import name &#8216;_request_ctx_stack&#8217; from &#8216;flask&#8217;ChatGPT<\/p>\n\n\n\n<p>\u8be5\u9519\u8bef\u901a\u5e38\u51fa\u73b0\u5728\u5c1d\u8bd5\u4ece Flask \u5305\u5bfc\u5165 <code>_request_ctx_stack<\/code> \u65f6\u3002\u8fd9\u901a\u5e38\u662f\u7531\u4e8e Flask \u7248\u672c\u4e0e\u4f60\u7684\u4ee3\u7801\u6216\u4f9d\u8d56\u5e93\u4e0d\u517c\u5bb9\u6240\u5bfc\u81f4\u7684\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u66f4\u65b0 Flask<\/h3>\n\n\n\n<p>\u5c1d\u8bd5\u66f4\u65b0 Flask \u5230\u6700\u65b0\u7248\u672c\uff1a<\/p>\n\n\n\n<p>bashCopy code<\/p>\n\n\n\n<p><code>pip install --upgrade Flask<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u68c0\u67e5\u4f9d\u8d56<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u4f9d\u8d56\u4e8e\u7279\u5b9a\u7248\u672c\u7684 Flask\uff0c\u786e\u4fdd\u4f60\u7684 <code>requirements.txt<\/code> \u6216 <code>setup.py<\/code> \u6587\u4ef6\u4e2d\u6307\u5b9a\u4e86\u6b63\u786e\u7684\u7248\u672c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u68c0\u67e5\u4ee3\u7801<\/h3>\n\n\n\n<p>\u786e\u4fdd\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u5c1d\u8bd5\u76f4\u63a5\u4ece Flask \u5bfc\u5165 <code>_request_ctx_stack<\/code>\uff0c\u901a\u5e38\u4f60\u5e94\u8be5\u4e0d\u9700\u8981\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e2a\u5185\u90e8\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u68c0\u67e5\u5176\u4ed6\u5e93<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u4f9d\u8d56\u4e8e Flask \u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u8fd9\u4e2a\u5e93\u53ef\u80fd\u662f\u95ee\u9898\u7684\u6839\u6e90\u3002\u5c1d\u8bd5\u66f4\u65b0\u8fd9\u4e9b\u5e93\uff0c\u6216\u68c0\u67e5\u5176\u4e0e Flask \u7684\u517c\u5bb9\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u865a\u62df\u73af\u5883<\/h3>\n\n\n\n<p>\u786e\u4fdd\u4f60\u5728\u6b63\u786e\u7684 Python \u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884c\u4ee3\u7801\u3002\u6709\u65f6\uff0c\u7cfb\u7edf\u7ea7\u522b\u7684 Python \u73af\u5883\u548c\u9879\u76ee\u7ea7\u522b\u7684\u865a\u62df\u73af\u5883\u53ef\u80fd\u4f1a\u5bfc\u81f4\u51b2\u7a81\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u6e05\u7406<\/h3>\n\n\n\n<p>\u5c1d\u8bd5\u5220\u9664 <code>__pycache__<\/code> \u76ee\u5f55\u548c <code>.pyc<\/code> \u6587\u4ef6\uff0c\u7136\u540e\u91cd\u65b0\u8fd0\u884c\u4f60\u7684\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p>bashCopy code<\/p>\n\n\n\n<p><code>find . -name \"*.pyc\" -exec rm -f {} \\; find . -name \"__pycache__\" -exec rm -rf {} \\;<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u67e5\u9605\u6587\u6863<\/h3>\n\n\n\n<p>\u67e5\u9605 Flask \u548c\u4efb\u4f55\u76f8\u5173\u5e93\u7684\u6587\u6863\uff0c\u770b\u770b\u662f\u5426\u6709\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u7684\u7279\u5b9a\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4ee5\u4e0a\u65b9\u6cd5\u90fd\u4e0d\u80fd\u89e3\u51b3\u95ee\u9898\uff0c\u5efa\u8bae\u521b\u5efa\u4e00\u4e2a\u6700\u5c0f\u53ef\u590d\u73b0\u95ee\u9898\u7684\u4ee3\u7801\u6837\u672c\uff0c\u5e76\u67e5\u9605 Flask \u7684 issue \u8ddf\u8e2a\u5668\u6216\u76f8\u5173\u793e\u533a\u4ee5\u5bfb\u6c42\u5e2e\u52a9\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca \u57fa\u7840\u6982\u5ff5\u7684\u6d4b\u8bd5\u7528\u4f8b\uff08Test Cases\uff09ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u6d4b\u8bd5\u7528\u4f8b\uff08Test Cases\uff09&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b&#8221;\u57fa\u7840\u6982\u5ff5&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5173\u952e\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6d4b\u8bd5\u7528\u4f8b\uff08Test Cases\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u6d4b\u8bd5\u7528\u4f8b\u662f\u4e00\u7ec4\u6761\u4ef6\u6216\u53d8\u91cf\uff0c\u7528\u4e8e\u786e\u5b9a\u4e00\u4e2a\u7cfb\u7edf\u6216\u8f6f\u4ef6\u5e94\u7528\u662f\u5426\u80fd\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u5e76\u80fd\u6b63\u786e\u5de5\u4f5c\u3002\u5728 pytest \u4e2d\uff0c\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u901a\u5e38\u662f\u4e00\u4e2a\u4ee5 <code>test_<\/code> \u524d\u7f00\u547d\u540d\u7684 Python \u51fd\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7ed3\u6784<\/h4>\n\n\n\n<p>\u4e00\u4e2a\u57fa\u7840\u7684 pytest \u6d4b\u8bd5\u7528\u4f8b\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_addition(): assert 1 + 1 == 2<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u7ec4\u6210\u90e8\u5206<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6d4b\u8bd5\u51fd\u6570<\/strong>: \u4ee5 <code>test_<\/code> \u5f00\u5934\u7684\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>\u65ad\u8a00\uff08Assertions\uff09<\/strong>: \u4f7f\u7528 <code>assert<\/code> \u5173\u952e\u5b57\u6765\u9a8c\u8bc1\u88ab\u6d4b\u8bd5\u4ee3\u7801\u7684\u884c\u4e3a\u3002<\/li>\n\n\n\n<li><strong>\u5939\u5177\uff08Fixtures\uff09<\/strong>: \u53ef\u9009\u7684\u9884\u7f6e\u6761\u4ef6\u6216\u914d\u7f6e\u3002<\/li>\n\n\n\n<li><strong>\u6807\u8bb0\uff08Markers\uff09<\/strong>: \u53ef\u9009\u7684\u6807\u7b7e\uff0c\u7528\u4e8e\u5206\u7c7b\u6216\u9009\u62e9\u6027\u8fd0\u884c\u6d4b\u8bd5\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u65ad\u8a00<\/h4>\n\n\n\n<p>\u65ad\u8a00\u662f\u6d4b\u8bd5\u7528\u4f8b\u7684\u6838\u5fc3\u3002\u5b83\u4eec\u7528\u4e8e\u9a8c\u8bc1\u88ab\u6d4b\u8bd5\u7684\u4ee3\u7801\u662f\u5426\u8fd4\u56de\u9884\u671f\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_subtraction(): assert 5 - 3 == 2<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5939\u5177\uff08Fixtures\uff09<\/h4>\n\n\n\n<p>\u5939\u5177\u7528\u4e8e\u8bbe\u7f6e\u6d4b\u8bd5\u73af\u5883\u6216\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5939\u5177\u6765\u521b\u5efa\u548c\u9500\u6bc1\u8fd9\u4e2a\u8fde\u63a5\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.fixture def database_connection(): conn = create_connection() yield conn conn.close() def test_database_query(database_connection): assert database_connection.query(\"SELECT 1\") == 1<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6807\u8bb0\uff08Markers\uff09<\/h4>\n\n\n\n<p>\u6807\u8bb0\u53ef\u4ee5\u7528\u4e8e\u6dfb\u52a0\u5143\u6570\u636e\u5230\u6d4b\u8bd5\u7528\u4f8b\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u7528\u547d\u4ee4\u884c\u9009\u9879\u6765\u9009\u62e9\u6027\u5730\u8fd0\u884c\u5b83\u4eec\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.mark.slow def test_large_computation(): assert compute_large_value() == \"expected_value\"<\/code><\/p>\n\n\n\n<p>\u8fd0\u884c\u6162\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<p>bashCopy code<\/p>\n\n\n\n<p><code>pytest -m slow<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u6570\u5316\uff08Parameterization\uff09<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u5316\u6765\u7528\u591a\u7ec4\u6570\u636e\u8fd0\u884c\u540c\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.mark.parametrize(\"input,expected\", [(5, 25), (2, 4), (3, 9)]) def test_squares(input, expected): assert input ** 2 == expected<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u63d0\u4f9b\u4e86\u5173\u4e8e\u6d4b\u8bd5\u7528\u4f8b\u7684\u5168\u9762\u800c\u8be6\u7ec6\u7684\u89e3\u91ca\uff0c\u5305\u62ec\u5176\u5b9a\u4e49\u3001\u7ed3\u6784\u3001\u4e3b\u8981\u7ec4\u6210\u90e8\u5206\u4ee5\u53ca\u5982\u4f55\u5728 pytest \u4e2d\u5b9e\u73b0\u5b83\u4eec\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u7684 pytest \u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u5bf9\u4f60\u7684\u6559\u7a0b\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca \u57fa\u7840\u6982\u5ff5\u7684\u6d4b\u8bd5\u5939\u5177\uff08Fixtures\uff09ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u6d4b\u8bd5\u5939\u5177\uff08Fixtures\uff09&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b&#8221;\u57fa\u7840\u6982\u5ff5&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u91cd\u8981\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6d4b\u8bd5\u5939\u5177\uff08Fixtures\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u5728 pytest \u4e2d\uff0c\u5939\u5177\uff08Fixtures\uff09\u7528\u4e8e\u8bbe\u7f6e\u6d4b\u8bd5\u51fd\u6570\u6240\u9700\u7684\u9884\u7f6e\u6761\u4ef6\u6216\u73af\u5883\u3002\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u3001\u521d\u59cb\u5316\u53d8\u91cf\u3001\u8bbe\u7f6e\u6587\u4ef6\u6216\u8005\u6267\u884c\u4efb\u4f55\u5176\u4ed6\u5fc5\u8981\u7684\u8bbe\u7f6e\u6216\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u521b\u5efa\u5939\u5177<\/h4>\n\n\n\n<p>\u4f7f\u7528 <code>@pytest.fixture<\/code> \u88c5\u9970\u5668\u6765\u5b9a\u4e49\u4e00\u4e2a\u5939\u5177\u3002\u8fd9\u4e2a\u5939\u5177\u51fd\u6570\u8fd4\u56de\u6240\u9700\u7684\u8d44\u6e90\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>import pytest @pytest.fixture def example_data(): return {\"key\": \"value\"}<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528\u5939\u5177<\/h4>\n\n\n\n<p>\u8981\u5728\u6d4b\u8bd5\u51fd\u6570\u4e2d\u4f7f\u7528\u5939\u5177\uff0c\u4f60\u9700\u8981\u5c06\u5939\u5177\u51fd\u6570\u540d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6d4b\u8bd5\u51fd\u6570\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_example(example_data): assert example_data[\"key\"] == \"value\"<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5939\u5177\u7684\u8303\u56f4\uff08Scope\uff09<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>scope<\/code> \u53c2\u6570\u6765\u63a7\u5236\u5939\u5177\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u51fd\u6570\u7ea7\u522b\uff08\u9ed8\u8ba4\uff09<\/strong>: \u5bf9\u6bcf\u4e2a\u6d4b\u8bd5\u51fd\u6570\u91cd\u65b0\u6267\u884c\u5939\u5177\u3002<\/li>\n\n\n\n<li><strong>\u6a21\u5757\u7ea7\u522b<\/strong>: \u5728\u6574\u4e2a\u6a21\u5757\u4e2d\u53ea\u6267\u884c\u4e00\u6b21\u3002<\/li>\n\n\n\n<li><strong>\u4f1a\u8bdd\u7ea7\u522b<\/strong>: \u5728\u6574\u4e2a\u6d4b\u8bd5\u4f1a\u8bdd\u4e2d\u53ea\u6267\u884c\u4e00\u6b21\u3002<\/li>\n<\/ul>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.fixture(scope=\"module\") def example_data(): return {\"key\": \"value\"}<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u6570\u5316\u5939\u5177<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>params<\/code> \u53c2\u6570\u6765\u53c2\u6570\u5316\u5939\u5177\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.fixture(params=[1, 2, 3]) def example_data(request): return request.param<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5939\u5177\u4f9d\u8d56<\/h4>\n\n\n\n<p>\u4e00\u4e2a\u5939\u5177\u53ef\u4ee5\u4f9d\u8d56\u4e8e\u5176\u4ed6\u5939\u5177\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.fixture def dependent_fixture(example_data): return example_data[\"key\"].upper()<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 yield \u8fdb\u884c\u6e05\u7406<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>yield<\/code> \u8bed\u53e5\u5728\u5939\u5177\u4e2d\u8fdb\u884c\u8d44\u6e90\u6e05\u7406\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.fixture def database_connection(): conn = create_connection() yield conn conn.close()<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u81ea\u52a8\u4f7f\u7528\u5939\u5177<\/h4>\n\n\n\n<p>\u4f7f\u7528 <code>autouse=True<\/code> \u53ef\u4ee5\u81ea\u52a8\u5e94\u7528\u5939\u5177\u5230\u6574\u4e2a\u6d4b\u8bd5\u4f1a\u8bdd\u6216\u6a21\u5757\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.fixture(autouse=True) def setup_and_teardown(): # Setup code yield # Teardown code<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u6d4b\u8bd5\u5939\u5177\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u5982\u4f55\u521b\u5efa\u3001\u4f7f\u7528\u3001\u53c2\u6570\u5316\u548c\u7ba1\u7406\u5939\u5177\u7684\u751f\u547d\u5468\u671f\u3002\u8fd9\u5c06\u5e2e\u52a9\u521d\u5b66\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528 pytest \u4e2d\u7684\u5939\u5177\u6765\u521b\u5efa\u66f4\u52a0\u5065\u58ee\u548c\u53ef\u7ef4\u62a4\u7684\u6d4b\u8bd5\u4ee3\u7801\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca \u57fa\u7840\u6982\u5ff5\u7684\u65ad\u8a00\uff08Assertions\uff09ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u65ad\u8a00\uff08Assertions\uff09&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b&#8221;\u57fa\u7840\u6982\u5ff5&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5173\u952e\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u65ad\u8a00\uff08Assertions\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u65ad\u8a00\u662f\u6d4b\u8bd5\u4e2d\u7528\u4e8e\u9a8c\u8bc1\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u7684\u8bed\u53e5\u3002\u5982\u679c\u65ad\u8a00\u5931\u8d25\uff08\u5373\u6761\u4ef6\u4e3a\u5047\uff09\uff0c\u5219\u6d4b\u8bd5\u5931\u8d25\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u9519\u8bef\u62a5\u544a\u3002\u5728 pytest \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 Python \u7684\u5185\u7f6e <code>assert<\/code> \u8bed\u53e5\u6765\u8fdb\u884c\u65ad\u8a00\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u7528\u6cd5<\/h4>\n\n\n\n<p>\u6700\u7b80\u5355\u7684\u65ad\u8a00\u662f\u68c0\u67e5\u4e00\u4e2a\u8868\u8fbe\u5f0f\u662f\u5426\u4e3a\u771f\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_addition(): assert 1 + 1 == 2<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u65ad\u8a00\u8868\u8fbe\u5f0f<\/h4>\n\n\n\n<p>\u9664\u4e86\u57fa\u7840\u7684\u7b49\u4e8e\u68c0\u67e5\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u4efb\u4f55\u6709\u6548\u7684 Python \u8868\u8fbe\u5f0f\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_collection(): assert 1 in [1, 2, 3]<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u591a\u91cd\u65ad\u8a00<\/h4>\n\n\n\n<p>\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u65ad\u8a00\uff0c\u4f46\u8981\u6ce8\u610f\uff0c\u5982\u679c\u5176\u4e2d\u4e00\u4e2a\u65ad\u8a00\u5931\u8d25\uff0c\u8be5\u6d4b\u8bd5\u7528\u4f8b\u5c06\u505c\u6b62\u6267\u884c\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_multiple_assertions(): assert 1 + 1 == 2 assert 2 + 2 == 4<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u65ad\u8a00\u5f02\u5e38<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>pytest.raises<\/code> \u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u65ad\u8a00\u662f\u5426\u629b\u51fa\u4e86\u9884\u671f\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_exception(): with pytest.raises(ValueError): int(\"invalid\")<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u5931\u8d25\u6d88\u606f<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4e3a <code>assert<\/code> \u8bed\u53e5\u63d0\u4f9b\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u5931\u8d25\u6d88\u606f\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_custom_message(): assert 1 + 1 == 3, \"Custom failure message: 1 + 1 should equal 2, not 3\"<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u590d\u6742\u5bf9\u8c61\u6bd4\u8f83<\/h4>\n\n\n\n<p>\u5bf9\u4e8e\u590d\u6742\u7684\u5bf9\u8c61\u6bd4\u8f83\uff0c\u5982\u5217\u8868\u6216\u5b57\u5178\uff0cpytest \u4f1a\u63d0\u4f9b\u8be6\u7ec6\u7684\u6bd4\u8f83\u8f93\u51fa\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_list(): assert [1, 2, 3] == [1, 2, 4]<\/code><\/p>\n\n\n\n<p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u8be6\u7ec6\u7684\u6bd4\u8f83\u8f93\u51fa\uff0c\u6307\u51fa\u5217\u8868\u4e2d\u54ea\u4e2a\u5143\u7d20\u4e0d\u5339\u914d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6d6e\u70b9\u6570\u6bd4\u8f83<\/h4>\n\n\n\n<p>\u7531\u4e8e\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528 <code>pytest.approx<\/code> \u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_float(): assert 0.1 + 0.2 == pytest.approx(0.3)<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u65ad\u8a00\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u5176\u5b9a\u4e49\u3001\u7528\u6cd5\u3001\u4ee5\u53ca\u5982\u4f55\u5728 pytest \u4e2d\u8fdb\u884c\u66f4\u590d\u6742\u7684\u65ad\u8a00\u3002\u8fd9\u5c06\u5e2e\u52a9\u521d\u5b66\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u65ad\u8a00\u6765\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u5065\u58ee\u6027\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u4f7f\u7528Fixtures\u7684\u4ec0\u4e48\u662fFixturesChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u4ec0\u4e48\u662f Fixtures\uff08\u5939\u5177\uff09&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u4f7f\u7528 Fixtures&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5f15\u8a00\u6216\u57fa\u7840\u6982\u5ff5\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ec0\u4e48\u662f Fixtures\uff08\u5939\u5177\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u5728\u8f6f\u4ef6\u6d4b\u8bd5\u4e2d\uff0c\u5939\u5177\uff08Fixtures\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u7a33\u5b9a\u7684\u6d4b\u8bd5\u73af\u5883\u7684\u673a\u5236\u3002\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u521b\u5efa\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u6216\u8005\u6267\u884c\u4efb\u4f55\u5176\u4ed6\u8bbe\u7f6e\u6216\u6e05\u7406\u4efb\u52a1\u3002\u5728 pytest \u4e2d\uff0c\u5939\u5177\u662f\u4e00\u79cd\u53ef\u91cd\u7528\u7684\u6d4b\u8bd5\u8d44\u6e90\uff0c\u53ef\u4ee5\u5728\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u6216\u6d4b\u8bd5\u6a21\u5757\u4e4b\u95f4\u5171\u4eab\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u9700\u8981\u5939\u5177<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4ee3\u7801\u91cd\u7528<\/strong>: \u901a\u8fc7\u4f7f\u7528\u5939\u5177\uff0c\u4f60\u53ef\u4ee5\u907f\u514d\u5728\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u4e2d\u91cd\u590d\u76f8\u540c\u7684\u8bbe\u7f6e\u548c\u6e05\u7406\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u8026<\/strong>: \u5939\u5177\u5141\u8bb8\u4f60\u5c06\u6d4b\u8bd5\u903b\u8f91\u4e0e\u6d4b\u8bd5\u73af\u5883\u7684\u8bbe\u7f6e\u548c\u6e05\u7406\u5206\u79bb\uff0c\u4f7f\u6d4b\u8bd5\u66f4\u6613\u4e8e\u7ef4\u62a4\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u6027<\/strong>: \u4f60\u53ef\u4ee5\u8f7b\u6613\u5730\u66f4\u6539\u5939\u5177\u7684\u5b9e\u73b0\uff0c\u800c\u4e0d\u5f71\u54cd\u4f7f\u7528\u8be5\u5939\u5177\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u5316<\/strong>: \u5939\u5177\u53ef\u4ee5\u88ab\u53c2\u6570\u5316\uff0c\u5141\u8bb8\u76f8\u540c\u7684\u6d4b\u8bd5\u903b\u8f91\u5728\u4e0d\u540c\u7684\u6761\u4ef6\u4e0b\u8fd0\u884c\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u5de5\u4f5c<\/h4>\n\n\n\n<p>\u5728 pytest \u4e2d\uff0c\u5939\u5177\u662f\u4f7f\u7528 <code>@pytest.fixture<\/code> \u88c5\u9970\u5668\u5b9a\u4e49\u7684\u666e\u901a Python \u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u8fd4\u56de\u9700\u8981\u5728\u6d4b\u8bd5\u4e2d\u4f7f\u7528\u7684\u8d44\u6e90\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>import pytest @pytest.fixture def example_data(): return {\"key\": \"value\"}<\/code><\/p>\n\n\n\n<p>\u4e00\u65e6\u5b9a\u4e49\u4e86\u5939\u5177\uff0c\u4f60\u5c31\u53ef\u4ee5\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6d4b\u8bd5\u51fd\u6570\uff0cpytest \u5c06\u81ea\u52a8\u5904\u7406\u5176\u751f\u547d\u5468\u671f\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>def test_example(example_data): assert example_data[\"key\"] == \"value\"<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u751f\u547d\u5468\u671f\u548c\u8303\u56f4<\/h4>\n\n\n\n<p>\u5939\u5177\u6709\u4e0d\u540c\u7684\u751f\u547d\u5468\u671f\u548c\u8303\u56f4\uff0c\u53ef\u4ee5\u662f\u51fd\u6570\u7ea7\u522b\uff08\u6bcf\u4e2a\u6d4b\u8bd5\u51fd\u6570\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\uff09\u3001\u6a21\u5757\u7ea7\u522b\uff08\u6bcf\u4e2a\u6a21\u5757\u4e00\u4e2a\u5b9e\u4f8b\uff09\u6216\u4f1a\u8bdd\u7ea7\u522b\uff08\u6574\u4e2a\u6d4b\u8bd5\u4f1a\u8bdd\u4e00\u4e2a\u5b9e\u4f8b\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7ec4\u5408\u548c\u4f9d\u8d56<\/h4>\n\n\n\n<p>\u5939\u5177\u53ef\u4ee5\u4f9d\u8d56\u4e8e\u5176\u4ed6\u5939\u5177\uff0c\u5141\u8bb8\u4f60\u521b\u5efa\u590d\u6742\u7684\u6d4b\u8bd5\u73af\u5883\uff0c\u540c\u65f6\u4fdd\u6301\u4ee3\u7801\u7684\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5168\u9762\u800c\u8be6\u7ec6\u7684\u89e3\u91ca\uff0c\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u5939\u5177\uff0c\u4e3a\u4ec0\u4e48\u9700\u8981\u5b83\u4eec\uff0c\u4ee5\u53ca\u5b83\u4eec\u5728 pytest \u4e2d\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u4f7f\u7528\u5939\u5177\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u4f7f\u7528Fixtures\u7684\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528FixturesChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528 Fixtures\uff08\u5939\u5177\uff09&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u4f7f\u7528 Fixtures&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u6838\u5fc3\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528 Fixtures\uff08\u5939\u5177\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u521b\u5efa Fixtures<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49\u51fd\u6570<\/strong>: \u521b\u5efa\u4e00\u4e2a\u666e\u901a\u7684 Python \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u4f60\u60f3\u5728\u6d4b\u8bd5\u4e2d\u4f7f\u7528\u7684\u8d44\u6e90\u6216\u8bbe\u7f6e\u3002<\/li>\n\n\n\n<li><strong>\u6dfb\u52a0\u88c5\u9970\u5668<\/strong>: \u4f7f\u7528 <code>@pytest.fixture<\/code> \u88c5\u9970\u5668\u6765\u6807\u8bb0\u8fd9\u4e2a\u51fd\u6570\u4e3a\u4e00\u4e2a\u5939\u5177\u3002<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u8d44\u6e90<\/strong>: \u5728\u5939\u5177\u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u7528 <code>return<\/code> \u8bed\u53e5\u8fd4\u56de\u6240\u9700\u7684\u8d44\u6e90\u3002<\/li>\n<\/ol>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code># \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5939\u5177 import pytest @pytest.fixture def example_data(): return {\"key\": \"value\"}<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 Fixtures<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f5c\u4e3a\u53c2\u6570<\/strong>: \u5c06\u5939\u5177\u51fd\u6570\u7684\u540d\u79f0\u4f5c\u4e3a\u53c2\u6570\u6dfb\u52a0\u5230\u4f60\u7684\u6d4b\u8bd5\u51fd\u6570\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u8bbf\u95ee\u8d44\u6e90<\/strong>: \u5728\u6d4b\u8bd5\u51fd\u6570\u5185\u90e8\uff0c\u4f60\u53ef\u4ee5\u50cf\u4f7f\u7528\u666e\u901a Python \u53d8\u91cf\u4e00\u6837\u4f7f\u7528\u8fd9\u4e2a\u5939\u5177\u3002<\/li>\n<\/ol>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code># \u4f7f\u7528\u5939\u5177 def test_example(example_data): assert example_data[\"key\"] == \"value\"<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5939\u5177\u7684\u8303\u56f4\uff08Scope\uff09<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>scope<\/code> \u53c2\u6570\u6765\u5b9a\u4e49\u5939\u5177\u7684\u8303\u56f4\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u51fd\u6570\u7ea7\u522b\uff08\u9ed8\u8ba4\uff09<\/strong>: \u5bf9\u6bcf\u4e2a\u6d4b\u8bd5\u51fd\u6570\u91cd\u65b0\u6267\u884c\u5939\u5177\u3002<\/li>\n\n\n\n<li><strong>\u6a21\u5757\u7ea7\u522b<\/strong>: \u5728\u6574\u4e2a\u6a21\u5757\u4e2d\u53ea\u6267\u884c\u4e00\u6b21\u3002<\/li>\n\n\n\n<li><strong>\u4f1a\u8bdd\u7ea7\u522b<\/strong>: \u5728\u6574\u4e2a\u6d4b\u8bd5\u4f1a\u8bdd\u4e2d\u53ea\u6267\u884c\u4e00\u6b21\u3002<\/li>\n<\/ul>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code># \u8bbe\u7f6e\u5939\u5177\u8303\u56f4\u4e3a\u6a21\u5757\u7ea7\u522b @pytest.fixture(scope=\"module\") def example_data(): return {\"key\": \"value\"}<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u6570\u5316 Fixtures<\/h4>\n\n\n\n<p>\u4f7f\u7528 <code>params<\/code> \u53c2\u6570\u53ef\u4ee5\u521b\u5efa\u53c2\u6570\u5316\u7684\u5939\u5177\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code># \u53c2\u6570\u5316\u5939\u5177 @pytest.fixture(params=[1, 2, 3]) def example_data(request): return request.param<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5939\u5177\u4f9d\u8d56<\/h4>\n\n\n\n<p>\u4e00\u4e2a\u5939\u5177\u53ef\u4ee5\u4f9d\u8d56\u4e8e\u5176\u4ed6\u5939\u5177\uff0c\u8fd9\u6837\u4f60\u53ef\u4ee5\u6784\u5efa\u66f4\u590d\u6742\u7684\u6d4b\u8bd5\u73af\u5883\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code># \u5939\u5177\u4f9d\u8d56 @pytest.fixture def more_data(example_data): example_data[\"more_key\"] = \"more_value\" return example_data<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6e05\u7406\u8d44\u6e90<\/h4>\n\n\n\n<p>\u4f7f\u7528 <code>yield<\/code> \u8bed\u53e5\u5728\u5939\u5177\u4e2d\u8fdb\u884c\u8d44\u6e90\u6e05\u7406\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code># \u4f7f\u7528 yield \u8fdb\u884c\u6e05\u7406 @pytest.fixture def database_connection(): conn = create_connection() yield conn conn.close()<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528 pytest \u4e2d\u7684\u5939\u5177\uff0c\u5305\u62ec\u5982\u4f55\u5b9a\u4e49\u3001\u8c03\u7528\u3001\u53c2\u6570\u5316\u548c\u7ba1\u7406\u5939\u5177\u7684\u751f\u547d\u5468\u671f\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u4f7f\u7528\u5939\u5177\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u4f7f\u7528Fixtures\u7684Fixtures\u7684\u4f5c\u7528\u57dfChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;Fixtures \u7684\u4f5c\u7528\u57df\uff08Scope\uff09&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u4f7f\u7528 Fixtures&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u91cd\u8981\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Fixtures \u7684\u4f5c\u7528\u57df\uff08Scope\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u5728 pytest \u4e2d\uff0c\u5939\u5177\uff08Fixtures\uff09\u7684\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86\u5939\u5177\u5b9e\u4f8b\u7684\u751f\u547d\u5468\u671f\uff0c\u5373\u5b83\u4eec\u4f55\u65f6\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u3002\u4f5c\u7528\u57df\u5f71\u54cd\u4e86\u5939\u5177\u7684\u91cd\u7528\u6027\u548c\u6d4b\u8bd5\u6267\u884c\u7684\u901f\u5ea6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7c7b\u578b<\/h4>\n\n\n\n<p>pytest \u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u7c7b\u578b\u7684\u4f5c\u7528\u57df\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u51fd\u6570\u7ea7\u522b\uff08<code>function<\/code>\uff09<\/strong>: \u9ed8\u8ba4\u7684\u4f5c\u7528\u57df\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u4f9d\u8d56\u8be5\u5939\u5177\u7684\u6d4b\u8bd5\u51fd\u6570\uff0c\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5939\u5177\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u6a21\u5757\u7ea7\u522b\uff08<code>module<\/code>\uff09<\/strong>: \u5728\u6574\u4e2a Python \u6a21\u5757\uff08\u4e00\u4e2a <code>.py<\/code> \u6587\u4ef6\uff09\u5185\uff0c\u5939\u5177\u53ea\u4f1a\u88ab\u521b\u5efa\u4e00\u6b21\u3002<\/li>\n\n\n\n<li><strong>\u7c7b\u7ea7\u522b\uff08<code>class<\/code>\uff09<\/strong>: \u5728\u6574\u4e2a\u6d4b\u8bd5\u7c7b\u5185\uff0c\u5939\u5177\u53ea\u4f1a\u88ab\u521b\u5efa\u4e00\u6b21\u3002<\/li>\n\n\n\n<li><strong>\u4f1a\u8bdd\u7ea7\u522b\uff08<code>session<\/code>\uff09<\/strong>: \u5728\u6574\u4e2a\u6d4b\u8bd5\u4f1a\u8bdd\uff08\u5373\u6240\u6709\u6d4b\u8bd5\uff09\u4e2d\uff0c\u5939\u5177\u53ea\u4f1a\u88ab\u521b\u5efa\u4e00\u6b21\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u8bbe\u7f6e\u4f5c\u7528\u57df<\/h4>\n\n\n\n<p>\u4f7f\u7528 <code>scope<\/code> \u53c2\u6570\u5728 <code>@pytest.fixture<\/code> \u88c5\u9970\u5668\u4e2d\u8bbe\u7f6e\u4f5c\u7528\u57df\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code># \u8bbe\u7f6e\u6a21\u5757\u7ea7\u522b\u7684\u4f5c\u7528\u57df @pytest.fixture(scope=\"module\") def example_data(): return {\"key\": \"value\"}<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u4f5c\u7528\u57df\u91cd\u8981<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6027\u80fd<\/strong>: \u66f4\u9ad8\u7ea7\u522b\u7684\u4f5c\u7528\u57df\uff08\u5982\u4f1a\u8bdd\u7ea7\u522b\uff09\u53ef\u80fd\u4f1a\u51cf\u5c11\u8bbe\u7f6e\u548c\u6e05\u7406\u7684\u6b21\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u6d4b\u8bd5\u901f\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u9694\u79bb<\/strong>: \u8f83\u4f4e\u7ea7\u522b\u7684\u4f5c\u7528\u57df\uff08\u5982\u51fd\u6570\u7ea7\u522b\uff09\u63d0\u4f9b\u66f4\u597d\u7684\u6d4b\u8bd5\u9694\u79bb\uff0c\u4f46\u53ef\u80fd\u4f1a\u964d\u4f4e\u6027\u80fd\u3002<\/li>\n\n\n\n<li><strong>\u8d44\u6e90\u7ba1\u7406<\/strong>: \u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u4f5c\u7528\u57df\uff0c\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6587\u4ef6\u6216\u5176\u4ed6\u8d44\u6e90\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f9d\u8d56\u5173\u7cfb<\/strong>: \u5982\u679c\u4e00\u4e2a\u9ad8\u4f5c\u7528\u57df\u7684\u5939\u5177\u4f9d\u8d56\u4e8e\u4e00\u4e2a\u4f4e\u4f5c\u7528\u57df\u7684\u5939\u5177\uff0c\u90a3\u4e48\u4f4e\u4f5c\u7528\u57df\u7684\u5939\u5177\u5c06\u5728\u9ad8\u4f5c\u7528\u57df\u7684\u5939\u5177\u6bcf\u6b21\u8c03\u7528\u65f6\u91cd\u65b0\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u6e05\u7406<\/strong>: \u5bf9\u4e8e\u6a21\u5757\u7ea7\u522b\u548c\u4f1a\u8bdd\u7ea7\u522b\u7684\u5939\u5177\uff0c\u52a1\u5fc5\u786e\u4fdd\u5728\u5939\u5177\u51fd\u6570\u4e2d\u8fdb\u884c\u9002\u5f53\u7684\u8d44\u6e90\u6e05\u7406\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86 pytest \u4e2d\u5173\u4e8e\u5939\u5177\u4f5c\u7528\u57df\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u5176\u5b9a\u4e49\u3001\u7c7b\u578b\u3001\u5982\u4f55\u8bbe\u7f6e\uff0c\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u5b83\u4eec\u662f\u91cd\u8981\u7684\u3002\u8fd9\u5c06\u5e2e\u52a9\u521d\u5b66\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528\u4f5c\u7528\u57df\u6765\u7ba1\u7406\u5939\u5177\u7684\u751f\u547d\u5468\u671f\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u53c2\u6570\u5316\u6d4b\u8bd5\u7684\u4ec0\u4e48\u662f\u53c2\u6570\u5316\u6d4b\u8bd5ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u4ec0\u4e48\u662f\u53c2\u6570\u5316\u6d4b\u8bd5\uff08Parameterized Testing\uff09&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u53c2\u6570\u5316\u6d4b\u8bd5&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5f15\u8a00\u6216\u57fa\u7840\u6982\u5ff5\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u53c2\u6570\u5316\u6d4b\u8bd5\uff08Parameterized Testing\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u53c2\u6570\u5316\u6d4b\u8bd5\u662f\u4e00\u79cd\u8f6f\u4ef6\u6d4b\u8bd5\u6280\u672f\uff0c\u7528\u4e8e\u4ee5\u7cfb\u7edf\u6027\u7684\u65b9\u5f0f\u4f7f\u7528\u4e0d\u540c\u7684\u8f93\u5165\u6570\u636e\u6765\u6267\u884c\u76f8\u540c\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002\u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\u68c0\u67e5\u88ab\u6d4b\u8bd5\u4ee3\u7801\u5728\u4e0d\u540c\u6761\u4ef6\u4e0b\u7684\u884c\u4e3a\u3002\u5728 pytest \u4e2d\uff0c\u53c2\u6570\u5316\u6d4b\u8bd5\u53ef\u4ee5\u7528\u4e8e\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u53d8\u4f53\u7684\u5355\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u9700\u8981\u53c2\u6570\u5316\u6d4b\u8bd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4ee3\u7801\u91cd\u7528<\/strong>: \u901a\u8fc7\u53c2\u6570\u5316\uff0c\u4f60\u53ef\u4ee5\u907f\u514d\u7f16\u5199\u51e0\u4e4e\u76f8\u540c\u7684\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff0c\u4ece\u800c\u51cf\u5c11\u4ee3\u7801\u91cd\u590d\u3002<\/li>\n\n\n\n<li><strong>\u5168\u9762\u6027<\/strong>: \u53c2\u6570\u5316\u5141\u8bb8\u4f60\u7528\u591a\u7ec4\u6570\u636e\u5168\u9762\u5730\u6d4b\u8bd5\u4e00\u4e2a\u529f\u80fd\uff0c\u786e\u4fdd\u5176\u5728\u5404\u79cd\u6761\u4ef6\u4e0b\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u6613\u4e8e\u7ef4\u62a4<\/strong>: \u5982\u679c\u6d4b\u8bd5\u903b\u8f91\u9700\u8981\u66f4\u6539\uff0c\u4f60\u53ea\u9700\u8981\u5728\u4e00\u4e2a\u5730\u65b9\u8fdb\u884c\u66f4\u6539\uff0c\u800c\u4e0d\u662f\u66f4\u65b0\u591a\u4e2a\u5355\u72ec\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u9ad8\u8986\u76d6\u7387<\/strong>: \u4f7f\u7528\u4e0d\u540c\u7684\u6d4b\u8bd5\u6570\u636e\u53ef\u4ee5\u66f4\u5168\u9762\u5730\u8986\u76d6\u4ee3\u7801\uff0c\u63d0\u9ad8\u6d4b\u8bd5\u7684\u8d28\u91cf\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u5b9e\u73b0<\/h4>\n\n\n\n<p>\u5728 pytest \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>@pytest.mark.parametrize<\/code> \u88c5\u9970\u5668\u6765\u5b9e\u73b0\u53c2\u6570\u5316\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>import pytest @pytest.mark.parametrize(\"input,expected\", [(5, 25), (2, 4), (3, 9)]) def test_squares(input, expected): assert input ** 2 == expected<\/code><\/p>\n\n\n\n<p>\u8fd9\u91cc\uff0c<code>input<\/code> \u548c <code>expected<\/code> \u662f\u6d4b\u8bd5\u51fd\u6570 <code>test_squares<\/code> \u7684\u53c2\u6570\uff0c\u800c <code>[(5, 25), (2, 4), (3, 9)]<\/code> \u662f\u53c2\u6570\u7684\u503c\u3002\u8fd9\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u5c06\u4f1a\u8fd0\u884c\u4e09\u6b21\uff0c\u6bcf\u6b21\u4f7f\u7528\u4e00\u7ec4\u4e0d\u540c\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u6570\u5316\u5939\u5177<\/h4>\n\n\n\n<p>\u9664\u4e86\u6d4b\u8bd5\u51fd\u6570\uff0c\u4f60\u8fd8\u53ef\u4ee5\u53c2\u6570\u5316\u5939\u5177\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u7684\u6d4b\u8bd5\u73af\u5883\u4e2d\u8fd0\u884c\u76f8\u540c\u7684\u6d4b\u8bd5\u903b\u8f91\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.fixture(params=[1, 2, 3]) def example_data(request): return request.param<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5168\u9762\u800c\u8be6\u7ec6\u7684\u89e3\u91ca\uff0c\u4ecb\u7ecd\u4e86\u4ec0\u4e48\u662f\u53c2\u6570\u5316\u6d4b\u8bd5\uff0c\u4e3a\u4ec0\u4e48\u9700\u8981\u5b83\uff0c\u4ee5\u53ca\u5982\u4f55\u5728 pytest \u4e2d\u5b9e\u73b0\u5b83\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u4f7f\u7528\u53c2\u6570\u5316\u6d4b\u8bd5\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u53c2\u6570\u5316\u6d4b\u8bd5\u7684\u5982\u4f55\u8fdb\u884c\u53c2\u6570\u5316\u6d4b\u8bd5ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u5982\u4f55\u8fdb\u884c\u53c2\u6570\u5316\u6d4b\u8bd5&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u53c2\u6570\u5316\u6d4b\u8bd5&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u6838\u5fc3\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u8fdb\u884c\u53c2\u6570\u5316\u6d4b\u8bd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>@pytest.mark.parametrize<\/code> \u88c5\u9970\u5668<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5bfc\u5165 pytest<\/strong>: \u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5bfc\u5165\u4e86 pytest \u6a21\u5757\u3002pythonCopy code<code>import pytest<\/code><\/li>\n\n\n\n<li><strong>\u5b9a\u4e49\u6d4b\u8bd5\u51fd\u6570<\/strong>: \u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u51fd\u6570\uff0c\u5e76\u4e3a\u5176\u5b9a\u4e49\u4e00\u4e9b\u53c2\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u5c06\u7528\u4e8e\u63a5\u6536\u6d4b\u8bd5\u6570\u636e\u3002pythonCopy code<code>def test_multiply(a, b, expected): ...<\/code><\/li>\n\n\n\n<li><strong>\u6dfb\u52a0\u88c5\u9970\u5668<\/strong>: \u4f7f\u7528 <code>@pytest.mark.parametrize<\/code> \u88c5\u9970\u5668\uff0c\u5e76\u4e3a\u5176\u63d0\u4f9b\u4e24\u4e2a\u4e3b\u8981\u53c2\u6570\uff1a\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u6d4b\u8bd5\u51fd\u6570\u7684\u53c2\u6570\u540d\uff0c\u53e6\u4e00\u4e2a\u662f\u503c\u5217\u8868\u3002pythonCopy code<code>@pytest.mark.parametrize(\"a, b, expected\", [(1, 2, 2), (2, 3, 6), (3, 5, 15)]) def test_multiply(a, b, expected): assert a * b == expected <\/code>\u8fd9\u91cc\uff0c\u6bcf\u4e2a\u5143\u7ec4 <code>(a, b, expected)<\/code> \u90fd\u4ee3\u8868\u4e00\u7ec4\u6d4b\u8bd5\u6570\u636e\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u6570\u5316\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b<\/h4>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u5728\u4e00\u4e2a\u6a21\u5757\u6216\u7c7b\u4e2d\u53c2\u6570\u5316\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.mark.parametrize(\"a, b, expected\", [(1, 2, 2), (2, 3, 6)]) class TestMultiply: def test_multiply_positive(self, a, b, expected): assert a * b == expected def test_multiply_properties(self, a, b, expected): assert a * b == b * a<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u6570\u5316\u5939\u5177\uff08Fixtures\uff09<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49\u5939\u5177<\/strong>: \u521b\u5efa\u4e00\u4e2a\u4f7f\u7528 <code>@pytest.fixture<\/code> \u88c5\u9970\u5668\u7684\u5939\u5177\u3002pythonCopy code<code>@pytest.fixture def example_data(): return 42<\/code><\/li>\n\n\n\n<li><strong>\u53c2\u6570\u5316\u5939\u5177<\/strong>: \u5728\u5939\u5177\u5b9a\u4e49\u4e2d\u4f7f\u7528 <code>params<\/code> \u53c2\u6570\u3002pythonCopy code<code>@pytest.fixture(params=[1, 2, 3]) def example_data(request): return request.param<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u53c2\u6570\u5316\u5939\u5177<\/strong>: \u5728\u6d4b\u8bd5\u51fd\u6570\u4e2d\uff0c\u50cf\u4f7f\u7528\u666e\u901a\u5939\u5177\u4e00\u6837\u4f7f\u7528\u53c2\u6570\u5316\u5939\u5177\u3002pythonCopy code<code>def test_example(example_data): assert example_data in [1, 2, 3]<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7ec4\u5408\u53c2\u6570<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a <code>@pytest.mark.parametrize<\/code> \u88c5\u9970\u5668\u6765\u7ec4\u5408\u591a\u4e2a\u53c2\u6570\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.mark.parametrize(\"a\", [1, 2]) @pytest.mark.parametrize(\"b\", [3, 4]) def test_combination(a, b): assert a &lt; b<\/code><\/p>\n\n\n\n<p>\u8fd9\u5c06\u751f\u6210 4 \u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff1a<code>(1, 3), (1, 4), (2, 3), (2, 4)<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u8fdb\u884c\u53c2\u6570\u5316\u6d4b\u8bd5\uff0c\u5305\u62ec\u4f7f\u7528 <code>@pytest.mark.parametrize<\/code> \u88c5\u9970\u5668\uff0c\u53c2\u6570\u5316\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff0c\u4ee5\u53ca\u5982\u4f55\u53c2\u6570\u5316\u548c\u7ec4\u5408\u591a\u4e2a\u53c2\u6570\u548c\u5939\u5177\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u4f7f\u7528\u53c2\u6570\u5316\u6d4b\u8bd5\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u53c2\u6570\u5316\u6d4b\u8bd5\u7684\u53c2\u6570\u5316FixturesChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u53c2\u6570\u5316 Fixtures\uff08\u5939\u5177\uff09&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u53c2\u6570\u5316\u6d4b\u8bd5&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u4e13\u95e8\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u6570\u5316 Fixtures\uff08\u5939\u5177\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u5728 pytest \u4e2d\uff0c\u9664\u4e86\u53ef\u4ee5\u53c2\u6570\u5316\u6d4b\u8bd5\u51fd\u6570\uff0c\u4f60\u8fd8\u53ef\u4ee5\u53c2\u6570\u5316\u5939\u5177\u3002\u53c2\u6570\u5316\u5939\u5177\u5141\u8bb8\u4f60\u5728\u4e0d\u540c\u7684\u6d4b\u8bd5\u73af\u5883\u6216\u6761\u4ef6\u4e0b\u8fd0\u884c\u76f8\u540c\u7684\u6d4b\u8bd5\u903b\u8f91\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u5b9e\u73b0\u53c2\u6570\u5316\u5939\u5177<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49\u5939\u5177<\/strong>: \u4f7f\u7528 <code>@pytest.fixture<\/code> \u88c5\u9970\u5668\u6765\u5b9a\u4e49\u4e00\u4e2a\u5939\u5177\u3002pythonCopy code<code>@pytest.fixture def example_data(): return {\"key\": \"value\"}<\/code><\/li>\n\n\n\n<li><strong>\u6dfb\u52a0\u53c2\u6570<\/strong>: \u5728\u5939\u5177\u5b9a\u4e49\u4e2d\u4f7f\u7528 <code>params<\/code> \u53c2\u6570\u6765\u6dfb\u52a0\u591a\u7ec4\u6d4b\u8bd5\u6570\u636e\u3002pythonCopy code<code>@pytest.fixture(params=[1, 2, 3]) def example_data(request): return request.param <\/code>\u5728\u8fd9\u91cc\uff0c<code>params=[1, 2, 3]<\/code> \u5b9a\u4e49\u4e86\u4e09\u7ec4\u4e0d\u540c\u7684\u6d4b\u8bd5\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u8bbf\u95ee\u53c2\u6570<\/strong>: \u4f7f\u7528 <code>request.param<\/code> \u6765\u8bbf\u95ee\u5f53\u524d\u53c2\u6570\u503c\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u4f7f\u7528\u53c2\u6570\u5316\u5939\u5177<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5728\u6d4b\u8bd5\u51fd\u6570\u4e2d\u4f7f\u7528<\/strong>: \u5c06\u53c2\u6570\u5316\u5939\u5177\u7684\u540d\u79f0\u4f5c\u4e3a\u6d4b\u8bd5\u51fd\u6570\u7684\u53c2\u6570\u3002pythonCopy code<code>def test_example(example_data): assert example_data in [1, 2, 3]<\/code><\/li>\n\n\n\n<li><strong>\u7ec4\u5408\u4f7f\u7528<\/strong>: \u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u6d4b\u8bd5\u51fd\u6570\u4e2d\u4f7f\u7528\u591a\u4e2a\u53c2\u6570\u5316\u5939\u5177\u3002pythonCopy code<code>@pytest.fixture(params=[4, 5, 6]) def more_data(request): return request.param def test_combination(example_data, more_data): assert example_data + more_data in [5, 6, 7, 6, 7, 8, 7, 8, 9]<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528\u57df<\/strong>: \u53c2\u6570\u5316\u5939\u5177\u7684\u4f5c\u7528\u57df\u5e94\u8be5\u4e0e\u4f7f\u7528\u5b83\u4eec\u7684\u6d4b\u8bd5\u51fd\u6570\u6216\u5176\u4ed6\u5939\u5177\u7684\u4f5c\u7528\u57df\u517c\u5bb9\u3002<\/li>\n\n\n\n<li><strong>\u4f9d\u8d56\u5173\u7cfb<\/strong>: \u53c2\u6570\u5316\u5939\u5177\u53ef\u4ee5\u4f9d\u8d56\u4e8e\u5176\u4ed6\u5939\u5177\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u4f5c\u7528\u57df\u548c\u53c2\u6570\u7684\u7ec4\u5408\u3002<\/li>\n\n\n\n<li><strong>\u8d44\u6e90\u6e05\u7406<\/strong>: \u5982\u679c\u4f60\u7684\u5939\u5177\u9700\u8981\u8fdb\u884c\u8d44\u6e90\u5206\u914d\u548c\u6e05\u7406\uff0c\u786e\u4fdd\u8fd9\u4e9b\u64cd\u4f5c\u4e0e\u53c2\u6570\u5316\u662f\u517c\u5bb9\u7684\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5728 pytest \u4e2d\u5982\u4f55\u5b9e\u73b0\u548c\u4f7f\u7528\u53c2\u6570\u5316\u5939\u5177\uff0c\u5305\u62ec\u5b9a\u4e49\u3001\u6dfb\u52a0\u53c2\u6570\u3001\u8bbf\u95ee\u53c2\u6570\u548c\u7ec4\u5408\u4f7f\u7528\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u4f7f\u7528\u53c2\u6570\u5316\u5939\u5177\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u6807\u8bb0\u4e0e\u6807\u7b7e\u7684\u4f7f\u7528pytest.mark\u8fdb\u884c\u6807\u8bb0ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u4f7f\u7528 <code>pytest.mark<\/code> \u8fdb\u884c\u6807\u8bb0&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u6807\u8bb0\u4e0e\u6807\u7b7e&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u6838\u5fc3\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 <code>pytest.mark<\/code> \u8fdb\u884c\u6807\u8bb0<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u5728 pytest \u4e2d\uff0c<code>pytest.mark<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e\u7ed9\u6d4b\u8bd5\u7528\u4f8b\u6dfb\u52a0\u5143\u6570\u636e\u6807\u8bb0\uff08\u6216\u6807\u7b7e\uff09\u7684\u673a\u5236\u3002\u8fd9\u4e9b\u6807\u8bb0\u53ef\u4ee5\u7528\u4e8e\u8fc7\u6ee4\u3001\u5206\u7c7b\u6216\u6267\u884c\u7279\u5b9a\u7684\u6d4b\u8bd5\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u6dfb\u52a0\u6807\u8bb0<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5355\u4e2a\u6807\u8bb0<\/strong>: \u4f7f\u7528 <code>@pytest.mark.MARK_NAME<\/code> \u88c5\u9970\u5668\u6765\u7ed9\u6d4b\u8bd5\u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u6807\u8bb0\u3002pythonCopy code<code>@pytest.mark.slow def test_slow_function(): pass<\/code><\/li>\n\n\n\n<li><strong>\u591a\u4e2a\u6807\u8bb0<\/strong>: \u4f60\u53ef\u4ee5\u7ed9\u4e00\u4e2a\u6d4b\u8bd5\u51fd\u6570\u6dfb\u52a0\u591a\u4e2a\u6807\u8bb0\u3002pythonCopy code<code>@pytest.mark.slow @pytest.mark.regression def test_slow_and_regression(): pass<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u4f7f\u7528\u6807\u8bb0<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8fd0\u884c\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5<\/strong>: \u4f7f\u7528 <code>-m<\/code> \u9009\u9879\u6765\u8fd0\u884c\u5e26\u6709\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5\u3002bashCopy code<code>pytest -m slow <\/code>\u8fd9\u5c06\u53ea\u8fd0\u884c\u5e26\u6709 <code>@pytest.mark.slow<\/code> \u6807\u8bb0\u7684\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><strong>\u6392\u9664\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5<\/strong>: \u4f7f\u7528 <code>-m \"not MARK_NAME\"<\/code> \u6765\u6392\u9664\u5e26\u6709\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5\u3002bashCopy code<code>pytest -m \"not slow\"<\/code><\/li>\n\n\n\n<li><strong>\u7ec4\u5408\u6807\u8bb0<\/strong>: \u4f7f\u7528\u903b\u8f91\u64cd\u4f5c\u7b26\uff08\u5982 <code>and<\/code>, <code>or<\/code>, <code>not<\/code>\uff09\u6765\u7ec4\u5408\u591a\u4e2a\u6807\u8bb0\u3002bashCopy code<code>pytest -m \"slow and regression\"<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u6807\u8bb0<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6ce8\u518c\u81ea\u5b9a\u4e49\u6807\u8bb0<\/strong>: \u5728 <code>pytest.ini<\/code> \u6216 <code>pyproject.toml<\/code> \u6587\u4ef6\u4e2d\u6ce8\u518c\u81ea\u5b9a\u4e49\u6807\u8bb0\uff0c\u4ee5\u907f\u514d pytest \u7684\u672a\u6ce8\u518c\u6807\u8bb0\u8b66\u544a\u3002iniCopy code<code># pytest.ini [pytest] markers = slow: marks tests as slow (deselect with '-m \"not slow\"')<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6807\u8bb0<\/strong>: \u6ce8\u518c\u540e\uff0c\u4f60\u53ef\u4ee5\u50cf\u4f7f\u7528\u5185\u7f6e\u6807\u8bb0\u4e00\u6837\u4f7f\u7528\u81ea\u5b9a\u4e49\u6807\u8bb0\u3002pythonCopy code<code>@pytest.mark.slow def test_custom_marker(): pass<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u6570\u5316\u6807\u8bb0<\/h4>\n\n\n\n<p>\u4f60\u8fd8\u53ef\u4ee5\u7ed9\u6807\u8bb0\u6dfb\u52a0\u53c2\u6570\uff0c\u4ee5\u4fbf\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6d4b\u8bd5\u903b\u8f91\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>@pytest.mark.parametrize(\"input,expected\", [(3, 9), (4, 16)], ids=[\"3^2\", \"4^2\"]) def test_squares(input, expected): assert input ** 2 == expected<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u5728 pytest \u4e2d\u4f7f\u7528 <code>pytest.mark<\/code> \u8fdb\u884c\u6807\u8bb0\uff0c\u5305\u62ec\u6dfb\u52a0\u3001\u4f7f\u7528\u3001\u81ea\u5b9a\u4e49\u548c\u53c2\u6570\u5316\u6807\u8bb0\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u4f7f\u7528\u6807\u8bb0\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u6807\u8bb0\u4e0e\u6807\u7b7e\u7684\u81ea\u5b9a\u4e49\u6807\u8bb0ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u81ea\u5b9a\u4e49\u6807\u8bb0&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u6807\u8bb0\u4e0e\u6807\u7b7e&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u4e13\u95e8\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u6807\u8bb0<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u81ea\u5b9a\u4e49\u6807\u8bb0\u5141\u8bb8\u4f60\u521b\u5efa\u7279\u5b9a\u4e8e\u4f60\u7684\u6d4b\u8bd5\u9700\u6c42\u7684\u6807\u8bb0\u3002\u8fd9\u4e9b\u6807\u8bb0\u53ef\u4ee5\u7528\u4e8e\u5206\u7c7b\u3001\u8fc7\u6ee4\u6216\u6267\u884c\u7279\u5b9a\u7684\u6d4b\u8bd5\u64cd\u4f5c\uff0c\u5c31\u50cf\u5185\u7f6e\u7684 <code>pytest.mark<\/code> \u6807\u8bb0\u4e00\u6837\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u6ce8\u518c\u81ea\u5b9a\u4e49\u6807\u8bb0<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521b\u5efa\u914d\u7f6e\u6587\u4ef6<\/strong>: \u5982\u679c\u4f60\u8fd8\u6ca1\u6709 <code>pytest.ini<\/code> \u6216 <code>pyproject.toml<\/code> \u6587\u4ef6\uff0c\u8bf7\u521b\u5efa\u4e00\u4e2a\u3002<\/li>\n\n\n\n<li><strong>\u6ce8\u518c\u6807\u8bb0<\/strong>: \u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528 <code>markers<\/code> \u5b57\u6bb5\u6765\u6ce8\u518c\u4f60\u7684\u81ea\u5b9a\u4e49\u6807\u8bb0\u3002<ul><li>\u5728 <code>pytest.ini<\/code> \u6587\u4ef6\u4e2d\uff1a<\/li><\/ul>iniCopy code<code>[pytest] markers = my_marker: description of my custom marker<\/code><ul><li>\u5728 <code>pyproject.toml<\/code> \u6587\u4ef6\u4e2d\uff1a<\/li><\/ul>tomlCopy code<code>[tool.pytest.ini_options] markers = [ \"my_marker: description of my custom marker\", ]<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u4f7f\u7528\u81ea\u5b9a\u4e49\u6807\u8bb0<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6dfb\u52a0\u5230\u6d4b\u8bd5\u51fd\u6570<\/strong>: \u4f7f\u7528 <code>@pytest.mark.MY_MARKER_NAME<\/code> \u88c5\u9970\u5668\u6765\u7ed9\u6d4b\u8bd5\u51fd\u6570\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6807\u8bb0\u3002pythonCopy code<code>@pytest.mark.my_marker def test_with_custom_marker(): pass<\/code><\/li>\n\n\n\n<li><strong>\u8fd0\u884c\u5e26\u6709\u81ea\u5b9a\u4e49\u6807\u8bb0\u7684\u6d4b\u8bd5<\/strong>: \u4f7f\u7528 <code>-m<\/code> \u9009\u9879\u6765\u8fd0\u884c\u5e26\u6709\u81ea\u5b9a\u4e49\u6807\u8bb0\u7684\u6d4b\u8bd5\u3002bashCopy code<code>pytest -m my_marker<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u7ec4\u5408\u81ea\u5b9a\u4e49\u6807\u8bb0<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u64cd\u4f5c\u7b26\uff08\u5982 <code>and<\/code>, <code>or<\/code>, <code>not<\/code>\uff09\u6765\u7ec4\u5408\u81ea\u5b9a\u4e49\u6807\u8bb0\u548c\u5185\u7f6e\u6807\u8bb0\u3002<\/p>\n\n\n\n<p>bashCopy code<\/p>\n\n\n\n<p><code>pytest -m \"my_marker and not slow\"<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u540d\u79f0\u51b2\u7a81<\/strong>: \u786e\u4fdd\u4f60\u7684\u81ea\u5b9a\u4e49\u6807\u8bb0\u540d\u79f0\u4e0d\u4e0e\u5185\u7f6e\u6807\u8bb0\u6216\u5176\u4ed6\u5e93\u7684\u6807\u8bb0\u540d\u79f0\u51b2\u7a81\u3002<\/li>\n\n\n\n<li><strong>\u63cf\u8ff0<\/strong>: \u4e3a\u81ea\u5b9a\u4e49\u6807\u8bb0\u63d0\u4f9b\u63cf\u8ff0\uff0c\u4ee5\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u4e86\u89e3\u5176\u7528\u9014\u3002<\/li>\n\n\n\n<li><strong>\u6587\u6863<\/strong>: \u5728\u9879\u76ee\u7684\u6d4b\u8bd5\u6587\u6863\u4e2d\u5217\u51fa\u6240\u6709\u81ea\u5b9a\u4e49\u6807\u8bb0\u53ca\u5176\u7528\u9014\uff0c\u4ee5\u4fbf\u56e2\u961f\u6210\u5458\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u5728 pytest \u4e2d\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u6807\u8bb0\uff0c\u5305\u62ec\u6ce8\u518c\u3001\u4f7f\u7528\u548c\u7ec4\u5408\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u6807\u8bb0\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u6807\u8bb0\u4e0e\u6807\u7b7e\u7684\u8fd0\u884c\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u8fd0\u884c\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u6807\u8bb0\u4e0e\u6807\u7b7e&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u4e13\u95e8\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fd0\u884c\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u5728 pytest \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u9009\u9879 <code>-m<\/code> \u6765\u8fd0\u884c\u5e26\u6709\u7279\u5b9a\u6807\u8bb0\uff08\u6216\u6807\u7b7e\uff09\u7684\u6d4b\u8bd5\u3002\u8fd9\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u53ea\u8fd0\u884c\u4e0e\u4f60\u5f53\u524d\u5173\u6ce8\u7684\u7279\u5b9a\u529f\u80fd\u6216\u95ee\u9898\u76f8\u5173\u7684\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u8fd0\u884c\u5e26\u6709\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u57fa\u7840\u7528\u6cd5<\/strong>: \u4f7f\u7528 <code>-m<\/code> \u9009\u9879\u540e\u8ddf\u6807\u8bb0\u540d\u79f0\u3002bashCopy code<code>pytest -m slow <\/code>\u8fd9\u5c06\u53ea\u8fd0\u884c\u5e26\u6709 <code>@pytest.mark.slow<\/code> \u6807\u8bb0\u7684\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><strong>\u6392\u9664\u7279\u5b9a\u6807\u8bb0<\/strong>: \u4f7f\u7528 <code>not<\/code> \u5173\u952e\u5b57\u6765\u6392\u9664\u5e26\u6709\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5\u3002bashCopy code<code>pytest -m \"not slow\" <\/code>\u8fd9\u5c06\u8fd0\u884c\u6240\u6709\u6ca1\u6709 <code>@pytest.mark.slow<\/code> \u6807\u8bb0\u7684\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><strong>\u7ec4\u5408\u591a\u4e2a\u6807\u8bb0<\/strong>: \u4f7f\u7528\u903b\u8f91\u64cd\u4f5c\u7b26\uff08\u5982 <code>and<\/code>, <code>or<\/code>\uff09\u6765\u7ec4\u5408\u591a\u4e2a\u6807\u8bb0\u3002bashCopy code<code>pytest -m \"slow and regression\" <\/code>\u8fd9\u5c06\u8fd0\u884c\u540c\u65f6\u5e26\u6709 <code>@pytest.mark.slow<\/code> \u548c <code>@pytest.mark.regression<\/code> \u6807\u8bb0\u7684\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u6216\u8fd0\u7b97\u7b26<\/strong>: \u5982\u679c\u4f60\u60f3\u8fd0\u884c\u5e26\u6709\u4efb\u4e00\u6807\u8bb0\u7684\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>or<\/code> \u8fd0\u7b97\u7b26\u3002bashCopy code<code>pytest -m \"slow or regression\"<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u62ec\u53f7<\/strong>: \u5f53\u4f60\u6709\u590d\u6742\u7684\u6807\u8bb0\u7ec4\u5408\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7\u6765\u660e\u786e\u4f18\u5148\u7ea7\u3002bashCopy code<code>pytest -m \"(slow or regression) and not smoke\"<\/code><\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u6807\u8bb0<\/strong>: \u8fd0\u884c\u5e26\u6709\u81ea\u5b9a\u4e49\u6807\u8bb0\u7684\u6d4b\u8bd5\u4e5f\u662f\u76f8\u540c\u7684\u3002bashCopy code<code>pytest -m my_custom_marker<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6807\u8bb0\u5fc5\u987b\u6ce8\u518c<\/strong>: \u786e\u4fdd\u4f60\u8fd0\u884c\u7684\u6240\u6709\u6807\u8bb0\uff08\u5305\u62ec\u81ea\u5b9a\u4e49\u6807\u8bb0\uff09\u90fd\u5df2\u5728 <code>pytest.ini<\/code> \u6216 <code>pyproject.toml<\/code> \u6587\u4ef6\u4e2d\u6ce8\u518c\u3002<\/li>\n\n\n\n<li><strong>\u5927\u5c0f\u5199\u654f\u611f<\/strong>: \u6807\u8bb0\u540d\u79f0\u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\uff0c\u786e\u4fdd\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528\u6b63\u786e\u7684\u5927\u5c0f\u5199\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u5728 pytest \u4e2d\u8fd0\u884c\u5e26\u6709\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5\uff0c\u5305\u62ec\u57fa\u7840\u7528\u6cd5\u3001\u6392\u9664\u7279\u5b9a\u6807\u8bb0\u3001\u7ec4\u5408\u591a\u4e2a\u6807\u8bb0\u4ee5\u53ca\u9ad8\u7ea7\u7528\u6cd5\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u4f7f\u7528\u6807\u8bb0\u6765\u8fd0\u884c\u7279\u5b9a\u6d4b\u8bd5\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u5904\u7406\u5f02\u5e38\u7684\u4f7f\u7528pytest.raises\u5904\u7406\u5f02\u5e38ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u4f7f\u7528 <code>pytest.raises<\/code> \u5904\u7406\u5f02\u5e38&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u5904\u7406\u5f02\u5e38&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u6838\u5fc3\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 <code>pytest.raises<\/code> \u5904\u7406\u5f02\u5e38<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p><code>pytest.raises<\/code> \u662f\u4e00\u4e2a pytest \u5185\u7f6e\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u6d4b\u8bd5\u5f02\u5e38\u3002\u5b83\u5141\u8bb8\u4f60\u9a8c\u8bc1\u6d4b\u8bd5\u7528\u4f8b\u662f\u5426\u6309\u9884\u671f\u629b\u51fa\u4e86\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6355\u83b7\u5f02\u5e38<\/strong>: \u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u548c <code>pytest.raises<\/code> \u6765\u6355\u83b7\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002pythonCopy code<code>import pytest def test_divide_by_zero(): with pytest.raises(ZeroDivisionError): 1 \/ 0 <\/code>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>1 \/ 0<\/code> \u5e94\u8be5\u629b\u51fa <code>ZeroDivisionError<\/code>\u3002\u5982\u679c\u5b83\u786e\u5b9e\u5982\u6b64\uff0c\u6d4b\u8bd5\u5c06\u901a\u8fc7\u3002<\/li>\n\n\n\n<li><strong>\u68c0\u67e5\u5f02\u5e38\u6d88\u606f<\/strong>: \u4f60\u8fd8\u53ef\u4ee5\u68c0\u67e5\u5f02\u5e38\u7684\u9519\u8bef\u6d88\u606f\u662f\u5426\u7b26\u5408\u9884\u671f\u3002pythonCopy code<code>def test_value_error_message(): with pytest.raises(ValueError, match=r\".* 123 .*\"): raise ValueError(\"Exception 123 occurred\") <\/code>\u5728\u8fd9\u91cc\uff0c<code>match<\/code> \u53c2\u6570\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u68c0\u67e5\u9519\u8bef\u6d88\u606f\u4e2d\u662f\u5426\u5305\u542b &#8220;123&#8221;\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>as<\/code> \u83b7\u53d6\u5f02\u5e38\u4fe1\u606f<\/strong>: \u4f60\u53ef\u4ee5\u4f7f\u7528 <code>as<\/code> \u5173\u952e\u5b57\u6765\u83b7\u53d6\u5b9e\u9645\u629b\u51fa\u7684\u5f02\u5e38\u5bf9\u8c61\u3002pythonCopy code<code>def test_get_exception_info(): with pytest.raises(ValueError) as excinfo: raise ValueError(\"Exception occurred\") assert \"Exception occurred\" in str(excinfo.value) <\/code><code>excinfo<\/code> \u5bf9\u8c61\u5305\u542b\u4e86\u5173\u4e8e\u629b\u51fa\u7684\u5f02\u5e38\u7684\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong>: <code>pytest.raises<\/code> \u4e5f\u9002\u7528\u4e8e\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002pythonCopy code<code>class CustomError(Exception): pass def test_custom_exception(): with pytest.raises(CustomError): raise CustomError(\"This is a custom exception\")<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e0d\u6355\u83b7\u5f02\u5e38<\/strong>: \u5982\u679c\u4ee3\u7801\u6ca1\u6709\u629b\u51fa\u9884\u671f\u7684\u5f02\u5e38\uff0c<code>pytest.raises<\/code> \u4f1a\u4f7f\u6d4b\u8bd5\u5931\u8d25\u3002<\/li>\n\n\n\n<li><strong>\u591a\u4e2a\u5f02\u5e38<\/strong>: <code>pytest.raises<\/code> \u53ea\u80fd\u6355\u83b7\u4e00\u4e2a\u5f02\u5e38\u3002\u5982\u679c\u4f60\u9700\u8981\u6d4b\u8bd5\u591a\u4e2a\u5f02\u5e38\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u591a\u4e2a <code>pytest.raises<\/code> \u5757\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u4f7f\u7528 <code>try\/except<\/code><\/strong>: \u5728\u6d4b\u8bd5\u5f02\u5e38\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528 <code>try\/except<\/code> \u5757\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f1a\u4f7f\u6d4b\u8bd5\u903b\u8f91\u53d8\u5f97\u590d\u6742\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u5728 pytest \u4e2d\u4f7f\u7528 <code>pytest.raises<\/code> \u6765\u5904\u7406\u5f02\u5e38\uff0c\u5305\u62ec\u57fa\u7840\u7528\u6cd5\u3001\u9ad8\u7ea7\u7528\u6cd5\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u5904\u7406\u5f02\u5e38\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u5904\u7406\u5f02\u5e38\u7684\u4f7f\u7528pytest.warns\u5904\u7406\u8b66\u544aChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u4f7f\u7528 <code>pytest.warns<\/code> \u5904\u7406\u8b66\u544a&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u5904\u7406\u5f02\u5e38&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u4e13\u95e8\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 <code>pytest.warns<\/code> \u5904\u7406\u8b66\u544a<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p><code>pytest.warns<\/code> \u662f\u4e00\u4e2a pytest \u5185\u7f6e\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u6d4b\u8bd5\u8b66\u544a\u3002\u5b83\u5141\u8bb8\u4f60\u9a8c\u8bc1\u6d4b\u8bd5\u7528\u4f8b\u662f\u5426\u6309\u9884\u671f\u53d1\u51fa\u4e86\u7279\u5b9a\u7c7b\u578b\u7684\u8b66\u544a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6355\u83b7\u8b66\u544a<\/strong>: \u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u548c <code>pytest.warns<\/code> \u6765\u6355\u83b7\u7279\u5b9a\u7c7b\u578b\u7684\u8b66\u544a\u3002pythonCopy code<code>import pytest import warnings def test_deprecation_warning(): with pytest.warns(DeprecationWarning): warnings.warn(\"deprecated\", DeprecationWarning) <\/code>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>warnings.warn(\"deprecated\", DeprecationWarning)<\/code> \u5e94\u8be5\u53d1\u51fa <code>DeprecationWarning<\/code>\u3002\u5982\u679c\u5b83\u786e\u5b9e\u5982\u6b64\uff0c\u6d4b\u8bd5\u5c06\u901a\u8fc7\u3002<\/li>\n\n\n\n<li><strong>\u68c0\u67e5\u8b66\u544a\u6d88\u606f<\/strong>: \u4f60\u8fd8\u53ef\u4ee5\u68c0\u67e5\u8b66\u544a\u7684\u6d88\u606f\u662f\u5426\u7b26\u5408\u9884\u671f\u3002pythonCopy code<code>def test_specific_warning_message(): with pytest.warns(UserWarning, match=r\".* 123 .*\"): warnings.warn(\"Warning 123 occurred\") <\/code>\u5728\u8fd9\u91cc\uff0c<code>match<\/code> \u53c2\u6570\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u68c0\u67e5\u8b66\u544a\u6d88\u606f\u4e2d\u662f\u5426\u5305\u542b &#8220;123&#8221;\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>as<\/code> \u83b7\u53d6\u8b66\u544a\u4fe1\u606f<\/strong>: \u4f60\u53ef\u4ee5\u4f7f\u7528 <code>as<\/code> \u5173\u952e\u5b57\u6765\u83b7\u53d6\u5b9e\u9645\u53d1\u51fa\u7684\u8b66\u544a\u5bf9\u8c61\u3002pythonCopy code<code>def test_get_warning_info(): with pytest.warns(UserWarning) as record: warnings.warn(\"User warning\") assert \"User warning\" in str(record[0].message) <\/code><code>record<\/code> \u5bf9\u8c61\u5305\u542b\u4e86\u5173\u4e8e\u53d1\u51fa\u7684\u8b66\u544a\u7684\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u591a\u4e2a\u8b66\u544a<\/strong>: \u5982\u679c\u4ee3\u7801\u53ef\u80fd\u53d1\u51fa\u591a\u4e2a\u8b66\u544a\uff0c<code>pytest.warns<\/code> \u4e5f\u80fd\u6355\u83b7\u5b83\u4eec\u3002pythonCopy code<code>def test_multiple_warnings(): with pytest.warns(None) as record: warnings.warn(\"User warning\") warnings.warn(\"Another warning\") assert len(record) == 2 <\/code>\u5728\u8fd9\u91cc\uff0c<code>None<\/code> \u8868\u793a\u6211\u4eec\u4e0d\u5173\u5fc3\u8b66\u544a\u7684\u5177\u4f53\u7c7b\u578b\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e0d\u53d1\u51fa\u8b66\u544a<\/strong>: \u5982\u679c\u4ee3\u7801\u6ca1\u6709\u53d1\u51fa\u9884\u671f\u7684\u8b66\u544a\uff0c<code>pytest.warns<\/code> \u4f1a\u4f7f\u6d4b\u8bd5\u5931\u8d25\u3002<\/li>\n\n\n\n<li><strong>\u8b66\u544a\u7c7b\u578b<\/strong>: \u786e\u4fdd\u4f60\u6307\u5b9a\u4e86\u6b63\u786e\u7684\u8b66\u544a\u7c7b\u578b\u3002\u5982\u679c\u4e0d\u786e\u5b9a\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>None<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u5ffd\u7565\u8b66\u544a<\/strong>: \u5728\u6d4b\u8bd5\u8b66\u544a\u65f6\uff0c\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528 <code>warnings.filterwarnings<\/code>\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5f71\u54cd\u6d4b\u8bd5\u7684\u51c6\u786e\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u5728 pytest \u4e2d\u4f7f\u7528 <code>pytest.warns<\/code> \u6765\u5904\u7406\u8b66\u544a\uff0c\u5305\u62ec\u57fa\u7840\u7528\u6cd5\u3001\u9ad8\u7ea7\u7528\u6cd5\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u5904\u7406\u8b66\u544a\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u9ad8\u7ea7\u4e3b\u9898\u7684\u5e76\u884c\u6d4b\u8bd5ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u5e76\u884c\u6d4b\u8bd5&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u9ad8\u7ea7\u4e3b\u9898&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u6838\u5fc3\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e76\u884c\u6d4b\u8bd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u5e76\u884c\u6d4b\u8bd5\u662f\u4e00\u79cd\u6d4b\u8bd5\u4f18\u5316\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u4f60\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff0c\u4ee5\u51cf\u5c11\u603b\u7684\u6d4b\u8bd5\u6267\u884c\u65f6\u95f4\u3002\u5728 pytest \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u63d2\u4ef6\uff0c\u5982 <code>pytest-xdist<\/code>\uff0c\u6765\u5b9e\u73b0\u5e76\u884c\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b89\u88c5 pytest-xdist<\/h4>\n\n\n\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5 <code>pytest-xdist<\/code> \u63d2\u4ef6\u3002<\/p>\n\n\n\n<p>bashCopy code<\/p>\n\n\n\n<p><code>pip install pytest-xdist<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8fd0\u884c\u5e76\u884c\u6d4b\u8bd5<\/strong>: \u4f7f\u7528 <code>-n<\/code> \u9009\u9879\u540e\u8ddf\u4f60\u60f3\u8981\u4f7f\u7528\u7684 CPU \u6838\u5fc3\u6570\u3002bashCopy code<code>pytest -n 4 <\/code>\u8fd9\u5c06\u4f7f\u7528 4 \u4e2a CPU \u6838\u5fc3\u5e76\u884c\u8fd0\u884c\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u68c0\u6d4b CPU \u6838\u5fc3<\/strong>: \u4f7f\u7528 <code>auto<\/code> \u53c2\u6570\u81ea\u52a8\u68c0\u6d4b\u5e76\u4f7f\u7528\u6240\u6709\u53ef\u7528\u7684 CPU \u6838\u5fc3\u3002bashCopy code<code>pytest -n auto<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5206\u5e03\u5f0f\u6d4b\u8bd5<\/strong>: <code>pytest-xdist<\/code> \u8fd8\u5141\u8bb8\u4f60\u5728\u591a\u53f0\u673a\u5668\u4e0a\u5206\u5e03\u5f0f\u8fd0\u884c\u6d4b\u8bd5\u3002bashCopy code<code>pytest --dist=loadscope<\/code><\/li>\n\n\n\n<li><strong>\u8fd0\u884c\u7279\u5b9a\u6807\u8bb0\u7684\u5e76\u884c\u6d4b\u8bd5<\/strong>: \u4f60\u53ef\u4ee5\u7ed3\u5408 <code>-m<\/code> \u9009\u9879\u6765\u8fd0\u884c\u5e26\u6709\u7279\u5b9a\u6807\u8bb0\u7684\u5e76\u884c\u6d4b\u8bd5\u3002bashCopy code<code>pytest -n 4 -m \"slow\"<\/code><\/li>\n\n\n\n<li><strong>\u8d44\u6e90\u9694\u79bb<\/strong>: \u5728\u5e76\u884c\u6d4b\u8bd5\u4e2d\uff0c\u786e\u4fdd\u6bcf\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u90fd\u662f\u72ec\u7acb\u7684\uff0c\u4e0d\u5171\u4eab\u5168\u5c40\u72b6\u6001\u6216\u8d44\u6e90\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>: \u786e\u4fdd\u4f60\u7684\u6d4b\u8bd5\u7528\u4f8b\u548c\u6240\u4f7f\u7528\u7684\u4efb\u4f55\u5171\u4eab\u8d44\u6e90\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u5e93\u9694\u79bb<\/strong>: \u5982\u679c\u4f60\u7684\u6d4b\u8bd5\u7528\u4f8b\u6d89\u53ca\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u786e\u4fdd\u6bcf\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u90fd\u6709\u5176\u81ea\u5df1\u72ec\u7acb\u7684\u6570\u636e\u5e93\u4e8b\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u65e5\u5fd7\u548c\u8f93\u51fa<\/strong>: \u5e76\u884c\u6d4b\u8bd5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65e5\u5fd7\u548c\u63a7\u5236\u53f0\u8f93\u51fa\u6df7\u4e71\u3002\u786e\u4fdd\u4f60\u6709\u9002\u5f53\u7684\u65e5\u5fd7\u7b56\u7565\u3002<\/li>\n\n\n\n<li><strong>\u4f9d\u8d56\u5173\u7cfb<\/strong>: \u907f\u514d\u7f16\u5199\u6709\u4f9d\u8d56\u5173\u7cfb\u7684\u6d4b\u8bd5\u7528\u4f8b\uff0c\u56e0\u4e3a\u5e76\u884c\u6d4b\u8bd5\u4f1a\u6253\u4e71\u5b83\u4eec\u7684\u6267\u884c\u987a\u5e8f\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u5728 pytest \u4e2d\u8fdb\u884c\u5e76\u884c\u6d4b\u8bd5\uff0c\u5305\u62ec\u57fa\u7840\u7528\u6cd5\u3001\u9ad8\u7ea7\u7528\u6cd5\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u90a3\u4e9b\u5e0c\u671b\u4f18\u5316\u6d4b\u8bd5\u901f\u5ea6\u548c\u6548\u7387\u7684\u9ad8\u7ea7\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u9ad8\u7ea7\u4e3b\u9898\u7684\u4f7f\u7528\u547d\u4ee4\u884c\u9009\u9879ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u4f7f\u7528\u547d\u4ee4\u884c\u9009\u9879&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u9ad8\u7ea7\u4e3b\u9898&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u4e13\u95e8\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u547d\u4ee4\u884c\u9009\u9879<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>pytest \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u547d\u4ee4\u884c\u9009\u9879\uff0c\u7528\u4e8e\u81ea\u5b9a\u4e49\u6d4b\u8bd5\u8fd0\u884c\u7684\u884c\u4e3a\u3002\u8fd9\u4e9b\u9009\u9879\u5141\u8bb8\u4f60\u63a7\u5236\u6d4b\u8bd5\u53d1\u73b0\u3001\u6267\u884c\u3001\u62a5\u544a\u548c\u8bb8\u591a\u5176\u4ed6\u65b9\u9762\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6307\u5b9a\u6d4b\u8bd5\u6587\u4ef6\u548c\u76ee\u5f55<\/strong>: \u4f60\u53ef\u4ee5\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u4e2d\u6307\u5b9a\u8981\u8fd0\u884c\u7684\u6d4b\u8bd5\u6587\u4ef6\u6216\u76ee\u5f55\u3002bashCopy code<code>pytest tests\/test_example.py<\/code><\/li>\n\n\n\n<li><strong>\u8fd0\u884c\u7279\u5b9a\u6d4b\u8bd5<\/strong>: \u4f7f\u7528 <code>::<\/code> \u7b26\u53f7\u6765\u8fd0\u884c\u7279\u5b9a\u7684\u6d4b\u8bd5\u51fd\u6570\u6216\u6d4b\u8bd5\u7c7b\u3002bashCopy code<code>pytest tests\/test_example.py::test_function<\/code><\/li>\n\n\n\n<li><strong>\u6807\u8bb0\u8fc7\u6ee4<\/strong>: \u4f7f\u7528 <code>-m<\/code> \u9009\u9879\u6765\u8fd0\u884c\u5e26\u6709\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5\u3002bashCopy code<code>pytest -m slow<\/code><\/li>\n\n\n\n<li><strong>\u5173\u952e\u5b57\u8868\u8fbe\u5f0f<\/strong>: \u4f7f\u7528 <code>-k<\/code> \u9009\u9879\u6765\u8fd0\u884c\u540d\u79f0\u5339\u914d\u7279\u5b9a\u5173\u952e\u5b57\u7684\u6d4b\u8bd5\u3002bashCopy code<code>pytest -k \"TestExample and not slow\"<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u66f4\u6539\u8f93\u51fa\u8be6\u7ec6\u5ea6<\/strong>: \u4f7f\u7528 <code>-v<\/code> \u548c <code>-q<\/code> \u9009\u9879\u6765\u63a7\u5236\u8f93\u51fa\u7684\u8be6\u7ec6\u5ea6\u3002bashCopy code<code>pytest -v # Verbose pytest -q # Quiet<\/code><\/li>\n\n\n\n<li><strong>\u751f\u6210\u6d4b\u8bd5\u62a5\u544a<\/strong>: \u4f7f\u7528 <code>--junitxml<\/code> \u9009\u9879\u6765\u751f\u6210 JUnit \u683c\u5f0f\u7684\u6d4b\u8bd5\u62a5\u544a\u3002bashCopy code<code>pytest --junitxml=report.xml<\/code><\/li>\n\n\n\n<li><strong>\u5931\u8d25\u91cd\u8bd5<\/strong>: \u4f7f\u7528 <code>--maxfail<\/code> \u9009\u9879\u6765\u8bbe\u7f6e\u5931\u8d25\u91cd\u8bd5\u7684\u6b21\u6570\u3002bashCopy code<code>pytest --maxfail=3<\/code><\/li>\n\n\n\n<li><strong>\u5e76\u884c\u8fd0\u884c<\/strong>: \u5982\u679c\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 <code>pytest-xdist<\/code>\uff0c\u4f7f\u7528 <code>-n<\/code> \u9009\u9879\u6765\u5e76\u884c\u8fd0\u884c\u6d4b\u8bd5\u3002bashCopy code<code>pytest -n 4<\/code><\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u63d2\u4ef6\u9009\u9879<\/strong>: \u5b89\u88c5\u7684 pytest \u63d2\u4ef6\u53ef\u80fd\u4f1a\u63d0\u4f9b\u989d\u5916\u7684\u547d\u4ee4\u884c\u9009\u9879\u3002bashCopy code<code>pytest --some-plugin-option=value<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u51b2\u7a81<\/strong>: \u786e\u4fdd\u4f60\u7684\u81ea\u5b9a\u4e49\u547d\u4ee4\u884c\u9009\u9879\u4e0d\u4e0e pytest \u6216\u5176\u4ed6\u63d2\u4ef6\u7684\u9009\u9879\u51b2\u7a81\u3002<\/li>\n\n\n\n<li><strong>\u914d\u7f6e\u6587\u4ef6<\/strong>: \u8bb8\u591a\u547d\u4ee4\u884c\u9009\u9879\u4e5f\u53ef\u4ee5\u5728 <code>pytest.ini<\/code> \u6216 <code>pyproject.toml<\/code> \u6587\u4ef6\u4e2d\u8bbe\u7f6e\u3002<\/li>\n\n\n\n<li><strong>\u63d2\u4ef6\u517c\u5bb9\u6027<\/strong>: \u5728\u4f7f\u7528\u4e0e\u63d2\u4ef6\u76f8\u5173\u7684\u547d\u4ee4\u884c\u9009\u9879\u65f6\uff0c\u786e\u4fdd\u4f60\u7684 pytest \u548c\u63d2\u4ef6\u7248\u672c\u662f\u517c\u5bb9\u7684\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u5728 pytest \u4e2d\u4f7f\u7528\u547d\u4ee4\u884c\u9009\u9879\uff0c\u5305\u62ec\u57fa\u7840\u7528\u6cd5\u3001\u9ad8\u7ea7\u7528\u6cd5\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u9ad8\u7ea7\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u4f7f\u7528\u547d\u4ee4\u884c\u9009\u9879\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u9ad8\u7ea7\u4e3b\u9898\u7684\u96c6\u6210\u5230CI\/CD\u7ba1\u9053ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u96c6\u6210\u5230 CI\/CD \u7ba1\u9053&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u9ad8\u7ea7\u4e3b\u9898&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u91cd\u8981\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u96c6\u6210\u5230 CI\/CD \u7ba1\u9053<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>CI\/CD\uff08\u6301\u7eed\u96c6\u6210\/\u6301\u7eed\u90e8\u7f72\uff09\u662f\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u7684\u4e00\u4e2a\u5173\u952e\u7ec4\u6210\u90e8\u5206\u3002\u901a\u8fc7\u5c06 pytest \u96c6\u6210\u5230\u4f60\u7684 CI\/CD \u7ba1\u9053\u4e2d\uff0c\u4f60\u53ef\u4ee5\u81ea\u52a8\u8fd0\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u4ee3\u7801\u66f4\u6539\u4e0d\u4f1a\u7834\u574f\u73b0\u6709\u529f\u80fd\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6dfb\u52a0\u6d4b\u8bd5\u6b65\u9aa4<\/strong>: \u5728\u4f60\u7684 CI\/CD \u914d\u7f6e\u6587\u4ef6\uff08\u5982 <code>.gitlab-ci.yml<\/code>\u3001<code>Jenkinsfile<\/code> \u6216 <code>github-actions.yml<\/code>\uff09\u4e2d\uff0c\u6dfb\u52a0\u4e00\u4e2a\u8fd0\u884c pytest \u7684\u6b65\u9aa4\u3002yamlCopy code<code># \u793a\u4f8b\uff1aGitHub Actions jobs: test: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions\/checkout@v2 - name: Set up Python uses: actions\/setup-python@v2 - name: Install dependencies run: pip install -r requirements.txt - name: Run tests run: pytest<\/code><\/li>\n\n\n\n<li><strong>\u7f13\u5b58\u4f9d\u8d56<\/strong>: \u4e3a\u4e86\u52a0\u901f\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5\u7f13\u5b58\u4f9d\u8d56\u3002yamlCopy code<code># \u793a\u4f8b\uff1a\u7f13\u5b58 Python \u4f9d\u8d56 - name: Cache dependencies uses: actions\/cache@v2 with: path: ~\/.cache\/pip key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5e76\u884c\u6d4b\u8bd5<\/strong>: \u5982\u679c\u4f60\u7684\u6d4b\u8bd5\u5e93\u5f88\u5927\uff0c\u8003\u8651\u4f7f\u7528\u5e76\u884c\u6d4b\u8bd5\u6765\u51cf\u5c11\u6d4b\u8bd5\u65f6\u95f4\u3002yamlCopy code<code># \u793a\u4f8b\uff1a\u4f7f\u7528 pytest-xdist \u8fdb\u884c\u5e76\u884c\u6d4b\u8bd5 - name: Run tests in parallel run: pytest -n auto<\/code><\/li>\n\n\n\n<li><strong>\u6761\u4ef6\u6d4b\u8bd5<\/strong>: \u6839\u636e\u89e6\u53d1\u7684\u4e8b\u4ef6\u6216\u5206\u652f\u6765\u8fd0\u884c\u7279\u5b9a\u7684\u6d4b\u8bd5\u3002yamlCopy code<code># \u793a\u4f8b\uff1a\u53ea\u5728 master \u5206\u652f\u4e0a\u8fd0\u884c \"slow\" \u6807\u8bb0\u7684\u6d4b\u8bd5 - name: Run slow tests on master if: github.ref == 'refs\/heads\/master' run: pytest -m slow<\/code><\/li>\n\n\n\n<li><strong>\u751f\u6210\u5e76\u53d1\u5e03\u6d4b\u8bd5\u62a5\u544a<\/strong>: \u81ea\u52a8\u751f\u6210\u6d4b\u8bd5\u62a5\u544a\uff0c\u5e76\u5c06\u5176\u53d1\u5e03\u5230 CI\/CD \u5e73\u53f0\u3002yamlCopy code<code># \u793a\u4f8b\uff1a\u751f\u6210 JUnit \u683c\u5f0f\u7684\u6d4b\u8bd5\u62a5\u544a - name: Generate test report run: pytest --junitxml=report.xml<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u73af\u5883\u53d8\u91cf<\/strong>: \u786e\u4fdd\u6240\u6709\u9700\u8981\u7684\u73af\u5883\u53d8\u91cf\u90fd\u5728 CI\/CD \u7ba1\u9053\u4e2d\u6b63\u786e\u8bbe\u7f6e\u3002<\/li>\n\n\n\n<li><strong>\u654f\u611f\u4fe1\u606f<\/strong>: \u4e0d\u8981\u5728 CI\/CD \u914d\u7f6e\u6587\u4ef6\u4e2d\u786c\u7f16\u7801\u654f\u611f\u4fe1\u606f\uff0c\u5982\u6570\u636e\u5e93\u51ed\u636e\u6216 API \u5bc6\u94a5\u3002<\/li>\n\n\n\n<li><strong>\u63d2\u4ef6\u517c\u5bb9\u6027<\/strong>: \u786e\u4fdd CI\/CD \u73af\u5883\u4e2d\u7684 pytest \u548c\u63d2\u4ef6\u7248\u672c\u4e0e\u4f60\u7684\u672c\u5730\u73af\u5883\u4e00\u81f4\u3002<\/li>\n\n\n\n<li><strong>\u8d44\u6e90\u9650\u5236<\/strong>: \u6ce8\u610f CI\/CD \u670d\u52a1\u53ef\u80fd\u6709\u8d44\u6e90\u9650\u5236\uff0c\u5982\u6700\u5927\u8fd0\u884c\u65f6\u95f4\u6216\u5e76\u884c\u4efb\u52a1\u6570\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5982\u4f55\u5728 pytest \u4e2d\u96c6\u6210\u5230 CI\/CD \u7ba1\u9053\uff0c\u5305\u62ec\u57fa\u7840\u7528\u6cd5\u3001\u9ad8\u7ea7\u7528\u6cd5\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u9ad8\u7ea7\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u548c CI\/CD \u96c6\u6210\u65b9\u9762\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u6700\u4f73\u5b9e\u8df5\u7684\u4ee3\u7801\u7ec4\u7ec7ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u4ee3\u7801\u7ec4\u7ec7&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u6700\u4f73\u5b9e\u8df5&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5173\u952e\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u7ec4\u7ec7<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u4ee3\u7801\u7ec4\u7ec7\u662f\u6d4b\u8bd5\u5de5\u7a0b\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u65b9\u9762\uff0c\u5b83\u6d89\u53ca\u5230\u5982\u4f55\u7ed3\u6784\u5316\u4f60\u7684\u6d4b\u8bd5\u4ee3\u7801\u3001\u6d4b\u8bd5\u6570\u636e\u548c\u914d\u7f6e\u6587\u4ef6\uff0c\u4ee5\u4fbf\u4e8e\u7ef4\u62a4\u3001\u6269\u5c55\u548c\u534f\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u76ee\u5f55\u7ed3\u6784<\/strong>: \u901a\u5e38\uff0c\u6d4b\u8bd5\u4ee3\u7801\u5e94\u8be5\u5b58\u653e\u5728\u4e00\u4e2a\u540d\u4e3a <code>tests<\/code> \u7684\u5355\u72ec\u76ee\u5f55\u4e2d\u3002cssCopy code<code>my_project\/ \u251c\u2500\u2500 src\/ \u251c\u2500\u2500 tests\/ \u2502 \u251c\u2500\u2500 unit\/ \u2502 \u251c\u2500\u2500 integration\/ \u2502 \u2514\u2500\u2500 e2e\/ \u2514\u2500\u2500 pytest.ini<\/code><\/li>\n\n\n\n<li><strong>\u547d\u540d\u7ea6\u5b9a<\/strong>: \u6d4b\u8bd5\u6587\u4ef6\u5e94\u4ee5 <code>test_<\/code> \u524d\u7f00\u5f00\u59cb\uff0c\u6d4b\u8bd5\u51fd\u6570\u548c\u65b9\u6cd5\u5e94\u4ee5 <code>test_<\/code> \u5f00\u5934\u3002pythonCopy code<code># \u6587\u4ef6\u540d: test_example.py def test_addition(): assert 1 + 1 == 2<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528 Fixtures<\/strong>: \u5c06\u5e38\u7528\u7684\u8bbe\u7f6e\u4ee3\u7801\u653e\u5165 fixtures \u4e2d\uff0c\u4ee5\u4fbf\u91cd\u7528\u3002pythonCopy code<code>@pytest.fixture def sample_data(): return {\"key\": \"value\"}<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6a21\u5757\u7ea7 Fixtures<\/strong>: \u5bf9\u4e8e\u8de8\u591a\u4e2a\u6d4b\u8bd5\u51fd\u6570\u6216\u7c7b\u5171\u4eab\u7684\u8d44\u6e90\uff0c\u4f7f\u7528\u6a21\u5757\u7ea7\u6216\u4f1a\u8bdd\u7ea7 fixtures\u3002pythonCopy code<code>@pytest.fixture(scope=\"module\") def db_connection(): # Setup code yield connection # Teardown code<\/code><\/li>\n\n\n\n<li><strong>\u53c2\u6570\u5316\u6d4b\u8bd5<\/strong>: \u4f7f\u7528 pytest \u7684\u53c2\u6570\u5316\u529f\u80fd\u6765\u51cf\u5c11\u91cd\u590d\u4ee3\u7801\u3002pythonCopy code<code>@pytest.mark.parametrize(\"input,expected\", [(1, 2), (2, 3), (3, 4)]) def test_increment(input, expected): assert input + 1 == expected<\/code><\/li>\n\n\n\n<li><strong>\u6807\u8bb0\u548c\u9009\u62e9\u5668<\/strong>: \u4f7f\u7528\u6807\u8bb0\u6765\u5206\u7c7b\u6d4b\u8bd5\uff0c\u5e76\u4f7f\u7528\u547d\u4ee4\u884c\u9009\u62e9\u5668\u6765\u8fd0\u884c\u7279\u5b9a\u7684\u6d4b\u8bd5\u96c6\u3002pythonCopy code<code>@pytest.mark.slow def test_slow_operation(): pass <\/code>\u8fd0\u884c\uff1abashCopy code<code>pytest -m slow<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u5168\u5c40\u72b6\u6001<\/strong>: \u5c3d\u91cf\u4e0d\u8981\u5728\u6d4b\u8bd5\u4e2d\u4f7f\u7528\u5168\u5c40\u72b6\u6001\u6216\u5355\u4f8b\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5f71\u54cd\u6d4b\u8bd5\u7684\u72ec\u7acb\u6027\u3002<\/li>\n\n\n\n<li><strong>\u6e05\u7406\u8d44\u6e90<\/strong>: \u786e\u4fdd\u6240\u6709\u7684\u8d44\u6e90\uff08\u5982\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6587\u4ef6\u7b49\uff09\u5728\u6d4b\u8bd5\u5b8c\u6210\u540e\u90fd\u5f97\u5230\u4e86\u59a5\u5584\u7684\u6e05\u7406\u3002<\/li>\n\n\n\n<li><strong>\u6587\u6863\u548c\u6ce8\u91ca<\/strong>: \u5728\u590d\u6742\u7684\u6d4b\u8bd5\u6216 fixtures \u4e2d\u6dfb\u52a0\u9002\u91cf\u7684\u6587\u6863\u548c\u6ce8\u91ca\u3002<\/li>\n\n\n\n<li><strong>\u7248\u672c\u63a7\u5236<\/strong>: \u5c06\u6d4b\u8bd5\u4ee3\u7801\u548c\u4e3b\u4ee3\u7801\u4e00\u8d77\u653e\u5165\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u4e2d\uff0c\u4ee5\u4fbf\u8ddf\u8e2a\u66f4\u6539\u548c\u4fc3\u8fdb\u56e2\u961f\u534f\u4f5c\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5728 pytest \u4e2d\u5173\u4e8e\u4ee3\u7801\u7ec4\u7ec7\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5305\u62ec\u57fa\u7840\u7528\u6cd5\u3001\u9ad8\u7ea7\u7528\u6cd5\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u6240\u6709\u7ea7\u522b\u7684\u5f00\u53d1\u8005\u548c\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u8fdb\u884c\u9ad8\u6548\u4ee3\u7801\u7ec4\u7ec7\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u6700\u4f73\u5b9e\u8df5\u7684\u6d4b\u8bd5\u8986\u76d6\u7387ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u6d4b\u8bd5\u8986\u76d6\u7387&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u6700\u4f73\u5b9e\u8df5&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u91cd\u8981\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6d4b\u8bd5\u8986\u76d6\u7387<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u6d4b\u8bd5\u8986\u76d6\u7387\u662f\u4e00\u4e2a\u5ea6\u91cf\u6807\u51c6\uff0c\u7528\u4e8e\u63cf\u8ff0\u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u88ab\u6267\u884c\u7684\u4ee3\u7801\u91cf\u4e0e\u603b\u4ee3\u7801\u91cf\u7684\u6bd4\u4f8b\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u8bc4\u4f30\u6d4b\u8bd5\u7684\u5168\u9762\u6027\u548c\u8d28\u91cf\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b89\u88c5\u8986\u76d6\u7387\u5de5\u5177<\/strong>: \u4f7f\u7528 <code>pytest-cov<\/code> \u63d2\u4ef6\u6765\u96c6\u6210\u8986\u76d6\u7387\u62a5\u544a\u3002bashCopy code<code>pip install pytest-cov<\/code><\/li>\n\n\n\n<li><strong>\u8fd0\u884c\u8986\u76d6\u7387\u6d4b\u8bd5<\/strong>: \u5728\u8fd0\u884c pytest \u65f6\uff0c\u6dfb\u52a0 <code>--cov<\/code> \u9009\u9879\u3002bashCopy code<code>pytest --cov=my_project <\/code>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u8986\u76d6\u7387\u62a5\u544a\uff0c\u663e\u793a\u6bcf\u4e2a\u6587\u4ef6\u7684\u8986\u76d6\u7387\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u751f\u6210 HTML \u62a5\u544a<\/strong>: \u4f7f\u7528 <code>--cov-report<\/code> \u9009\u9879\u751f\u6210 HTML \u683c\u5f0f\u7684\u8986\u76d6\u7387\u62a5\u544a\u3002bashCopy code<code>pytest --cov=my_project --cov-report html <\/code>\u8fd9\u5c06\u5728 <code>htmlcov\/<\/code> \u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a HTML \u62a5\u544a\u3002<\/li>\n\n\n\n<li><strong>\u8bbe\u7f6e\u8986\u76d6\u7387\u9608\u503c<\/strong>: \u4f60\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u8986\u76d6\u7387\u9608\u503c\uff0c\u5982\u679c\u5b9e\u9645\u8986\u76d6\u7387\u4f4e\u4e8e\u8fd9\u4e2a\u503c\uff0c\u6d4b\u8bd5\u5c06\u5931\u8d25\u3002bashCopy code<code>pytest --cov=my_project --cov-fail-under=90 <\/code>\u8fd9\u5c06\u5728\u8986\u76d6\u7387\u4f4e\u4e8e 90% \u65f6\u5bfc\u81f4\u6d4b\u8bd5\u5931\u8d25\u3002<\/li>\n\n\n\n<li><strong>\u6392\u9664\u4ee3\u7801<\/strong>: \u4f7f\u7528 <code>.coveragerc<\/code> \u6587\u4ef6\u6765\u6392\u9664\u67d0\u4e9b\u4ee3\u7801\u5757\u6216\u6587\u4ef6\u3002iniCopy code<code># .coveragerc [report] omit = my_project\/excluded_file.py<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e0d\u662f\u552f\u4e00\u6307\u6807<\/strong>: \u867d\u7136\u8986\u76d6\u7387\u662f\u4e00\u4e2a\u6709\u7528\u7684\u6307\u6807\uff0c\u4f46\u5b83\u4e0d\u5e94\u8be5\u662f\u8bc4\u4f30\u6d4b\u8bd5\u8d28\u91cf\u7684\u552f\u4e00\u6807\u51c6\u3002<\/li>\n\n\n\n<li><strong>\u8986\u76d6\u7387\u7c7b\u578b<\/strong>: \u6ce8\u610f\u8986\u76d6\u7387\u6709\u591a\u79cd\u7c7b\u578b\uff0c\u5305\u62ec\u884c\u8986\u76d6\u7387\u3001\u5206\u652f\u8986\u76d6\u7387\u3001\u51fd\u6570\u8986\u76d6\u7387\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u6301\u7eed\u76d1\u63a7<\/strong>: \u5728 CI\/CD \u7ba1\u9053\u4e2d\u96c6\u6210\u8986\u76d6\u7387\u68c0\u67e5\uff0c\u4ee5\u6301\u7eed\u76d1\u63a7\u8986\u76d6\u7387\u53d8\u5316\u3002<\/li>\n\n\n\n<li><strong>\u4ee3\u7801\u5ba1\u67e5<\/strong>: \u4f7f\u7528\u8986\u76d6\u7387\u62a5\u544a\u4f5c\u4e3a\u4ee3\u7801\u5ba1\u67e5\u7684\u4e00\u90e8\u5206\uff0c\u4ee5\u8bc6\u522b\u53ef\u80fd\u9700\u8981\u66f4\u591a\u6d4b\u8bd5\u7684\u533a\u57df\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5728 pytest \u4e2d\u5173\u4e8e\u6d4b\u8bd5\u8986\u76d6\u7387\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5305\u62ec\u57fa\u7840\u7528\u6cd5\u3001\u9ad8\u7ea7\u7528\u6cd5\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u5f00\u53d1\u8005\u548c\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u8fdb\u884c\u6709\u6548\u7684\u8986\u76d6\u7387\u6d4b\u8bd5\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u6700\u4f73\u5b9e\u8df5\u7684\u65e5\u5fd7\u548c\u62a5\u544aChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u65e5\u5fd7\u548c\u62a5\u544a&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u4e2d&#8221;\u6700\u4f73\u5b9e\u8df5&#8221;\u90e8\u5206\u7684\u4e00\u4e2a\u5173\u952e\u5b50\u8282\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u65e5\u5fd7\u548c\u62a5\u544a<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u4e49<\/h4>\n\n\n\n<p>\u65e5\u5fd7\u548c\u62a5\u544a\u662f\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u5b83\u4eec\u4e0d\u4ec5\u5e2e\u52a9\u4f60\u8ddf\u8e2a\u6d4b\u8bd5\u7684\u6267\u884c\u60c5\u51b5\uff0c\u8fd8\u80fd\u4e3a\u56e2\u961f\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u53cd\u9988\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u5185\u7f6e\u65e5\u5fd7<\/strong>: pytest \u652f\u6301 Python \u7684\u5185\u7f6e <code>logging<\/code> \u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u5728\u6d4b\u8bd5\u6216 fixtures \u4e2d\u4f7f\u7528\u5b83\u3002pythonCopy code<code>import logging def test_logging(): logging.info(\"This is an info message\")<\/code><\/li>\n\n\n\n<li><strong>\u751f\u6210\u7b80\u5355\u62a5\u544a<\/strong>: \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cpytest \u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u7b80\u5355\u7684\u6d4b\u8bd5\u62a5\u544a\u3002bashCopy code<code>pytest<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u751f\u6210\u8be6\u7ec6\u62a5\u544a<\/strong>: \u4f7f\u7528 <code>--junitxml<\/code> \u9009\u9879\u751f\u6210 JUnit \u683c\u5f0f\u7684\u8be6\u7ec6\u6d4b\u8bd5\u62a5\u544a\u3002bashCopy code<code>pytest --junitxml=path\/to\/report.xml<\/code><\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u65e5\u5fd7\u914d\u7f6e<\/strong>: \u4f60\u53ef\u4ee5\u5728 <code>pytest.ini<\/code> \u6587\u4ef6\u4e2d\u81ea\u5b9a\u4e49\u65e5\u5fd7\u8bbe\u7f6e\u3002iniCopy code<code># pytest.ini [pytest] log_cli = true log_cli_level = INFO<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u63d2\u4ef6\u751f\u6210\u62a5\u544a<\/strong>: \u6709\u591a\u4e2a pytest \u63d2\u4ef6\u53ef\u7528\u4e8e\u751f\u6210\u66f4\u4e30\u5bcc\u7684\u6d4b\u8bd5\u62a5\u544a\uff0c\u5982 <code>pytest-html<\/code>\u3002bashCopy code<code>pip install pytest-html pytest --html=path\/to\/report.html<\/code><\/li>\n\n\n\n<li><strong>\u65e5\u5fd7\u6355\u83b7<\/strong>: \u4f7f\u7528 <code>--capture<\/code> \u9009\u9879\u6765\u63a7\u5236 stdout \u548c stderr \u7684\u6355\u83b7\u3002bashCopy code<code>pytest --capture=no # \u4e0d\u6355\u83b7\u8f93\u51fa<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u65e5\u5fd7\u7ea7\u522b<\/strong>: \u786e\u4fdd\u9009\u62e9\u9002\u5f53\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u4ee5\u907f\u514d\u8f93\u51fa\u8fc7\u591a\u65e0\u7528\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u654f\u611f\u4fe1\u606f<\/strong>: \u907f\u514d\u5728\u65e5\u5fd7\u548c\u62a5\u544a\u4e2d\u8f93\u51fa\u654f\u611f\u6216\u79c1\u5bc6\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u62a5\u544a\u5b58\u50a8<\/strong>: \u786e\u4fdd\u6d4b\u8bd5\u62a5\u544a\u5b58\u50a8\u5728\u4e00\u4e2a\u5b89\u5168\u548c\u53ef\u8bbf\u95ee\u7684\u4f4d\u7f6e\u3002<\/li>\n\n\n\n<li><strong>\u6301\u7eed\u96c6\u6210<\/strong>: \u5728 CI\/CD \u7ba1\u9053\u4e2d\u96c6\u6210\u65e5\u5fd7\u548c\u62a5\u544a\uff0c\u4ee5\u81ea\u52a8\u5316\u8fd9\u4e00\u8fc7\u7a0b\u5e76\u63d0\u4f9b\u5b9e\u65f6\u53cd\u9988\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u5b50\u8282\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u5728 pytest \u4e2d\u5173\u4e8e\u65e5\u5fd7\u548c\u62a5\u544a\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5305\u62ec\u57fa\u7840\u7528\u6cd5\u3001\u9ad8\u7ea7\u7528\u6cd5\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u7b49\u65b9\u9762\u3002\u8fd9\u5c06\u4e3a\u5f00\u53d1\u8005\u548c\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u63d0\u4f9b\u4e00\u4e2a\u660e\u786e\u548c\u5bb9\u6613\u9075\u5faa\u7684\u8def\u5f84\uff0c\u4ee5\u5f00\u59cb\u4ed6\u4eec\u5728 pytest \u4e2d\u8fdb\u884c\u6709\u6548\u7684\u65e5\u5fd7\u548c\u62a5\u544a\u751f\u6210\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca \u7ed3\u8bba\u4e0e\u4e0b\u4e00\u6b65\u7684\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u8d44\u6e90ChatGPT<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4ee5\u4e0b\u662f\u5bf9&#8221;\u7ed3\u8bba\u4e0e\u4e0b\u4e00\u6b65\u7684\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u8d44\u6e90&#8221;\u8fd9\u4e00\u90e8\u5206\u7684\u8be6\u7ec6\u89e3\u91ca\u3002\u8fd9\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u6559\u7a0b\u7684\u7ed3\u5c3e\u90e8\u5206\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed3\u8bba\u4e0e\u4e0b\u4e00\u6b65\u7684\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u8d44\u6e90<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u7ed3\u8bba<\/h4>\n\n\n\n<p>\u901a\u8fc7\u672c\u6559\u7a0b\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86 pytest \u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ec\u5b89\u88c5\u3001\u914d\u7f6e\u3001\u57fa\u7840\u6982\u5ff5\u3001\u4f7f\u7528 Fixtures\u3001\u53c2\u6570\u5316\u6d4b\u8bd5\u3001\u6807\u8bb0\u4e0e\u6807\u7b7e\u3001\u5904\u7406\u5f02\u5e38\u3001\u9ad8\u7ea7\u4e3b\u9898\u4ee5\u53ca\u6700\u4f73\u5b9e\u8df5\u7b49\u3002\u8fd9\u4e9b\u77e5\u8bc6\u5c06\u4e3a\u4f60\u5728 Python \u6d4b\u8bd5\u9886\u57df\u5185\u7684\u8fdb\u4e00\u6b65\u53d1\u5c55\u5960\u5b9a\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u8d44\u6e90<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b98\u65b9\u6587\u6863<\/strong>: pytest \u7684<a href=\"https:\/\/docs.pytest.org\/en\/latest\/\">\u5b98\u65b9\u6587\u6863<\/a>\u662f\u6700\u6743\u5a01\u3001\u6700\u5168\u9762\u7684\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u5728\u7ebf\u8bfe\u7a0b\u548c\u6559\u7a0b<\/strong>: \u7f51\u7ad9\u5982 Udemy, Coursera \u548c YouTube \u63d0\u4f9b\u4e86\u5927\u91cf\u7684 pytest \u6559\u7a0b\u548c\u8bfe\u7a0b\u3002<\/li>\n\n\n\n<li><strong>\u793e\u533a\u548c\u8bba\u575b<\/strong>: <a href=\"https:\/\/stackoverflow.com\/questions\/tagged\/pytest\">Stack Overflow<\/a> \u548c <a href=\"https:\/\/www.reddit.com\/r\/pytest\/\">Reddit<\/a> \u662f\u89e3\u51b3\u95ee\u9898\u548c\u5b66\u4e60\u6700\u4f73\u5b9e\u8df5\u7684\u597d\u5730\u65b9\u3002<\/li>\n\n\n\n<li><strong>\u4e66\u7c4d<\/strong>: \u6709\u591a\u672c\u5173\u4e8e Python \u6d4b\u8bd5\u548c pytest \u7684\u4e66\u7c4d\uff0c\u5982 &#8220;Python Testing with pytest&#8221;\u3002<\/li>\n\n\n\n<li><strong>\u5f00\u6e90\u9879\u76ee<\/strong>: \u5728 GitHub \u4e0a\u53c2\u4e0e\u4e00\u4e9b\u4f7f\u7528 pytest \u7684\u5f00\u6e90\u9879\u76ee\uff0c\u4ee5\u83b7\u5f97\u5b9e\u9645\u7ecf\u9a8c\u3002<\/li>\n\n\n\n<li><strong>\u535a\u5ba2\u548c\u6587\u7ae0<\/strong>: \u8bb8\u591a\u5f00\u53d1\u8005\u548c\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u5728\u4ed6\u4eec\u7684\u535a\u5ba2\u6216 Medium \u4e0a\u53d1\u5e03\u5173\u4e8e pytest \u7684\u9ad8\u8d28\u91cf\u6587\u7ae0\u3002<\/li>\n\n\n\n<li><strong>\u5de5\u4f5c\u574a\u548c\u7814\u8ba8\u4f1a<\/strong>: \u65f6\u4e0d\u65f6\u5730\u53c2\u52a0\u4e0e pytest \u76f8\u5173\u7684\u5de5\u4f5c\u574a\u548c\u7814\u8ba8\u4f1a\uff0c\u4ee5\u6269\u5927\u4f60\u7684\u77e5\u8bc6\u548c\u7f51\u7edc\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e0b\u4e00\u6b65<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b9e\u8df5<\/strong>: \u6700\u597d\u7684\u5b66\u4e60\u65b9\u5f0f\u5c31\u662f\u505a\u3002\u5c1d\u8bd5\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u5b9e\u65bd\u4f60\u6240\u5b66\u7684\u77e5\u8bc6\u3002<\/li>\n\n\n\n<li><strong>\u6301\u7eed\u5b66\u4e60<\/strong>: \u6d4b\u8bd5\u662f\u4e00\u4e2a\u5feb\u901f\u53d1\u5c55\u7684\u9886\u57df\u3002\u8ba2\u9605\u76f8\u5173\u7684\u65b0\u95fb\u6e90\u548c\u793e\u4ea4\u5a92\u4f53\u6e20\u9053\uff0c\u4ee5\u4fdd\u6301\u6700\u65b0\u72b6\u6001\u3002<\/li>\n\n\n\n<li><strong>\u8d21\u732e<\/strong>: \u8003\u8651\u4e3a pytest \u793e\u533a\u505a\u51fa\u8d21\u732e\uff0c\u65e0\u8bba\u662f\u901a\u8fc7\u7f16\u5199\u6587\u6863\uff0c\u8fd8\u662f\u901a\u8fc7\u89e3\u51b3\u95ee\u9898\u548c\u6dfb\u52a0\u65b0\u529f\u80fd\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u7ed3\u5c3e\u90e8\u5206\u4e0d\u4ec5\u603b\u7ed3\u4e86\u6574\u4e2a\u6559\u7a0b\u7684\u5185\u5bb9\uff0c\u8fd8\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u548c\u8d44\u6e90\uff0c\u4ee5\u4fbf\u8bfb\u8005\u53ef\u4ee5\u7ee7\u7eed\u4ed6\u4eec\u7684\u5b66\u4e60\u4e4b\u65c5\u3002\u5e0c\u671b\u8fd9\u80fd\u5e2e\u52a9\u4f60\u5728\u521b\u5efa\u4f60\u7684 pytest \u6559\u7a0b\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u8981\u505apytest\u7684\u4e2d\u6587\u6559\u7a0b\uff0c\u8bf7\u5148\u7ed9\u51fa\u8be5 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-2962","post","type-post","status-publish","format-standard","hentry","category-material"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts\/2962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2962"}],"version-history":[{"count":1,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts\/2962\/revisions"}],"predecessor-version":[{"id":2963,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts\/2962\/revisions\/2963"}],"wp:attachment":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}