{"id":4654,"date":"2022-03-27T18:30:11","date_gmt":"2022-03-27T10:30:11","guid":{"rendered":"https:\/\/egonlin.com\/?p=4654"},"modified":"2022-03-27T18:30:11","modified_gmt":"2022-03-27T10:30:11","slug":"17%ef%bc%9apytest%e7%9a%84%e8%bf%90%e8%a1%8c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=4654","title":{"rendered":"17\uff1apytest\u7684\u8fd0\u884c\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\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>pytest\u7684\u8fd0\u884c\u4e5f\u662f\u591a\u6837\u5316\u7684\u3002<\/p>\n<h2>\u6b63\u6587:<\/h2>\n<h3><\/h3>\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) &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<p>2.\u8fd0\u884c\u547d\u4ee4\uff1a pytest -k &quot;math&quot; test_runrule.py <\/p>\n<p>\u5176\u4e2d-k &quot;math&quot; \u7b5b\u9009\u53ea\u5305\u542bmath\u7684\u5b57\u7b26\u6d4b\u8bd5\u7528\u4f8b,\u7ed3\u679c\u5982\u4e0b\uff0c\u53ea\u8fd0\u884c\u4e86test_func_math\uff0c\u4e0d\u8fd0\u884ctest_isnottrue<\/p>\n<pre><code class=\"language-vbscript\">(testops) &gt;pytest -k &quot;math&quot; 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 \/ 1 deselected \/ 1 selected                                                                                        \n\ntest_runrule.py .                                                                                                              [100%]\n\n================================================== 1 passed, 1 deselected in 0.01s ==================================================\n<\/code><\/pre>\n<p>3.\u8fd0\u884c\u547d\u4ee4\uff1a pytest test_runrule.py -q  <\/p>\n<p>\u5176\u4e2d-q \u662f\u51cf\u5c11\u5197\u957f<\/p>\n<pre><code class=\"language-vbscript\">(testops) &gt;pytest  test_runrule.py -q\nF.                                                                                                                             [100%]\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\n1 failed, 1 passed in 0.03s\n<\/code><\/pre>\n<p>4  \u8fd0\u884c\u547d\u4ee4\uff1a pytest  test_runrule.py -x<\/p>\n<p>\u5176\u4e2d-x \u5982\u679c\u51fa\u73b0\u4e00\u6761\u6d4b\u8bd5\u7528\u4f8b\u5931\u8d25,\u5219\u9000\u51fa\u6d4b\u8bd5<\/p>\n<pre><code class=\"language-vbscript\">(testops) &gt;pytest  test_runrule.py -x\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\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 in 0.03s =========================================================\n<\/code><\/pre>\n<ol start=\"5\">\n<li>\n<p>\u8fd0\u884c\u547d\u4ee4\uff1a pytest  test_runrule.py -v<\/p>\n<p>-v \u80fd\u628a\u4fe1\u606f\u6253\u5370\u7684\u66f4\u8be6\u7ec6<\/p>\n<pre><code>(testops) >pytest test_runrule.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 2 items                                                                                                                    \n\ntest_runrule.py::test_isnottrue FAILED                                                                                         [ 50%]\ntest_runrule.py::test_func_math PASSED                                                                                         [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 ====================================================<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8fd0\u884c\u547d\u4ee4  pytest  .\\<\/p>\n<p>\u5176\u4e2d.\\\u662f\u6587\u4ef6\u76ee\u5f55\u7684\u540d\u5b57<\/p>\n<pre><code class=\"language-vbscript\">(testops) >pytest .\\\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 22 items                                                                                                                   \n\ntest_assert.py .                                                                                                               [  4%]\ntest_introfixture.py .                                                                                                         [  9%]\ntest_main.py F                                                                                                                 [ 13%]\ntest_operator.py ...........                                                                                                   [ 63%]\ntest_parameterized.py ....                                                                                                     [ 81%]\ntest_pytestfixture.py .                                                                                                        [ 86%]\ntest_runrule.py F.                                                                                                             [ 95%]\ntest_sample.py F                                                                                                               [100%]\n\n============================================================= FAILURES ==============================================================\n____________________________________________________________ test_answer ____________________________________________________________\n\n   def test_answer():\n>       assert inc(3) == 5\nE       assert 4 == 5\nE        +  where 4 = inc(3)\n\ntest_main.py:7: AssertionError\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____________________________________________________________ test_answer ____________________________________________________________\n\n   def test_answer():\n>       assert inc(3) == 5\nE       assert 4 == 5\nE        +  where 4 = inc(3)\n\ntest_sample.py:6: AssertionError\n=================================================== 3 failed, 19 passed in 0.12s ====================================================<\/code><\/pre>\n<\/li>\n<li>\n<p>\u901a\u8fc7main()\u65b9\u6cd5\u8fd0\u884c\u6d4b\u8bd5<\/p>\n<pre><code class=\"language-python\">import pytest\n\nif __name__== '__main__':\n   pytest.main(['-s','.\/test_runrule.py'])<\/code><\/pre>\n<p>8.\u8fd0\u884c\u7684\u65f6\u5019\u751f\u6210html\u62a5\u544a<\/p>\n<pre><code class=\"language-vbscript\">#\u751f\u6210html\u62a5\u544a\u5728\u5f53\u524d\u8def\u5f84,css\u6587\u4ef6\u5206\u79bb\npytest test_markers.py --html=report.html\n#\u751f\u6210html\u62a5\u544a\u5728\u5f53\u524d\u8def\u5f84,cs\u6587\u4ef6\u4e0d\u5206\u79bb\npytest test_markers.py --html=report.html --self-contained-html<\/code><\/pre>\n<\/li>\n<\/ol>\n<pre><code class=\"language-powershell\"># \u5148\u5b89\u88c5pytest-html \u5916\u90e8\u6a21\u5757\npip install pytest-html<\/code><\/pre>\n<h2>\u603b\u7ed3\uff1a<\/h2>\n<p>1   pytest\u7684\u8fd0\u884c\u53c2\u6570\u5343\u5343\u4e07\uff0c\u52a1\u5fc5\u7262\u8bb0  -v -s  <\/p>\n<p>2   \u8fd0\u884c\u5165\u53e3\u6709py\u6587\u4ef6\uff0c\u6709class \u7c7b\uff0c \u8fd8\u6709 function\u65b9\u6cd5<\/p>\n<p>3   pytest\u7684html\u62a5\u544a\u9700\u8981\u628acss\u653e\u5165html\u6587\u4ef6\u4e2d\uff0c\u624d\u80fd\u5206\u4eab\u51fa\u53bb\u7684\u65f6\u5019\u4e0d\u4e22\u5931<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img2018.cnblogs.com\/blog\/1825659\/201911\/1825659-20191106161211525-2128538955..png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/img2018.cnblogs.com\/blog\/1825659\/201911\/1825659-20191106161211525-2128538955..png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"avatar\" \/><\/div><\/p>\n<h2>\u601d\u8003\u4e0e\u5ef6\u4f38<\/h2>\n<p>1  pytest\u7684html\u62a5\u544a\u4e2d\u80fd\u663e\u793a\u7528\u4f8b\u63cf\u8ff0\u5417\uff1f<\/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\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\/4654"}],"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=4654"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4654\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}