{"id":4667,"date":"2022-03-27T18:39:23","date_gmt":"2022-03-27T10:39:23","guid":{"rendered":"https:\/\/egonlin.com\/?p=4667"},"modified":"2022-03-27T18:39:23","modified_gmt":"2022-03-27T10:39:23","slug":"13%ef%bc%9apytest%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=4667","title":{"rendered":"13\uff1apytest\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p>04-13pytest\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u6709\u4e86unittest\u8fd9\u4e2a\u7ecf\u5178\u7684\u6d4b\u8bd5\u6846\u67b6\u505a\u94fa\u57ab\uff0c\u90a3\u4e48\u5b66\u4e60\u5176\u4ed6\u4efb\u4f55\u7684\u6d4b\u8bd5\u6846\u67b6\u90fd\u53d8\u5f97\u6709\u7ae0\u6cd5\u53ef\u5faa\u4e86\u3002<\/p>\n<p>pytest\u6d4b\u8bd5\u6846\u67b6\u4e5f\u662f\u7531unittest\u6539\u7f16\u800c\u6765\uff0c\u6240\u4ee5\u8bb8\u591a\u5730\u65b9\u90fd\u662f\u4e00\u8109\u76f8\u627f\u3002<\/p>\n<p>\u6211\u76f8\u4fe1\u8bb8\u591a\u8bfb\u8005\u518d\u770b\u4e86unittest\u7684\u6587\u7ae0\u4e4b\u540e\uff0c\u5df2\u4e0d\u9700\u8981\u8017\u8d39\u8111\u7ec6\u80de\u5c31\u53ef\u4ee5\u628apytest\u7684\u4f7f\u7528\u638c\u63e1\u4e86\u3002\u4f60\u662f\u4e0d\u662f\u4e5f\u662f\u5176\u4e2d\u4e00\u4e2a\u5462\uff1f<\/p>\n<h2>\u6b63\u6587:<\/h2>\n<h3>1   \u65ad\u8a00<\/h3>\n<h4>pytest\u76f4\u63a5\u4f7f\u7528 assert   \u8fdb\u884c\u65ad\u8a00<\/h4>\n<p>\u200b    .\\test_assert.py<\/p>\n<pre><code class=\"language-python\"># content of test_assert.py\n\n# \u529f\u80fd\u51fd\u6570\ndef func_number():\n    return 3\n\n# \u6d4b\u8bd5\u51fd\u6570\ndef test_function():\n    assert func_number() == 3\n<\/code><\/pre>\n<p>\u6211\u4eec\u628a\u811a\u672c\u8fd0\u884c\u4e00\u4e0b\uff1a <\/p>\n<blockquote>\n<pre><code class=\"language-vb\">...> pytest Stage5\\07pytest\\pytest test_assert.py -v\n####   -v \u53c2\u6570\u80fd\u628a\u7ed3\u679c\u6253\u5370\u7684\u66f4\u8be6\u7ec6 \n####   -v, --verbose         increase verbosity.\n<\/code><\/pre>\n<\/blockquote>\n<p>\u60f3\u8981\u77e5\u9053\u66f4\u591apytest\u7684\u53c2\u6570\u8f93\u5165pytest &#8211;help<\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-vb\">(testops) D:\\Coding\\Project\\testops\\Stage5\\07pytest&gt;pytest test_assert.py -v\n======================================================== test session starts ========================================================\nplatform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 -- d:\\python\\virtualenvs\\testops\\scripts\\python.exe\ncachedir: .pytest_cache\nrootdir: D:\\Coding\\Project\\testops\\Stage5\\07pytest\ncollected 1 item                                                                                                                     \n\ntest_assert.py::test_function PASSED                                                                                           [100%]\n\n========================================================= 1 passed in 0.01s =========================================================\n\n(testops) D:\\Coding\\Project\\testops\\Stage5\\07pytest&gt;\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u7684\u8fd0\u884c\u7ed3\u679c\u770b\u4e0d\u5230\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u90a3\u4f60\u9700\u8981\u52a0\u4e0a -v \u53c2\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u662f\u5173\u4e8epytest\u65ad\u8a00\u7684\u629b\u7816\u5f15\u7389\uff0c\u5c31\u5728\u8fd9\u7ae0\u5176\u4ed6\u5c0f\u8282\u8fd8\u6709\u5176\u4ed6\u5177\u4f53\u7684\u4f7f\u7528\u8bf4\u660e\uff0c\u5728\u4e0b\u4e00\u7ae0\u8282\u8fd8\u6709\u5b9e\u6218\u4e3e\u4f8b\u8bf4\u660e\u3002<\/p>\n<h3>2  Fixture<\/h3>\n<p>fixture\u662f\u6d4b\u8bd5\u811a\u624b\u67b6\u7684\u610f\u601d\u3002\u8fd8\u8bb0\u5f97\u5728unittest\u7684\u6587\u7ae0\u8bf4\u8d77\u7684  \u53a8\u623f\u811a\u624b\u67b6\u3002<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/pytest\u7684fixture\u8be6\u89e3.gif'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/pytest\u7684fixture\u8be6\u89e3.gif\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>\u4e0a\u6709  \u62bd\u6cb9\u70df\u673a\u9876 \uff0c\u4e0b\u6e38\u70e7\u706b\u7684\u7076\u53f0 \uff0c\u4e2d\u95f4\u5c31\u662f\u53a8\u5e08\u7684\u6447\u6446\u7a7a\u95f4\u3002<\/p>\n<p>\u200b    \u5728pytest\u4e2d\uff0cfixture\u7684\u4f5c\u7528\u5f97\u5230\u4e86\u6539\u8fdb\uff0c\u4e0d\u518d\u90a3\u4e48\u56fa\u5b9a\u548c\u7b28\u91cd\uff0c\u5df2\u7ecf\u53d8\u5f97 \u53ef\u4ee5\u79fb\u52a8\u3002<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/pytest\u7684fixture\u8be6\u89e32.gif'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/pytest\u7684fixture\u8be6\u89e32.gif\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>.\\test_pytestfixture.py<\/p>\n<pre><code class=\"language-python\">#fixture\u7684\u7b80\u5355\u4f8b\u5b50\nimport pytest\n\n@pytest.fixture()\ndef func_isequal():\n    a = &#039;robert&#039;\n    return a\n\ndef test_equalrobert(func_isequal):\n    assert func_isequal == &#039;robert&#039;\n\nif __name__ == &#039;__main__&#039;:\n    pytest.main(&quot;-v -s test_pytestfixture.py&quot;)<\/code><\/pre>\n<p>\u200b   \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-vbscript\">(testops) D:\\Coding\\Project\\testops\\Stage5\\07pytest&gt;pytest test_pytestfixture.py -v -s\n======================================================== test session starts ========================================================\nplatform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 -- d:\\python\\virtualenvs\\testops\\scripts\\python.exe\ncachedir: .pytest_cache\nrootdir: D:\\Coding\\Project\\testops\\Stage5\\07pytest\ncollected 1 item                                                                                                                     \n\ntest_pytestfixture.py::test_equalrobert PASSED\n\n========================================================= 1 passed in 0.05s =========================================================\n<\/code><\/pre>\n<h6>\u5b9a\u4e49\u811a\u624b\u67b6\u7684\u65f6\u5019\u8981\u7528@pytest.fixture()\u5173\u952e\u5b57<\/h6>\n<h3>3 \u53c2\u6570\u5316<\/h3>\n<p>\u770b\u6587\u7ae0\u7684\u4f60\u8111\u888b\u91cc\u9762\u4f1a\u4e0d\u4f1a\u5728\u60f3\uff0c\u53c2\u6570\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u6211\u60f3\uff0c\u5e94\u8be5\u6ca1\u6709\u3002\u5982\u679c\u6709\u7684\u8bdd\uff0c\u90a3\u4f60\u8fd8\u662f\u52a0\u6211\u8054\u7cfb\u65b9\u5f0f\uff0c\u5355\u72ec\u804a\uff0c\u56e0\u4e3a\u8fd9\u4e1c\u897f\u53ea\u53ef\u610f\u4f1a\u4e0d\u53ef\u8a00\u4f20\u3002\u6b64\u65f6\u65c1\u8fb9\u7684\u4eba\u90fd\u53d1\u51fa\u4e86\u7b11\u58f0\u3002<\/p>\n<p>.\\test_parameterized.py<\/p>\n<pre><code class=\"language-python\"># content  test_parameterized.py\nimport pytest\nimport math\n\n@pytest.mark.parametrize(\n    &quot;base,exponet,expected&quot;,\n    [(2,3,8),\n     (2,4,16),\n     (2,5,32),\n     (2,6,64)],\n    ids =[&quot;case1_**3&quot;,&quot;case2_**4&quot;,&quot;case3_**5&quot;,&quot;case4_**6&quot;]\n)\ndef test_pow(base,exponet,expected):\n    assert math.pow(base,exponet) ==expected<\/code><\/pre>\n<h6>pytest\u7684\u53c2\u6570\u5316\u9700\u8981\u7528\u5230@pytest.mark.parametrize() \u5173\u952e\u5b57<\/h6>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4f20\u4e863\u4e2a\u53c2\u6570 argnames, argvalues, ids<\/p>\n<p>\u5206\u522b\u4ee3\u8868\uff0c\u53c2\u6570\u540d\u79f0\uff0c\u53c2\u6570\u503c\uff0c\u7528\u4f8b\u540d\u79f0<\/p>\n<p>\u8fd9\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u662f\u5bf9\u5e42\u8fd0\u7b97\u7684\u7ed3\u679c\u8fdb\u884c\u9a8c\u8bc1\uff0c\u4f8b\u5982  (2,3,8) \u662f \u6307 \u5e95\u6570\u662f2\uff0c\u6307\u6570\u662f3\uff0c\u9884\u671f\u7ed3\u679c\u662f8 .<\/p>\n<p>\u5176\u4e2d\u7528\u5230\u4e86python \u5185\u7f6emath\u6a21\u5757\u7684pow\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-python\">(testops) D:\\Coding\\Project\\testops\\Stage5\\07pytest&gt;pytest test_parameterized.py -v\n======================================================== test session starts ========================================================\nplatform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 -- d:\\python\\virtualenvs\\testops\\scripts\\python.exe\ncachedir: .pytest_cache\nrootdir: D:\\Coding\\Project\\testops\\Stage5\\07pytest\ncollected 4 items                                                                                                                    \n\ntest_parameterized.py::test_pow[case1_**3] PASSED                                                                              [ 25%]\ntest_parameterized.py::test_pow[case2_**4] PASSED                                                                              [ 50%]\ntest_parameterized.py::test_pow[case3_**5] PASSED                                                                              [ 75%]\ntest_parameterized.py::test_pow[case4_**6] PASSED                                                                              [100%]\n\n========================================================= 4 passed in 0.02s =========================================================\n<\/code><\/pre>\n<h3>4 \u8fd0\u884c\u6d4b\u8bd5<\/h3>\n<p>\u524d\u9762\u6211\u4eec\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u90fd\u662f\u5728terminal\u4e0b\uff0c\u8f93\u5165\u547d\u4ee4 pytest test_*.py\u6765\u8fd0\u884c\u3002<\/p>\n<p>\u5b9e\u9645\u4e0apytest\u63d0\u4f9b\u4e86\u4e0d\u5c11\u5176\u4ed6\u8fd0\u884c\u6307\u4ee4\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7pytest &#8211;help\u67e5\u770b\u3002<\/p>\n<h5>\u8fd9\u91cc\u5217\u51fa\u5e38\u7528\u7684\u4e00\u4e9b\u6307\u4ee4\uff1a<\/h5>\n<p>-k  \u8fd0\u884c\u540d\u79f0\u4e2d\u5305\u542b\u67d0\u5b57\u7b26\u7684\u6d4b\u8bd5\u7528\u4f8b<\/p>\n<p>-q \u51cf\u5c11\u6d4b\u8bd5\u7684\u8fd0\u884c\u5197\u957f<\/p>\n<p>-x \u5982\u679c\u51fa\u73b0\u4e00\u5929\u6d4b\u8bd5\u7528\u4f8b\u5931\u8d25,\u5219\u9000\u51fa\u6d4b\u8bd5<\/p>\n<p>-v \u8f93\u51fa\u66f4\u8be6\u7ec6\u7684\u65e5\u5fd7\u4fe1\u606f<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u6d4b\u8bd5py\u6587\u4ef6<\/p>\n<p>.\\test_runrule.py<\/p>\n<pre><code class=\"language-python\"># \u8fd0\u884c\u6d4b\u8bd5\u7684\u6848\u4f8b\u8bf4\u660e\u6a21\u5757\n\n# \u529f\u80fd\u51fd\u6570\n\ndef func(x,y):\n    return x+y &gt;10\n\ndef func_math(x,y):\n    import math\n    return math.fabs(x)\n\ndef test_isnottrue():\n    assert func(3,3) is True\n\ndef test_func_math():\n    assert func_math(3.333,6.222) &gt;0<\/code><\/pre>\n<p>\u200b    \u6211\u4eec\u8fd0\u884c\u4e0a\u9762\u7684test_runrule.py <\/p>\n<p>1.\u8fd0\u884c\u547d\u4ee4\uff1a pytest  test_runrule.py <\/p>\n<pre><code class=\"language-vbscript\">(testops) D:\\Coding\\Project\\testops\\Stage5\\07pytest&gt;pytest  test_runrule.py\n======================================================== test session starts ========================================================\nplatform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0\nrootdir: D:\\Coding\\Project\\testops\\Stage5\\07pytest\ncollected 2 items                                                                                                                    \n\ntest_runrule.py F.                                                                                                             [100%]\n\n============================================================= FAILURES ==============================================================\n__________________________________________________________ test_isnottrue ___________________________________________________________\n\n    def test_isnottrue():\n>       assert func(3,3) is True\nE       assert False is True\nE        +  where False = func(3, 3)\n\ntest_runrule.py:13: AssertionError\n==================================================== 1 failed, 1 passed in 0.03s ====================================================\n<\/code><\/pre>\n<h3>5  \u751f\u6210\u6d4b\u8bd5\u62a5\u544a<\/h3>\n<h6>pytest\u652f\u6301\u751f\u6210\u591a\u79cd\u683c\u5f0f\u7684\u6d4b\u8bd5\u62a5\u544a\u3002<\/h6>\n<p>\u8fd0\u884c\u547d\u4ee4\uff1a pytest test_runrule.py &#8211;junit-xml=.\/report\/log.xml<\/p>\n<pre><code class=\"language-vbscript\">(testops) D:\\Coding\\Project\\testops\\Stage5\\07pytest&gt;pytest test_runrule.py --junit-xml=.\/report\/log.xml\n======================================================== test session starts ========================================================\nplatform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0\nrootdir: D:\\Coding\\Project\\testops\\Stage5\\07pytest\ncollected 2 items                                                                                                                    \n\ntest_runrule.py F.                                                                                                             [100%]\n\n============================================================= FAILURES ==============================================================\n__________________________________________________________ test_isnottrue ___________________________________________________________\n\n    def test_isnottrue():\n>       assert func(3,3) is True\nE       assert False is True\nE        +  where False = func(3, 3)\n\ntest_runrule.py:14: AssertionError\n--------------------------- generated xml file: D:\\Coding\\Project\\testops\\Stage5\\07pytest\\report\\log.xml ----------------------------\n==================================================== 1 failed, 1 passed in 0.03s ====================================================\n<\/code><\/pre>\n<p>\u67e5\u770breport\\log.xml\u5982\u4e0b<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;testsuites&gt;&lt;testsuite errors=&quot;0&quot; failures=&quot;1&quot; hostname=&quot;DESKTOP-V5FAHI8&quot; name=&quot;pytest&quot; skipped=&quot;0&quot; tests=&quot;2&quot; time=&quot;0.030&quot; timestamp=&quot;2019-11-01T17:10:51.928076&quot;&gt;&lt;testcase classname=&quot;test_runrule&quot; file=&quot;test_runrule.py&quot; line=&quot;12&quot; name=&quot;test_isnottrue&quot; time=&quot;0.000&quot;&gt;&lt;failure message=&quot;assert False is True\n +  where False = func(3, 3)&quot;&gt;def test_isnottrue():\n>       assert func(3,3) is True\nE       assert False is True\nE        +  where False = func(3, 3)\n\ntest_runrule.py:14: AssertionError&lt;\/failure&gt;&lt;\/testcase&gt;&lt;testcase classname=&quot;test_runrule&quot; file=&quot;test_runrule.py&quot; line=&quot;15&quot; name=&quot;test_func_math&quot; time=&quot;0.000&quot;&gt;&lt;\/testcase&gt;&lt;\/testsuite&gt;&lt;\/testsuites&gt;<\/code><\/pre>\n<p>\u770b\u4e86\u8fd9\u4e48\u957f\u7684\u4ee3\u7801\u6216\u8005xml\uff0c\u662f\u4e0d\u662f\u89c9\u5f97\u8fd9\u62a5\u544a\u6837\u5f0f\u592a\u96be\u770b\u4e86\uff0c\u65e0\u6cd5\u5165\u773c\u3002<\/p>\n<p>\u5176\u5b9e\uff0c\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u6392\u7248\u5f88\u6f02\u4eae\u7684\u62a5\u544a\u3002\u4f8b\u5982\u8fd9\u4e2a\u662fpytest\u7684html\u62a5\u544a\u6a21\u677f\u3002<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img2018.cnblogs.com\/blog\/1825659\/201911\/1825659-20191106160749519-171071291..png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/img2018.cnblogs.com\/blog\/1825659\/201911\/1825659-20191106160749519-171071291..png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"avatar\" \/><\/div><\/p>\n<p>\u540e\u9762\u4f1a\u6709\u9488\u5bf9\u62a5\u544a\u7684\u5c0f\u8282\u6765\u8fdb\u884c\u8be6\u7ec6\u8bb0\u8ff0\u3002<\/p>\n<h3>6   \u672c\u5730\u914d\u7f6e\u6587\u4ef6conftest.py<\/h3>\n<p>.\/conftest.py<\/p>\n<pre><code class=\"language-python\">import pytest\n#\u8bbe\u7f6e\u6d4b\u8bd5\u94a9\u5b50\n\n@pytest.fixture()\ndef test_url():\n    return &quot;http:\/\/www.qq.com&quot;\n    return &quot;http:\/\/www.baidu.com&quot;<\/code><\/pre>\n<p>.\/test_qq.py<\/p>\n<pre><code class=\"language-python\">def test_qq(test_url):\n    print(test_url)<\/code><\/pre>\n<p>confset.py\u662fpytest\u7279\u6709\u7684\u672c\u5730\u6d4b\u8bd5\u914d\u7f6e\u6587\u4ef6,\u5373\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u9879\u76ee\u7ea7\u522b\u7684Fixture,\u4e5f\u53ef\u4ee5\u7528\u6765\u5bfc\u5165\u5916\u90e8\u63d2\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\u94a9\u5b50\u51fd\u6570\u3002<\/p>\n<h2>\u603b\u7ed3\uff1a<\/h2>\n<p>1  \u65ad\u8a00  \u7684\u5173\u952e\u5b57assert<\/p>\n<p>2  \u811a\u624b\u67b6\u7684\u5b9a\u4e49\u9700\u8981\u7528\u5230 @pytest.fixture()  \u5185\u7f6e\u65b9\u6cd5<\/p>\n<p>3   \u53c2\u6570\u5316\u7528\u5230\u7684\u5173\u952e\u5b57\uff1a@pytest.mark.parametrize(\uff09<\/p>\n<p>4   \u8fd0\u884c\u6d4b\u7684\u5e38\u7528\u6307\u4ee4<\/p>\n<p>-v    <\/p>\n<p>-s <\/p>\n<p>-q <\/p>\n<p>5  \u751f\u6210\u6d4b\u8bd5\u62a5\u544a\u7684\u65b9\u6cd5\u6709\u591a<\/p>\n<p>6 conftest.py\u540d\u5b57\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539<\/p>\n<h2>\u601d\u8003\u4e0e\u5ef6\u4f38<\/h2>\n<p>1  pytest\u4e2dfixture\u6e90\u7801\u89e3\u6790<\/p>\n<pre><code class=\"language-python\">    def parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None):\n        &quot;&quot;&quot; Add new invocations to the underlying test function using the list\n        of argvalues for the given argnames.  Parametrization is performed\n        during the collection phase.  If you need to setup expensive resources\n        see about setting indirect to do it rather at test setup time.\n\n        :arg argnames: a comma-separated string denoting one or more argument\n                       names, or a list\/tuple of argument strings.\n\n        :arg argvalues: The list of argvalues determines how often a\n            test is invoked with different argument values.  If only one\n            argname was specified argvalues is a list of values.  If N\n            argnames were specified, argvalues must be a list of N-tuples,\n            where each tuple-element specifies a value for its respective\n            argname.\n\n        :arg indirect: The list of argnames or boolean. A list of arguments&#039;\n            names (self,subset of argnames). If True the list contains all names from\n            the argnames. Each argvalue corresponding to an argname in this list will\n            be passed as request.param to its respective argname fixture\n            function so that it can perform more expensive setups during the\n            setup phase of a test rather than at collection time.\n\n        :arg ids: list of string ids, or a callable.\n            If strings, each is corresponding to the argvalues so that they are\n            part of the test id. If None is given as id of specific test, the\n            automatically generated id for that argument will be used.\n            If callable, it should take one argument (self,a single argvalue) and return\n            a string or return None. If None, the automatically generated id for that\n            argument will be used.\n            If no ids are provided they will be generated automatically from\n            the argvalues.\n\n        :arg scope: if specified it denotes the scope of the parameters.\n            The scope is used for grouping tests by parameter instances.\n            It will also override any fixture-function defined scope, allowing\n            to set a dynamic scope using test context or configuration.\n        &quot;&quot;&quot;<\/code><\/pre>\n<p>:arg ids: list of string ids, or a callable. \u8fd9\u91cc\u53ef\u4ee5\u5b9a\u4e49\u6d4b\u8bd5\u7528\u4f8b\u7684\u540d\u79f0\uff0c\u5982\u679c\u6709N\u4e2a\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u8fd9\u662f\u4e2a\u5f88\u68d2\u7684\u7528\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>04-13pytest\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5 \u5f15\u8a00 \u6709\u4e86unittest\u8fd9\u4e2a\u7ecf\u5178\u7684\u6d4b\u8bd5\u6846\u67b6\u505a\u94fa\u57ab\uff0c\u90a3\u4e48\u5b66\u4e60\u5176\u4ed6\u4efb\u4f55\u7684\u6d4b [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[357,355],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4667"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4667"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4667\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}