{"id":102560,"date":"2026-09-09T07:09:34","date_gmt":"2026-09-08T23:09:34","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/102560.html"},"modified":"2026-09-09T07:09:34","modified_gmt":"2026-09-08T23:09:34","slug":"pytest-fixture-%e8%bf%9b%e9%98%b6%ef%bc%9aconftest-%e4%b8%8e%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/102560.html","title":{"rendered":"pytest Fixture \u8fdb\u9636\uff1aconftest \u4e0e\u4f9d\u8d56\u6ce8\u5165"},"content":{"rendered":"<p>\u4e13\u680f&#xff1a;Python \u81ea\u52a8\u5316\u6d4b\u8bd5\u4ece\u5165\u95e8\u5230\u5b9e\u6218 \u00b7 \u7b2c 4 \u7bc7 \u4e0a\u63a5&#xff1a;\u7b2c 3 \u7bc7\u300apytest \u53c2\u6570\u5316\u4e0e Mark \u6807\u8bb0\u300b \u4e0b\u63a5&#xff1a;\u7b2c 5 \u7bc7\u300apytest \u63d2\u4ef6\u751f\u6001\u4e0e Allure \u62a5\u544a\u300b&#xff08;\u9884\u544a&#xff09; \u9002\u8bfb\u4eba\u7fa4&#xff1a;\u5df2\u638c\u63e1 Fixture \u57fa\u7840&#xff08;scope \/ yield&#xff09;\u7684 pytest \u4f7f\u7528\u8005<\/p>\n<hr \/>\n<h3>\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u7b2c 2 \u7bc7\u4e0d\u591f\u7528&#xff1f;<\/h3>\n<p>\u7b2c 2 \u7bc7\u91cc&#xff0c;\u6211\u4eec\u5b66\u4f1a\u4e86\u7528 &#064;pytest.fixture \u628a setup\/teardown \u5199\u8fdb\u4e00\u4e2a\u51fd\u6570\u3002\u90a3\u65f6\u7684 fixture \u548c\u6d4b\u8bd5\u5199\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u91cc&#xff0c;\u50cf\u8fd9\u6837&#xff1a;<\/p>\n<p><span class=\"token comment\"># test_calc.py<\/span><br \/>\n<span class=\"token keyword\">import<\/span> pytest<\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">calc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">from<\/span> calculator <span class=\"token keyword\">import<\/span> Calculator<br \/>\n    <span class=\"token keyword\">return<\/span> Calculator<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_add<\/span><span class=\"token punctuation\">(<\/span>calc<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> calc<span class=\"token punctuation\">.<\/span>add<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">3<\/span><\/p>\n<p>\u8fd9\u5728\u5355\u6587\u4ef6 demo \u91cc\u6ca1\u95ee\u9898\u3002\u4f46\u771f\u5b9e\u9879\u76ee\u7684\u76ee\u5f55\u957f\u8fd9\u6837&#xff1a;<\/p>\n<p>project\/<br \/>\n\u251c\u2500\u2500 src\/calculator.py<br \/>\n\u251c\u2500\u2500 tests\/<br \/>\n\u2502   \u251c\u2500\u2500 test_add.py<br \/>\n\u2502   \u251c\u2500\u2500 test_sub.py<br \/>\n\u2502   \u251c\u2500\u2500 test_mul.py<br \/>\n\u2502   \u2514\u2500\u2500 test_div.py<\/p>\n<p>\u95ee\u9898\u6765\u4e86&#xff1a;\u5982\u679c\u6bcf\u4e2a\u6d4b\u8bd5\u6587\u4ef6\u90fd copy \u4e00\u4efd calc fixture&#xff0c;\u6539\u4e00\u4e2a\u903b\u8f91\u5c31\u8981\u6539\u56db\u5904\u2014\u2014\u8fd9\u8fdd\u53cd\u4e86 DRY&#xff0c;\u4e5f\u4e0d\u662f&#034;\u8fdb\u9636&#034;\u8be5\u6709\u7684\u6837\u5b50\u3002<\/p>\n<p>\u8fd9\u4e00\u7bc7\u8981\u89e3\u51b3\u7684\u5c31\u662f&#xff1a;fixture \u5982\u4f55\u8de8\u6587\u4ef6\u5171\u4eab\u3001\u5982\u4f55\u5206\u5c42\u7ec4\u7ec7\u3001\u5982\u4f55\u50cf\u642d\u79ef\u6728\u4e00\u6837\u7ec4\u5408\u4f9d\u8d56\u3002 \u4e09\u4e2a\u5173\u952e\u8bcd&#xff1a;<\/p>\n<ul>\n<li>conftest.py \u2014\u2014 fixture \u7684&#034;\u516c\u5171\u4ed3\u5e93&#034;<\/li>\n<li>Fixture \u5206\u5c42\u4e0e\u4f5c\u7528\u57df\u94fe \u2014\u2014 \u9879\u76ee\u7ea7 \/ \u6a21\u5757\u7ea7 \/ \u7528\u4f8b\u7ea7\u5404\u53f8\u5176\u804c<\/li>\n<li>\u4f9d\u8d56\u6ce8\u5165&#xff08;DI&#xff09; \u2014\u2014 pytest \u7684&#034;\u58f0\u660e\u5373\u6ce8\u5165&#034;\u54f2\u5b66<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e00\u3001conftest.py&#xff1a;Fixture \u7684\u5171\u4eab\u5c42<\/h3>\n<h4>1.1 \u5b83\u662f\u4ec0\u4e48<\/h4>\n<p>conftest.py \u662f pytest \u81ea\u52a8\u8bc6\u522b\u7684\u914d\u7f6e\u6587\u4ef6&#xff0c;\u7528\u4e8e\u5b58\u653e\u591a\u4e2a\u6d4b\u8bd5\u6a21\u5757\u5171\u7528\u7684 fixture \u548c hook\u3002 \u5b83\u4e0d\u9700\u8981 import\u2014\u2014pytest \u4f1a\u5728\u6d4b\u8bd5\u6536\u96c6\u9636\u6bb5\u81ea\u52a8\u53d1\u73b0\u5e76\u52a0\u8f7d\u3002<\/p>\n<p>\u6838\u5fc3\u89c4\u5219&#xff1a;\u4e00\u4e2a\u76ee\u5f55\u4e00\u4e2a conftest&#xff0c;\u4f5c\u7528\u8303\u56f4\u5411\u4e0b\u9012\u5f52\u3002<\/p>\n<p>project\/<br \/>\n\u251c\u2500\u2500 conftest.py            \u2190 \u2460 \u5168\u5c40\u7ea7&#xff08;\u6574\u4e2a project&#xff09;<br \/>\n\u251c\u2500\u2500 src\/calculator.py<br \/>\n\u2514\u2500\u2500 tests\/<br \/>\n    \u251c\u2500\u2500 conftest.py        \u2190 \u2461 \u6a21\u5757\u7ea7&#xff08;\u4ec5 tests\/ \u4e0b\u751f\u6548&#xff09;<br \/>\n    \u251c\u2500\u2500 test_add.py<br \/>\n    \u2514\u2500\u2500 api\/<br \/>\n        \u251c\u2500\u2500 conftest.py    \u2190 \u2462 \u5b50\u6a21\u5757\u7ea7&#xff08;\u4ec5 api\/ \u4e0b\u751f\u6548&#xff09;<br \/>\n        \u2514\u2500\u2500 test_api.py<\/p>\n<p>\u5c31\u8fd1\u539f\u5219&#xff1a;test_api.py \u8bf7\u6c42\u4e00\u4e2a fixture \u65f6&#xff0c;pytest \u7684\u67e5\u627e\u987a\u5e8f\u662f&#xff1a;<\/p>\n<p>tests\/api\/conftest.py  \u2192  tests\/conftest.py  \u2192  project\/conftest.py  \u2192  \u5185\u7f6e<\/p>\n<p>\u79bb\u8bf7\u6c42\u6700\u8fd1\u7684\u5148\u5339\u914d&#xff0c;\u8fd9\u8ddf Python \u7684\u53d8\u91cf\u4f5c\u7528\u57df&#xff08;LEGB&#xff09;\u4e00\u4e2a\u9053\u7406\u3002<\/p>\n<h4>1.2 \u7b2c\u4e00\u539f\u5219&#xff1a;\u5171\u4eab\u7684\u653e conftest&#xff0c;\u79c1\u6709\u7684\u7559\u672c\u5730<\/h4>\n<table>\n<tr>\u653e\u54ea\u91cc\u4f8b\u5b50\u7406\u7531<\/tr>\n<tbody>\n<tr>\n<td>\u2705 conftest.py<\/td>\n<td>\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6d4f\u89c8\u5668\u5b9e\u4f8b\u3001\u767b\u5f55\u6001\u3001\u914d\u7f6e\u52a0\u8f7d<\/td>\n<td>\u591a\u6587\u4ef6\u5171\u7528&#xff0c;\u6539\u4e00\u5904\u5168\u5c40\u751f\u6548<\/td>\n<\/tr>\n<tr>\n<td>\u2705 \u6d4b\u8bd5\u6587\u4ef6\u5185<\/td>\n<td>\u67d0\u4e2a\u7528\u4f8b\u4e13\u5c5e\u7684\u4e34\u65f6\u6570\u636e<\/td>\n<td>\u53ea\u6709\u8fd9\u4e00\u4e2a\u6587\u4ef6\u7528&#xff0c;\u522b\u6c61\u67d3\u5168\u5c40<\/td>\n<\/tr>\n<tr>\n<td>\u274c \u522b\u653e __init__.py<\/td>\n<td>\u2014\u2014<\/td>\n<td>pytest \u4e0d\u81ea\u52a8\u52a0\u8f7d&#xff0c;\u4e14\u6c61\u67d3\u4e1a\u52a1\u5305<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4a1; \u7ecf\u9a8c\u6cd5\u5219&#xff1a;\u5f53\u4f60\u53d1\u73b0\u81ea\u5df1\u5728\u7b2c\u4e09\u4e2a\u6587\u4ef6\u91cc copy \u540c\u4e00\u4e2a fixture \u65f6&#xff0c;\u5c31\u662f\u628a\u5b83\u642c\u8fdb conftest.py \u7684\u4fe1\u53f7\u3002<\/p>\n<h4>1.3 \u52a8\u624b&#xff1a;\u628a\u8ba1\u7b97\u5668 fixture \u642c\u8fdb conftest<\/h4>\n<p>\u6cbf\u7528\u4e13\u680f\u4e00\u8d2f\u7684\u9879\u76ee\u7ed3\u6784&#xff1a;<\/p>\n<p>pytest_series\/<br \/>\n\u251c\u2500\u2500 src\/<br \/>\n\u2502   \u2514\u2500\u2500 calculator.py<br \/>\n\u251c\u2500\u2500 tests\/<br \/>\n\u2502   \u251c\u2500\u2500 conftest.py        \u2190 &#x1f195; \u672c\u7bc7\u6838\u5fc3<br \/>\n\u2502   \u251c\u2500\u2500 test_add.py<br \/>\n\u2502   \u251c\u2500\u2500 test_sub.py<br \/>\n\u2502   \u2514\u2500\u2500 test_mul.py<br \/>\n\u2514\u2500\u2500 pytest.ini<\/p>\n<p>src\/calculator.py&#xff08;\u4e0e\u7b2c 1~3 \u7bc7\u4fdd\u6301\u4e00\u81f4&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Calculator<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">add<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> a<span class=\"token punctuation\">,<\/span> b<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token keyword\">return<\/span> a <span class=\"token operator\">&#043;<\/span> b<br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">sub<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> a<span class=\"token punctuation\">,<\/span> b<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token keyword\">return<\/span> a <span class=\"token operator\">&#8211;<\/span> b<br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">mul<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> a<span class=\"token punctuation\">,<\/span> b<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token keyword\">return<\/span> a <span class=\"token operator\">*<\/span> b<br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">div<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> a<span class=\"token punctuation\">,<\/span> b<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> b <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">raise<\/span> ValueError<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u9664\u6570\u4e0d\u80fd\u4e3a 0&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> a <span class=\"token operator\">\/<\/span> b<\/p>\n<p>tests\/conftest.py&#xff08;\u5171\u4eab fixture&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> sys<span class=\"token punctuation\">,<\/span> os<br \/>\n<span class=\"token keyword\">import<\/span> pytest<\/p>\n<p><span class=\"token comment\"># \u8ba9 tests\/ \u80fd import src\/&#xff08;\u5c0f\u578b\u9879\u76ee\u5e38\u7528\u6280\u5de7&#xff0c;\u5927\u9879\u76ee\u5efa\u8bae\u7528 pip install -e .&#xff09;<\/span><br \/>\nsys<span class=\"token punctuation\">.<\/span>path<span class=\"token punctuation\">.<\/span>insert<span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> os<span class=\"token punctuation\">.<\/span>path<span class=\"token punctuation\">.<\/span>abspath<span class=\"token punctuation\">(<\/span>os<span class=\"token punctuation\">.<\/span>path<span class=\"token punctuation\">.<\/span>join<span class=\"token punctuation\">(<\/span>os<span class=\"token punctuation\">.<\/span>path<span class=\"token punctuation\">.<\/span>dirname<span class=\"token punctuation\">(<\/span>__file__<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;..&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;src&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><span class=\"token punctuation\">(<\/span>scope<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;function&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">calc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u6bcf\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u4e00\u4e2a\u5168\u65b0\u7684 Calculator \u5b9e\u4f8b&#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\\\\n[setup] \u521b\u5efa Calculator&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    c <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">__import__<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;calculator&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>Calculator<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">yield<\/span> c<br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\\\\n[teardown] \u9500\u6bc1 Calculator&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>tests\/test_add.py&#xff08;\u5e72\u5e72\u51c0\u51c0&#xff0c;\u4e0d\u518d\u5b9a\u4e49 fixture&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_add_int<\/span><span class=\"token punctuation\">(<\/span>calc<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> calc<span class=\"token punctuation\">.<\/span>add<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">3<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_add_negative<\/span><span class=\"token punctuation\">(<\/span>calc<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> calc<span class=\"token punctuation\">.<\/span>add<span class=\"token punctuation\">(<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">3<\/span><\/p>\n<p>tests\/test_mul.py&#xff1a;<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_mul<\/span><span class=\"token punctuation\">(<\/span>calc<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> calc<span class=\"token punctuation\">.<\/span>mul<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">6<\/span><\/p>\n<p>\u8fd0\u884c&#xff1a;<\/p>\n<p>$ pytest tests\/ <span class=\"token parameter variable\">-v<\/span> <span class=\"token parameter variable\">-s<\/span><\/p>\n<p>\u8f93\u51fa&#xff08;\u5173\u952e\u90e8\u5206&#xff09;&#xff1a;<\/p>\n<p>tests\/test_add.py::test_add_int<br \/>\n[setup] \u521b\u5efa Calculator<br \/>\nPASSED<br \/>\n[teardown] \u9500\u6bc1 Calculator<\/p>\n<p>tests\/test_add.py::test_add_negative<br \/>\n[setup] \u521b\u5efa Calculator<br \/>\nPASSED<br \/>\n[teardown] \u9500\u6bc1 Calculator<\/p>\n<p>tests\/test_mul.py::test_mul<br \/>\n[setup] \u521b\u5efa Calculator<br \/>\nPASSED<br \/>\n[teardown] \u9500\u6bc1 Calculator<\/p>\n<p>\u2705 3 \u4e2a\u7528\u4f8b\u5404\u81ea\u62ff\u5230\u72ec\u7acb\u7684 calc&#xff0c;setup\/teardown \u6e05\u6670\u53ef\u89c1&#xff08;-s \u8ba9 print \u8f93\u51fa\u5230\u7ec8\u7aef&#xff09;\u3002<\/p>\n<p>\u770b\u5230\u8fd9\u91cc\u4f60\u5df2\u7ecf\u9a8c\u8bc1\u4e86\u7b2c\u4e00\u53e5\u8bdd&#xff1a;fixture \u4ece conftest \u91cc&#034;\u81ea\u52a8\u51fa\u73b0&#034;\u5728\u6d4b\u8bd5\u51fd\u6570\u53c2\u6570\u91cc&#xff0c;\u65e0\u9700 import\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001Fixture \u5206\u5c42&#xff1a;\u9879\u76ee\u7ea7 vs \u6a21\u5757\u7ea7<\/h3>\n<h4>2.1 \u4e09\u5c42 conftest \u7684\u804c\u8d23\u5212\u5206<\/h4>\n<table>\n<tr>\u5c42\u7ea7\u4f4d\u7f6e\u9002\u5408\u653e\u4ec0\u4e48scope \u5efa\u8bae<\/tr>\n<tbody>\n<tr>\n<td>\u5168\u5c40\u7ea7<\/td>\n<td>\u9879\u76ee\u6839 conftest.py<\/td>\n<td>\u914d\u7f6e\u52a0\u8f7d\u3001\u65e5\u5fd7\u3001\u6d4f\u89c8\u5668\/Appium \u9a71\u52a8\u3001\u6570\u636e\u5e93\u5f15\u64ce<\/td>\n<td>session<\/td>\n<\/tr>\n<tr>\n<td>\u6a21\u5757\u7ea7<\/td>\n<td>tests\/ \u4e0b conftest.py<\/td>\n<td>\u4e1a\u52a1\u901a\u7528\u7684\u6d4b\u8bd5\u6570\u636e\u3001\u5171\u4eab mock<\/td>\n<td>module \/ function<\/td>\n<\/tr>\n<tr>\n<td>\u5b50\u6a21\u5757\u7ea7<\/td>\n<td>tests\/api\/conftest.py<\/td>\n<td>API \u4e13\u5c5e\u7684 client\u3001token\u3001fixture \u7ec4\u5408<\/td>\n<td>\u6309\u9700\u8981<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2.2 \u793a\u4f8b&#xff1a;\u914d\u7f6e \u2192 \u6570\u636e\u5e93 \u2192 \u4e1a\u52a1\u6570\u636e&#xff0c;\u9010\u7ea7\u4f9d\u8d56<\/h4>\n<p>project\/<br \/>\n\u251c\u2500\u2500 conftest.py                 \u2190 \u5168\u5c40&#xff1a;\u52a0\u8f7d\u914d\u7f6e &#043; \u542f\u52a8 DB<br \/>\n\u251c\u2500\u2500 tests\/<br \/>\n\u2502   \u251c\u2500\u2500 conftest.py             \u2190 \u6a21\u5757&#xff1a;\u57fa\u4e8e DB \u6784\u9020\u4e1a\u52a1\u6570\u636e<br \/>\n\u2502   \u2514\u2500\u2500 users\/<br \/>\n\u2502       \u251c\u2500\u2500 conftest.py         \u2190 \u5b50\u6a21\u5757&#xff1a;\u7528\u6237\u76f8\u5173\u7684 fixture<br \/>\n\u2502       \u2514\u2500\u2500 test_user.py<\/p>\n<p><span class=\"token comment\"># \u9879\u76ee\u6839 conftest.py<\/span><br \/>\n<span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><span class=\"token punctuation\">(<\/span>scope<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;session&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">config<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;db_url&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;sqlite:\/\/\/:memory:&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;env&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;test&#034;<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><span class=\"token punctuation\">(<\/span>scope<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;session&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">db_engine<\/span><span class=\"token punctuation\">(<\/span>config<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">from<\/span> sqlalchemy <span class=\"token keyword\">import<\/span> create_engine<br \/>\n    eng <span class=\"token operator\">&#061;<\/span> create_engine<span class=\"token punctuation\">(<\/span>config<span class=\"token punctuation\">[<\/span><span class=\"token string\">&#034;db_url&#034;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">yield<\/span> eng<br \/>\n    eng<span class=\"token punctuation\">.<\/span>dispose<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># tests\/conftest.py<\/span><br \/>\n<span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><span class=\"token punctuation\">(<\/span>scope<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;module&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">db_session<\/span><span class=\"token punctuation\">(<\/span>db_engine<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>      <span class=\"token comment\"># \u2190 \u76f4\u63a5&#034;\u6ce8\u5165&#034;\u4e0a\u5c42 fixture<\/span><br \/>\n    conn <span class=\"token operator\">&#061;<\/span> db_engine<span class=\"token punctuation\">.<\/span>connect<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    tx <span class=\"token operator\">&#061;<\/span> conn<span class=\"token punctuation\">.<\/span>begin<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">yield<\/span> conn<br \/>\n    tx<span class=\"token punctuation\">.<\/span>rollback<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    conn<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># tests\/users\/conftest.py<\/span><br \/>\n<span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">user_factory<\/span><span class=\"token punctuation\">(<\/span>db_session<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">_make<\/span><span class=\"token punctuation\">(<\/span>name<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># &#8230;\u63d2\u5165\u4e00\u6761\u7528\u6237\u8bb0\u5f55&#8230;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;id&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;name&#034;<\/span><span class=\"token punctuation\">:<\/span> name<span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> _make<\/p>\n<p>\u8fd9\u5c31\u662f\u5206\u5c42\u7684\u6838\u5fc3\u4ef7\u503c&#xff1a;db_session \u4f9d\u8d56 db_engine&#xff0c;user_factory \u4f9d\u8d56 db_session\u2014\u2014\u6bcf\u4e00\u5c42\u53ea\u5173\u5fc3&#034;\u6211\u8981\u7528\u4ec0\u4e48&#034;&#xff0c;\u4e0d\u5173\u5fc3&#034;\u5b83\u4ece\u54ea\u6765\u3001\u600e\u4e48\u9020&#034;\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u4f9d\u8d56\u6ce8\u5165&#xff08;DI&#xff09;&#xff1a;pytest \u7684\u6838\u5fc3\u54f2\u5b66<\/h3>\n<h4>3.1 \u4ec0\u4e48\u662f\u4f9d\u8d56\u6ce8\u5165<\/h4>\n<p>\u4f9d\u8d56\u6ce8\u5165&#xff08;Dependency Injection&#xff09;&#xff1a;\u4e00\u4e2a\u5bf9\u8c61\u6240\u9700\u8981\u7684\u4f9d\u8d56&#xff0c;\u7531\u5916\u90e8&#xff08;\u6846\u67b6&#xff09;\u5728\u8fd0\u884c\u65f6&#034;\u6ce8\u5165&#034;\u8fdb\u6765&#xff0c;\u800c\u4e0d\u662f\u81ea\u5df1\u5728\u5185\u90e8 hard-code \u521b\u5efa\u3002<\/p>\n<p>\u5bf9\u6bd4\u4e24\u79cd\u5199\u6cd5&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u274c \u4e0d\u7528 DI&#xff1a;\u6d4b\u8bd5\u81ea\u5df1 new \u4f9d\u8d56&#xff08;\u7d27\u8026\u5408\u3001\u96be\u66ff\u6362&#xff09;<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">test_order<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    db <span class=\"token operator\">&#061;<\/span> MySQLConnection<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span>       <span class=\"token comment\"># \u771f\u5b9e\u6570\u636e\u5e93&#xff0c;\u6162\u4e14\u4e0d\u7a33\u5b9a<\/span><br \/>\n    service <span class=\"token operator\">&#061;<\/span> OrderService<span class=\"token punctuation\">(<\/span>db<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><\/p>\n<p><span class=\"token comment\"># \u2705 \u7528 DI&#xff1a;\u4f9d\u8d56\u7531 fixture \u63d0\u4f9b&#xff08;\u677e\u8026\u5408\u3001\u53ef\u66ff\u6362&#xff09;<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">test_order<\/span><span class=\"token punctuation\">(<\/span>db_session<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>         <span class=\"token comment\"># \u2190 db_session \u4ece\u54ea\u6765&#xff1f;\u4e0d\u7528\u7ba1<\/span><br \/>\n    service <span class=\"token operator\">&#061;<\/span> OrderService<span class=\"token punctuation\">(<\/span>db_session<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><\/p>\n<p>pytest \u7684 DI \u662f&#034;\u58f0\u660e\u5f0f&#034;\u7684&#xff1a;\u4f60\u53ea\u8981\u5728\u53c2\u6570\u91cc\u5199 def test_xxx(db_session):&#xff0c;pytest \u5c31\u4f1a&#xff1a;<\/p>\n<li>\u67e5 fixture \u5b9a\u4e49&#xff08;\u6309\u53c2\u6570\u540d\u5339\u914d&#xff09;<\/li>\n<li>\u9012\u5f52\u89e3\u6790\u5b83\u7684\u4f9d\u8d56&#xff08;db_session \u2192 db_engine \u2192 config&#xff09;<\/li>\n<li>\u6309 scope \u7f13\u5b58 \/ \u65b0\u5efa\u5b9e\u4f8b<\/li>\n<li>\u8c03\u7528\u4f60\u7684\u6d4b\u8bd5\u51fd\u6570&#xff0c;\u628a\u5b9e\u4f8b\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165<\/li>\n<p>\u4f60\u53ea\u58f0\u660e&#034;\u6211\u9700\u8981\u4ec0\u4e48&#034;&#xff0c;\u4e0d\u5199&#034;\u600e\u4e48\u9020&#034;\u3002 \u8fd9\u5c31\u662f pytest \u88ab\u79f0\u4e3a&#034;\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6&#034;\u7684\u539f\u56e0\u3002<\/p>\n<h4>3.2 \u81ea\u52a8\u53d1\u73b0 &#043; \u7c7b\u578b\u65e0\u5173<\/h4>\n<p>\u6ce8\u610f\u4e00\u4e2a\u7ec6\u8282&#xff1a;pytest \u4e0d\u8981\u6c42 fixture \u6709\u7c7b\u578b\u6ce8\u89e3&#xff0c;\u5b8c\u5168\u9760\u540d\u5b57\u5339\u914d\u3002\u8fd9\u65e2\u662f\u4f18\u70b9&#xff08;\u7075\u6d3b&#xff09;\u4e5f\u662f\u5751\u70b9&#xff08;\u540d\u5b57\u6253\u9519\u5c31\u6536\u96c6\u4e0d\u5230&#xff09;\u3002<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_foo<\/span><span class=\"token punctuation\">(<\/span>calc<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>        <span class=\"token comment\"># \u2705 \u53c2\u6570\u540d &#061; fixture \u540d&#xff0c;\u81ea\u52a8\u6ce8\u5165<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">test_bar<\/span><span class=\"token punctuation\">(<\/span>calculator<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>   <span class=\"token comment\"># \u274c \u540d\u5b57\u5bf9\u4e0d\u4e0a \u2192 fixture &#039;calculator&#039; not found<\/span><\/p>\n<p>&#x1f4a1; \u540d\u5b57\u6253\u9519\u662f\u6700\u5e38\u89c1\u7684 conftest \u62a5\u9519\u3002\u8bb0\u4f4f&#xff1a;\u53c2\u6570\u540d\u5fc5\u987b &#061;&#061; fixture \u51fd\u6570\u540d\u3002<\/p>\n<h4>3.3 \u4f9d\u8d56\u6ce8\u5165\u7684\u5b9e\u6218\u597d\u5904<\/h4>\n<p>\u2460 \u6d4b\u8bd5\u5373\u6587\u6863&#xff1a;\u770b\u53c2\u6570\u5217\u8868\u5c31\u77e5\u9053\u8fd9\u4e2a\u7528\u4f8b\u4f9d\u8d56\u54ea\u4e9b\u8d44\u6e90\u3002<\/p>\n<p>\u2461 \u8f7b\u677e\u66ff\u6362\u5b9e\u73b0&#xff08;Mock&#xff09;&#xff1a;<\/p>\n<p><span class=\"token comment\"># tests\/conftest.py<\/span><br \/>\n<span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">payment_gateway<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u771f\u5b9e\u73af\u5883\u7528\u652f\u4ed8\u5b9d&#xff0c;\u6d4b\u8bd5\u73af\u5883\u7528\u4e00\u4e2a\u5047\u5b9e\u73b0<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> FakePaymentGateway<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>   <span class=\"token comment\"># \u6c38\u8fdc\u8fd4\u56de\u6210\u529f<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_checkout<\/span><span class=\"token punctuation\">(<\/span>payment_gateway<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>   <span class=\"token comment\"># \u2190 \u7528\u7684\u662f Fake&#xff0c;\u4e0d\u6263\u771f\u94b1<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> checkout<span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">,<\/span> payment_gateway<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>ok<\/p>\n<p>\u2462 \u5e76\u884c\u5b89\u5168\u7684\u57fa\u7840&#xff1a;\u56e0\u4e3a\u6bcf\u4e2a worker \u62ff\u5230\u7684\u662f\u81ea\u5df1 scope \u5185\u7684\u5b9e\u4f8b&#xff0c;\u5f7c\u6b64\u9694\u79bb\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001Fixture \u4f9d\u8d56\u56fe&#xff1a;\u770b\u6e05\u8c01\u4f9d\u8d56\u8c01<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260908230932-6aa095ac64470.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u25b2 Fixture \u4f9d\u8d56\u5173\u7cfb&#xff08;\u5012\u7f6e\u6811&#xff09;&#xff1a;config \u2192 db_engine \u2192 db_session \u2192 user_factory&#xff0c;calc \u4f9d\u8d56 config&#xff0c;\u7bad\u5934\u6307\u5411\u88ab\u4f9d\u8d56\u65b9\u3002<\/p>\n<p>\u4e0a\u56fe\u5c55\u793a\u4e86\u7b2c 2~4 \u7bc7 fixture \u4f53\u7cfb\u7684\u5b8c\u6574\u4f9d\u8d56\u5173\u7cfb&#xff1a;<\/p>\n<ul>\n<li>config&#xff08;session&#xff09;\u2192 db_engine&#xff08;session&#xff09;\u2192 db_session&#xff08;module&#xff09;\u2192 user_factory&#xff08;function&#xff09;<\/li>\n<li>\u7bad\u5934\u65b9\u5411 &#061; \u4f9d\u8d56\u65b9\u5411&#xff08;A \u2192 B \u8868\u793a A \u4f9d\u8d56 B&#xff09;<\/li>\n<li>\u8d8a\u9760\u8fd1\u5e95\u90e8&#xff0c;scope \u8d8a\u77ed\u3001\u751f\u547d\u5468\u671f\u8d8a\u77ed<\/li>\n<\/ul>\n<p>\u8fd9\u5f20\u56fe\u8981\u8bb0\u4f4f\u4e00\u53e5\u8bdd&#xff1a;fixture \u7684\u4f9d\u8d56\u56fe&#xff0c;\u5c31\u662f\u4e00\u68f5&#034;\u5012\u7f6e\u7684\u6811&#034;&#xff0c;\u6839\u662f\u914d\u7f6e&#xff0c;\u53f6\u662f\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001conftest \u8fdb\u9636\u7528\u6cd5<\/h3>\n<h4>5.1 fixture \u91cd\u547d\u540d&#xff1a;&#064;pytest.fixture(name&#061;&#8230;)<\/h4>\n<p><span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><span class=\"token punctuation\">(<\/span>name<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;db&#034;<\/span><span class=\"token punctuation\">)<\/span>     <span class=\"token comment\"># \u2190 \u6d4b\u8bd5\u91cc\u7528 db&#xff0c;\u800c\u4e0d\u662f\u957f\u957f\u7684 fixture \u540d<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">database_connection<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_xxx<\/span><span class=\"token punctuation\">(<\/span>db<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>          <span class=\"token comment\"># \u2705 \u7528\u522b\u540d<\/span><\/p>\n<h4>5.2 \u5e26\u53c2\u6570\u7684 fixture&#xff1a;params &#043; ids<\/h4>\n<p><span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><span class=\"token punctuation\">(<\/span>params<span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">&#034;chrome&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;firefox&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;edge&#034;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> ids<span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">lambda<\/span> b<span class=\"token punctuation\">:<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;browser&#061;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>b<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">browser<\/span><span class=\"token punctuation\">(<\/span>request<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">yield<\/span> request<span class=\"token punctuation\">.<\/span>param<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">test_ui<\/span><span class=\"token punctuation\">(<\/span>browser<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> browser <span class=\"token keyword\">in<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;chrome&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;firefox&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;edge&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u8fd0\u884c\u4f1a\u4ea7\u751f 3 \u4e2a\u7528\u4f8b&#xff1a;test_ui[browser&#061;chrome] \/ [browser&#061;firefox] \/ [browser&#061;edge]\u3002<\/p>\n<h4>5.3 autouse&#061;True&#xff1a;\u81ea\u52a8\u751f\u6548\u7684 fixture<\/h4>\n<p><span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><span class=\"token punctuation\">(<\/span>autouse<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">,<\/span> scope<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;function&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">log_case_start<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;&gt;&gt;&gt; \u7528\u4f8b\u5f00\u59cb&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">yield<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;&lt;&lt;&lt; \u7528\u4f8b\u7ed3\u675f&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u6bcf\u4e2a\u7528\u4f8b\u81ea\u52a8\u5957\u7528&#xff0c;\u65e0\u9700\u5199\u5728\u53c2\u6570\u91cc\u3002 \u614e\u7528\u2014\u2014\u5b83\u4f1a\u9690\u5f0f\u5f71\u54cd\u6240\u6709\u6d4b\u8bd5&#xff0c;\u9002\u5408\u505a\u65e5\u5fd7\u3001\u8ba1\u65f6\u3001\u6e05\u7406\u4e34\u65f6\u72b6\u6001\u3002<\/p>\n<h4>5.4 \u52a8\u6001\u5207\u6362\u5b9e\u73b0&#xff1a;\u7ed3\u5408 &#064;pytest.mark.parametrize<\/h4>\n<p><span class=\"token decorator annotation punctuation\">&#064;pytest<span class=\"token punctuation\">.<\/span>fixture<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">gateway<\/span><span class=\"token punctuation\">(<\/span>request<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> request<span class=\"token punctuation\">.<\/span>config<span class=\"token punctuation\">.<\/span>getoption<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;&#8211;real&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> RealGateway<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> FakeGateway<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u547d\u4ee4\u884c &#8211;real \u65f6\u5207\u771f\u5b9e\u5b9e\u73b0&#xff0c;\u5426\u5219\u7528\u5047\u5b9e\u73b0\u2014\u2014\u4e00\u5957\u6d4b\u8bd5\u8dd1\u4e24\u79cd\u73af\u5883\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001\u5e38\u89c1\u8e29\u5751\u4e0e\u6392\u9519<\/h3>\n<table>\n<tr>\u95ee\u9898\u539f\u56e0\u89e3\u51b3<\/tr>\n<tbody>\n<tr>\n<td>fixture &#039;xxx&#039; not found<\/td>\n<td>\u53c2\u6570\u540d \u2260 fixture \u540d \/ \u4e0d\u5728 conftest \u94fe\u4e0a<\/td>\n<td>\u68c0\u67e5\u62fc\u5199\u3001conftest \u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>fixture \u6ca1\u88ab\u590d\u7528&#xff08;\u6bcf\u6b21\u90fd\u65b0\u5efa&#xff09;<\/td>\n<td>scope \u8bbe\u6210\u4e86 function<\/td>\n<td>\u6309\u9700\u6539\u4e3a module\/session<\/td>\n<\/tr>\n<tr>\n<td>session fixture \u72b6\u6001\u4e32\u5473<\/td>\n<td>\u8de8\u7528\u4f8b\u5171\u4eab\u53ef\u53d8\u5bf9\u8c61<\/td>\n<td>session \u7ea7\u53ea\u7528\u4e0d\u53ef\u53d8\/\u8fde\u63a5\u6c60&#xff0c;\u6570\u636e\u7528 module\/function<\/td>\n<\/tr>\n<tr>\n<td>conftest \u91cc\u6539\u4e86\u4e0d\u751f\u6548<\/td>\n<td>pytest \u7f13\u5b58 \/ IDE \u6ca1\u8bc6\u522b\u6839\u76ee\u5f55<\/td>\n<td>\u5220\u9664 __pycache__\u3001\u8bbe\u4e3a pytest rootdir<\/td>\n<\/tr>\n<tr>\n<td>\u5faa\u73af\u4f9d\u8d56<\/td>\n<td>A \u4f9d\u8d56 B&#xff0c;B \u53c8\u4f9d\u8d56 A<\/td>\n<td>\u62c6\u5206\u516c\u5171\u90e8\u5206\u5230\u7b2c\u4e09\u4e2a fixture<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f \u6700\u91cd\u8981\u7684\u63d0\u9192&#xff1a;session \u7ea7 fixture \u662f&#034;\u5168\u5c40\u5355\u4f8b&#034;&#xff0c;\u5343\u4e07\u4e0d\u8981\u5728\u91cc\u9762\u5b58\u53ef\u53d8\u6d4b\u8bd5\u6570\u636e\u2014\u2014\u5426\u5219\u7528\u4f8b\u95f4\u4f1a\u4e92\u76f8\u6c61\u67d3&#xff0c;\u8c03\u8bd5\u8d77\u6765\u6781\u5176\u75db\u82e6\u3002<\/p>\n<hr \/>\n<h3>\u4e03\u3001\u5b8c\u6574\u8fd0\u884c\u6548\u679c\u793a\u4f8b<\/h3>\n<p>$ pytest tests\/ -v<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; test session starts &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\ncollected 5 items<\/p>\n<p>tests\/test_add.py::test_add_int PASSED                                        [ 20%]<br \/>\ntests\/test_add.py::test_add_negative PASSED                                    [ 40%]<br \/>\ntests\/test_sub.py::test_sub PASSED                                             [ 60%]<br \/>\ntests\/test_mul.py::test_mul PASSED                                             [ 80%]<br \/>\ntests\/test_div.py::test_div_by_zero PASSED                                     [100%]<\/p>\n<p>&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; 5 passed in 0.08s &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<\/p>\n<p>\u2705 5 \u4e2a\u7528\u4f8b\u5168\u90e8\u901a\u8fc7&#xff0c;fixture \u4ece conftest \u81ea\u52a8\u6ce8\u5165\u2014\u2014\u6d4b\u8bd5\u6587\u4ef6\u91cc\u6ca1\u6709\u4efb\u4f55 setup \u4ee3\u7801\u3002<\/p>\n<hr \/>\n<h3>\u516b\u3001\u672c\u7bc7\u5c0f\u7ed3<\/h3>\n<ul>\n<li>\u2705 conftest.py \u662f fixture \u7684\u5171\u4eab\u5c42&#xff0c;\u5c31\u8fd1\u8986\u76d6\u3001\u81ea\u52a8\u53d1\u73b0\u3001\u65e0\u9700 import<\/li>\n<li>\u2705 Fixture \u5206\u4e09\u5c42&#xff1a;\u5168\u5c40&#xff08;session&#xff09;\u2192 \u6a21\u5757&#xff08;module&#xff09;\u2192 \u5b50\u6a21\u5757&#xff0c;\u804c\u8d23\u6e05\u6670<\/li>\n<li>\u2705 \u4f9d\u8d56\u6ce8\u5165&#xff1a;\u6d4b\u8bd5\u53ea\u58f0\u660e&#034;\u8981\u4ec0\u4e48&#034;&#xff0c;pytest \u8d1f\u8d23&#034;\u600e\u4e48\u9020&#034;&#xff0c;\u9760\u540d\u5b57\u5339\u914d<\/li>\n<li>\u2705 \u8fdb\u9636\u6280\u5de7&#xff1a;name \u91cd\u547d\u540d\u3001params \u53c2\u6570\u5316\u3001autouse \u81ea\u52a8\u751f\u6548<\/li>\n<li>\u2705 \u907f\u5751&#xff1a;session \u7ea7\u4e0d\u5b58\u53ef\u53d8\u72b6\u6001&#xff0c;\u53c2\u6570\u540d\u5fc5\u987b &#061;&#061; fixture \u540d<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e5d\u3001\u52a8\u624b\u7ec3\u4e60<\/h3>\n<li>\u5728 tests\/conftest.py \u52a0\u4e00\u4e2a tmp_db fixture&#xff08;tmp_path &#043; \u5185\u5b58\u6570\u636e\u5e93&#xff09;&#xff0c;\u8ba9\u6240\u6709\u7528\u4f8b\u5171\u4eab<\/li>\n<li>\u65b0\u589e tests\/api\/conftest.py&#xff0c;\u5b9a\u4e49\u4e00\u4e2a\u53ea\u7ed9 API \u6d4b\u8bd5\u7528\u7684 client fixture<\/li>\n<li>\u5199\u4e00\u4e2a\u4f9d\u8d56 client \u7684\u7528\u4f8b&#xff0c;\u9a8c\u8bc1\u5b50\u6a21\u5757 conftest \u7684\u5c31\u8fd1\u539f\u5219<\/li>\n<li>\u628a calc \u7684 scope \u6539\u6210 module&#xff0c;\u89c2\u5bdf setup\/teardown \u6b21\u6570\u53d8\u5316<\/li>\n<hr \/>\n<h3>\u5341\u3001\u601d\u8003\u9898&#xff08;\u6b22\u8fce\u8bc4\u8bba\u533a\u8ba8\u8bba&#xff09;<\/h3>\n<li>\u4ec0\u4e48\u65f6\u5019\u8be5\u628a fixture \u653e conftest&#xff0c;\u4ec0\u4e48\u65f6\u5019\u7559\u5728\u6d4b\u8bd5\u6587\u4ef6\u5185&#xff1f;<\/li>\n<li>session scope \u7684 fixture \u4e3a\u4ec0\u4e48&#034;\u5371\u9669&#034;&#xff1f;\u5982\u4f55\u5b89\u5168\u4f7f\u7528&#xff1f;<\/li>\n<li>pytest \u7684 DI \u548c\u4f60\u7528\u8fc7\u7684\u5176\u4ed6\u6846\u67b6&#xff08;Spring \/ Dagger&#xff09;\u6709\u4f55\u5f02\u540c&#xff1f;<\/li>\n<hr \/>\n<h3>\u4e0b\u4e00\u7bc7\u9884\u544a<\/h3>\n<p>&#x1f449; \u7b2c 5 \u7bc7\u300apytest \u63d2\u4ef6\u751f\u6001\u4e0e Allure \u6d4b\u8bd5\u62a5\u544a\u300b<\/p>\n<p>\u4f60\u5c06\u5b66\u5230&#xff1a;<\/p>\n<ul>\n<li>\u5e38\u7528\u63d2\u4ef6&#xff08;pytest-cov \/ pytest-xdist \/ pytest-html&#xff09;<\/li>\n<li>\u5982\u4f55\u7528 pytest &#8211;plugins \u7ba1\u7406\u63d2\u4ef6<\/li>\n<li>Allure \u62a5\u544a\u751f\u6210\u4e0e\u7f8e\u5316<\/li>\n<li>CI \u4e2d\u96c6\u6210\u6d4b\u8bd5\u62a5\u544a<\/li>\n<\/ul>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u4e13\u680f&#xff1a;Python \u81ea\u52a8\u5316\u6d4b\u8bd5\u4ece\u5165\u95e8\u5230\u5b9e\u6218 \u00b7 \u7b2c 4 \u7bc7 \u4e0a\u63a5&#xff1a;\u7b2c 3 \u7bc7\u300apytest \u53c2\u6570\u5316\u4e0e Mark \u6807\u8bb0\u300b \u4e0b\u63a5&#xff1a;\u7b2c 5 \u7bc7\u300apytest \u63d2\u4ef6\u751f\u6001\u4e0e Allure \u62a5\u544a\u300b&#xff08;\u9884\u544a&#xff09; \u9002\u8bfb\u4eba\u7fa4&#xff1a;\u5df2\u638c\u63e1 Fixture \u57fa\u7840&#xff08;scope \/ yield&#xff09;\u7684 pytest \u4f7f\u7528\u8005 \u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u7b2c 2 \u7bc7\u4e0d\u591f\u7528&#xff1f;<br \/>\n\u7b2c 2 \u7bc7\u91cc&amp;#x<\/p>\n","protected":false},"author":2,"featured_media":102559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4327],"topic":[],"class_list":["post-102560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-pytest"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>pytest Fixture \u8fdb\u9636\uff1aconftest \u4e0e\u4f9d\u8d56\u6ce8\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/102560.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"pytest Fixture \u8fdb\u9636\uff1aconftest \u4e0e\u4f9d\u8d56\u6ce8\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e13\u680f&#xff1a;Python \u81ea\u52a8\u5316\u6d4b\u8bd5\u4ece\u5165\u95e8\u5230\u5b9e\u6218 \u00b7 \u7b2c 4 \u7bc7 \u4e0a\u63a5&#xff1a;\u7b2c 3 \u7bc7\u300apytest \u53c2\u6570\u5316\u4e0e Mark \u6807\u8bb0\u300b \u4e0b\u63a5&#xff1a;\u7b2c 5 \u7bc7\u300apytest \u63d2\u4ef6\u751f\u6001\u4e0e Allure \u62a5\u544a\u300b&#xff08;\u9884\u544a&#xff09; \u9002\u8bfb\u4eba\u7fa4&#xff1a;\u5df2\u638c\u63e1 Fixture \u57fa\u7840&#xff08;scope \/ yield&#xff09;\u7684 pytest \u4f7f\u7528\u8005 \u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u7b2c 2 \u7bc7\u4e0d\u591f\u7528&#xff1f; \u7b2c 2 \u7bc7\u91cc&amp;#x\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/102560.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-08T23:09:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260908230932-6aa095ac64470.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/102560.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/102560.html\",\"name\":\"pytest Fixture \u8fdb\u9636\uff1aconftest \u4e0e\u4f9d\u8d56\u6ce8\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-08T23:09:34+00:00\",\"dateModified\":\"2026-09-08T23:09:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/102560.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/102560.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/102560.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"pytest Fixture \u8fdb\u9636\uff1aconftest \u4e0e\u4f9d\u8d56\u6ce8\u5165\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"pytest Fixture \u8fdb\u9636\uff1aconftest \u4e0e\u4f9d\u8d56\u6ce8\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/102560.html","og_locale":"zh_CN","og_type":"article","og_title":"pytest Fixture \u8fdb\u9636\uff1aconftest \u4e0e\u4f9d\u8d56\u6ce8\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e13\u680f&#xff1a;Python \u81ea\u52a8\u5316\u6d4b\u8bd5\u4ece\u5165\u95e8\u5230\u5b9e\u6218 \u00b7 \u7b2c 4 \u7bc7 \u4e0a\u63a5&#xff1a;\u7b2c 3 \u7bc7\u300apytest \u53c2\u6570\u5316\u4e0e Mark \u6807\u8bb0\u300b \u4e0b\u63a5&#xff1a;\u7b2c 5 \u7bc7\u300apytest \u63d2\u4ef6\u751f\u6001\u4e0e Allure \u62a5\u544a\u300b&#xff08;\u9884\u544a&#xff09; \u9002\u8bfb\u4eba\u7fa4&#xff1a;\u5df2\u638c\u63e1 Fixture \u57fa\u7840&#xff08;scope \/ yield&#xff09;\u7684 pytest \u4f7f\u7528\u8005 \u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u7b2c 2 \u7bc7\u4e0d\u591f\u7528&#xff1f; \u7b2c 2 \u7bc7\u91cc&amp;#x","og_url":"https:\/\/www.wsisp.com\/helps\/102560.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-08T23:09:34+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260908230932-6aa095ac64470.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/102560.html","url":"https:\/\/www.wsisp.com\/helps\/102560.html","name":"pytest Fixture \u8fdb\u9636\uff1aconftest \u4e0e\u4f9d\u8d56\u6ce8\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-08T23:09:34+00:00","dateModified":"2026-09-08T23:09:34+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/102560.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/102560.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/102560.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"pytest Fixture \u8fdb\u9636\uff1aconftest \u4e0e\u4f9d\u8d56\u6ce8\u5165"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/102560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=102560"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/102560\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/102559"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=102560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=102560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=102560"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=102560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}