{"id":4662,"date":"2022-03-27T18:35:21","date_gmt":"2022-03-27T10:35:21","guid":{"rendered":"https:\/\/egonlin.com\/?p=4662"},"modified":"2022-03-27T18:35:21","modified_gmt":"2022-03-27T10:35:21","slug":"14%ef%bc%9apytest%e7%9a%84fixture%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=4662","title":{"rendered":"14\uff1apytest\u7684fixture\u8be6\u89e3"},"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\u57fa\u7840\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>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>\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>\u4ece\u56fe\u4e2d\u6211\u4eec\u53ef\u4ee5\u5f62\u8c61\u7684\u77e5\u9053\u65b0\u7684\u811a\u624b\u67b6\u5177\u6709\u4e864\u4e2a\u8f6e\u5b50\uff0c\u4f53\u79ef\u4e5f\u66f4\u8f7b\u4fbf\u4e86\uff0c\u770b\u8d77\u6765\u66f4\u65b0\u9896\u3002<\/p>\n<p>\u800c\u8001\u7684\u811a\u624b\u67b6\u7684\u57fa\u672c\u529f\u80fd\uff0c\u5b83\u4e5f\u5177\u6709\uff0c\u6709\u5927\u9505\uff0c\u6709\u7076\u53f0\u53f0\u9762\uff0c\u8fd8\u6709\u70df\u56f1\u3002<\/p>\n<h5>\u5b83\u53ef\u4ee5\u5f53\u6210\u51fd\u6570\u7684\u53c2\u6570<\/h5>\n<p>.\\test_funcationargument.py<\/p>\n<pre><code class=\"language-python\">import pytest\n\n&quot;&quot;&quot;\n\u811a\u624b\u67b6\n---\u8fd9\u662f\u4e00\u4e2a\u5173\u4e8e\u51fd\u6570\u53c2\u6570\u7684fixture\n&quot;&quot;&quot;\n@pytest.fixture()\ndef asargument():\n    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u5173\u4e8e\u51fd\u6570\u53c2\u6570\u7684fixture&quot;,&#039;\\n&#039;,__name__)\n    return __name__\n\ndef test_asargument(asargument):\n    print(asargument)\n    assert asargument is not  None ,&quot;\u83b7\u53d6\u4e3a\u7a7a&quot;<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code class=\"language-python\">(testops) &gt;pytest  -s -v test_funcationargument.py\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\nmetadata: {&#039;Python&#039;: &#039;3.7.1&#039;, &#039;Platform&#039;: &#039;Windows-10-10.0.18362-SP0&#039;, &#039;Packages&#039;: {&#039;pytest&#039;: &#039;5.2.2&#039;, &#039;py&#039;: &#039;1.8.0&#039;, &#039;pluggy&#039;: &#039;0.13.0&#039;}, &#039;Plugins&#039;: {&#039;html&#039;: &#039;2.0.0&#039;, &#039;\nmetadata&#039;: &#039;1.8.0&#039;}}\nrootdir: D:\\Coding\\Project\\testops\\Stage5\\07pytest\\fixture\nplugins: html-2.0.0, metadata-1.8.0\ncollected 1 item                                                                                                                                                        \n\ntest_funcationargument.py::test_asargument \u8fd9\u662f\u4e00\u4e2a\u5173\u4e8e\u51fd\u6570\u53c2\u6570\u7684fixture\n fixture.test_funcationargument\nfixture.test_funcationargument\nPASSED\n\n========================================================================== 1 passed in 0.02s ===========================================================================\n<\/code><\/pre>\n<h5>\u5b83\u53ef\u4ee5\u5f53\u6210\u4f9d\u8d56\u6765\u6ce8\u5165<\/h5>\n<pre><code class=\"language-python\"># content  .\/confest.py\nimport pytest\nimport smtplib\n\n@pytest.fixture(scope=&quot;module&quot;)\ndef smtp_connection():\n    re_r =smtplib.SMTP(&quot;smtp.qq.com&quot;,587,timeout=5)\n    print(re_r)\n    return re_r\n\n# content .\/test_module.py\ndef test_ehlo(smtp_connection):\n    response,msg = smtp_connection.ehlo()\n    print(response,msg)\n    assert response==250\n    assert b&quot;smtp.qq.com&quot; in msg\n    assert 0 # for demo purposes\n\ndef test_noop(smtp_connection):\n\n    response ,msg = smtp_connection.noop()\n    print(response,msg)\n    assert response == 250\n    assert 0 # for demo purposes<\/code><\/pre>\n<h2>\u603b\u7ed3\uff1a<\/h2>\n<pre><code class=\"language-vb\">1\u3001\u521b\u5efa\uff1aconftest.py\u6587\u4ef6\u91cc\u9762\u7684\u51fd\u6570\u524d\u52a0\uff1a@pytest.fixture @pytest.fixture(scope=&quot;session&quot;,autouse=True)  \n\u53c2\u6570scope\u9ed8\u8ba4\u51fd\u6570\u7ea7\u522b\uff0csession\u56de\u8bdd\u7ea7\u522b\u6574\u4f53\u8fd0\u884c\u4e00\u6b21\uff0c\u53ef\u8bbe\u7f6e\u81ea\u52a8\u6267\u884c\n2\u3001yield \u9694\u65ad\u524d\u7f6e\u540e\u7f6e\uff0c\u540e\u9762\u52a0\u51fd\u6570\u8fd4\u56de\u503c\u3002\n3\u3001\u8c03\u7528\uff1a\u51fd\u6570\u6216\u7c7b\u524d\u52a0 @pytest.mark.usefixture(&quot;\u51fd\u6570\u540d&quot;)         \nfixture\u51fd\u6570\u540d\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u7528\u4f8b\u76ee\u7684\uff1a\u7528\u4e8e\u516c\u5171\u6570\u636e\u51c6\u5907\u548c\u6e05\u7406\u3002\n4 fixture\u91cc\u9762\u6709\u4e2ascope\u53c2\u6570\u53ef\u4ee5\u63a7\u5236fixture\u7684\u4f5c\u7528\u8303\u56f4:session &gt; module &gt; class &gt; function<\/code><\/pre>\n<h2>\u601d\u8003\u4e0e\u5ef6\u4f38<\/h2>\n<h5>1 fixture \u7684scope\u6e90\u7801\u5757<\/h5>\n<pre><code class=\"language-python\">def fixture(\n    callable_or_scope=None,\n    *args,\n    scope=&quot;function&quot;,\n    params=None,\n    autouse=False,\n    ids=None,\n    name=None\n):\n    &quot;&quot;&quot;Decorator to mark a fixture factory function.\n\n    This decorator can be used, with or without parameters, to define a\n    fixture function.\n\n    The name of the fixture function can later be referenced to cause its\n    invocation ahead of running tests: test\n    modules or classes can use the ``pytest.mark.usefixtures(fixturename)``\n    marker.\n\n    Test functions can directly use fixture names as input\n    arguments in which case the fixture instance returned from the fixture\n    function will be injected.\n\n    Fixtures can provide their values to test functions using ``return`` or ``yield``\n    statements. When using ``yield`` the code block after the ``yield`` statement is executed\n    as teardown code regardless of the test outcome, and must yield exactly once.\n\n    :arg scope: the scope for which this fixture is shared, one of\n                ``&quot;function&quot;`` (default), ``&quot;class&quot;``, ``&quot;module&quot;``,\n                ``&quot;package&quot;`` or ``&quot;session&quot;`` (``&quot;package&quot;`` is considered **experimental**\n                at this time).\n\n                This parameter may also be a callable which receives ``(fixture_name, config)``\n                as parameters, and must return a ``str`` with one of the values mentioned above.\n\n                See :ref:`dynamic scope` in the docs for more information.\n\n    :arg params: an optional list of parameters which will cause multiple\n                invocations of the fixture function and all of the tests\n                using it.\n                The current parameter is available in ``request.param``.\n\n    :arg autouse: if True, the fixture func is activated for all tests that\n                can see it.  If False (the default) then an explicit\n                reference is needed to activate the fixture.\n\n    :arg ids: list of string ids each corresponding to the params\n                so that they are part of the test id. If no ids are provided\n                they will be generated automatically from the params.\n\n    :arg name: the name of the fixture. This defaults to the name of the\n                decorated function. If a fixture is used in the same module in\n                which it is defined, the function name of the fixture will be\n                shadowed by the function arg that requests the fixture; one way\n                to resolve this is to name the decorated function\n                ``fixture_&lt;fixturename&gt;`` and then use\n                ``@pytest.fixture(name=&#039;&lt;fixturename&gt;&#039;)``.<\/code><\/pre>\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\u57fa\u7840\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\/4662"}],"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=4662"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4662\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}