{"id":33,"date":"2026-06-11T10:00:00","date_gmt":"2026-06-11T02:00:00","guid":{"rendered":"http:\/\/localhost:8888\/?p=33"},"modified":"2026-07-09T16:17:45","modified_gmt":"2026-07-09T08:17:45","slug":"pytest%e9%ab%98%e7%ba%a7%e7%94%a8%e6%b3%95%e5%85%a8%e6%94%bb%e7%95%a5","status":"publish","type":"post","link":"https:\/\/blog.runnergo.cn\/?p=33","title":{"rendered":"Pytest\u9ad8\u7ea7\u7528\u6cd5\u5168\u653b\u7565 &#8211; \u53c2\u6570\u5316\u3001\u94a9\u5b50\u51fd\u6570\u4e0e\u63d2\u4ef6\u5f00\u53d1\u5b9e\u6218"},"content":{"rendered":"<h1>Pytest\u9ad8\u7ea7\u7528\u6cd5\u5168\u653b\u7565 &#8211; \u53c2\u6570\u5316\u3001\u94a9\u5b50\u51fd\u6570\u4e0e\u63d2\u4ef6\u5f00\u53d1\u5b9e\u6218<\/h1>\n<p>&gt; \u4f5c\u8005\uff1a\u963f\u6cfddebug<br \/>\n&#8212;<\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u963f\u6cfddebug\u3002\u505aPython\u81ea\u52a8\u5316\u6d4b\u8bd55\u5e74\u4e86\uff0cPytest\u662f\u6211\u7684\u4e3b\u529b\u5de5\u5177\u3002\u5f88\u591a\u6d4b\u8bd5\u540c\u884c\u7528Pytest\uff0c\u4f46\u53ea\u7528\u4e86\u57fa\u7840\u529f\u80fd\uff08<code>pytest.main()<\/code>\u3001<code>@pytest.fixture<\/code>\uff09\u3002\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u60f3\u5206\u4eabPytest\u7684\u9ad8\u7ea7\u7528\u6cd5\uff0c\u8ba9\u4f60\u7684\u6d4b\u8bd5\u66f4\u5f3a\u5927\u3001\u66f4\u4f18\u96c5\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u8986\u76d6\uff1a<\/p>\n<ul>\n<li>\u53c2\u6570\u5316\u8fdb\u9636<\/li>\n<li>\u94a9\u5b50\u51fd\u6570\uff08Hooks\uff09<\/li>\n<li>\u81ea\u5b9a\u4e49\u63d2\u4ef6\u5f00\u53d1<\/li>\n<li>\u6d4b\u8bd5\u62a5\u544a\u7f8e\u5316<\/li>\n<li>\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/li>\n<\/ul>\n<p>&#8212;<\/p>\n<h2>\u4e00\u3001\u53c2\u6570\u5316\u8fdb\u9636<\/h2>\n<h3>\u57fa\u7840\u53c2\u6570\u5316<\/h3>\n<p><code>`<\/code>python<br \/>\nimport pytest<\/p>\n<h1>\u5355\u53c2\u6570<\/h1>\n<p>@pytest.mark.parametrize(&#8220;input&#8221;, [1, 2, 3])<br \/>\ndef test_single_param(input):<br \/>\n    assert input &gt; 0<\/p>\n<h1>\u591a\u53c2\u6570<\/h1>\n<p>@pytest.mark.parametrize(&#8220;a, b, expected&#8221;, [<br \/>\n    (1, 2, 3),<br \/>\n    (10, 20, 30),<br \/>\n    (-5, 5, 0),<br \/>\n])<br \/>\ndef test_multi_params(a, b, expected):<br \/>\n    assert a + b == expected<br \/>\n<code>`<\/code><\/p>\n<h3>\u53c2\u6570\u5316 + ID\u547d\u540d<\/h3>\n<p><code>`<\/code>python<br \/>\n@pytest.mark.parametrize(&#8220;input, expected&#8221;, [<br \/>\n    pytest.param(1, True, id=&#8221;\u6b63\u6570&#8221;),<br \/>\n    pytest.param(-1, False, id=&#8221;\u8d1f\u6570&#8221;),<br \/>\n    pytest.param(0, False, id=&#8221;\u96f6&#8221;),<br \/>\n])<br \/>\ndef test_with_ids(input, expected):<br \/>\n    assert (input &gt; 0) == expected<br \/>\n<code>`<\/code><\/p>\n<p>\u8fd0\u884c\u65f6\u6d4b\u8bd5\u540d\u79f0\u66f4\u6e05\u6670\uff1a<br \/>\n<code>`<\/code><br \/>\ntest_with_ids[\u6b63\u6570] PASSED<br \/>\ntest_with_ids[\u8d1f\u6570] PASSED<br \/>\ntest_with_ids[\u96f6] PASSED<br \/>\n<code>`<\/code><\/p>\n<h3>\u4ece\u6587\u4ef6\u8bfb\u53d6\u53c2\u6570<\/h3>\n<p><code>`<\/code>python<br \/>\nimport yaml<br \/>\nimport pytest<\/p>\n<h1>\u8bfb\u53d6YAML\u6587\u4ef6\u4f5c\u4e3a\u6d4b\u8bd5\u6570\u636e<\/h1>\n<p>def load_test_data():<br \/>\n    with open(&#8220;test_data.yaml&#8221;, &#8220;r&#8221;) as f:<br \/>\n        return yaml.safe_load(f)<\/p>\n<p>@pytest.mark.parametrize(&#8220;data&#8221;, load_test_data())<br \/>\ndef test_from_yaml(data):<br \/>\n    assert data[&#8220;input&#8221;] + data[&#8220;add&#8221;] == data[&#8220;expected&#8221;]<\/p>\n<h1>test_data.yaml\u5185\u5bb9\uff1a<\/h1>\n<h1>&#8211; input: 1<\/h1>\n<h1>  add: 2<\/h1>\n<h1>  expected: 3<\/h1>\n<h1>&#8211; input: 10<\/h1>\n<h1>  add: 5<\/h1>\n<h1>  expected: 15<\/h1>\n<p><code>`<\/code><\/p>\n<h3>\u53c2\u6570\u5316\u7ec4\u5408<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>\u591a\u4e2aparametrize\u4f1a\u7ec4\u5408\u751f\u6210\u591a\u4e2a\u6d4b\u8bd5<\/h1>\n<p>@pytest.mark.parametrize(&#8220;x&#8221;, [1, 2])<br \/>\n@pytest.mark.parametrize(&#8220;y&#8221;, [10, 20])<br \/>\ndef test_combination(x, y):<br \/>\n    # \u4f1a\u751f\u62104\u4e2a\u6d4b\u8bd5\uff1a<br \/>\n    # x=1, y=10<br \/>\n    # x=1, y=20<br \/>\n    # x=2, y=10<br \/>\n    # x=2, y=20<br \/>\n    assert x * y &gt; 0<br \/>\n<code>`<\/code><\/p>\n<h3>\u6761\u4ef6\u8df3\u8fc7\u53c2\u6570<\/h3>\n<p><code>`<\/code>python<br \/>\nimport sys<\/p>\n<p>@pytest.mark.parametrize(&#8220;value&#8221;, [<br \/>\n    pytest.param(&#8220;windows_only&#8221;, marks=pytest.mark.skipif(sys.platform != &#8220;win32&#8243;, reason=&#8221;\u4ec5Windows&#8221;)),<br \/>\n    pytest.param(&#8220;linux_only&#8221;, marks=pytest.mark.skipif(sys.platform != &#8220;linux&#8221;, reason=&#8221;\u4ec5Linux&#8221;)),<br \/>\n    pytest.param(&#8220;all_platforms&#8221;, marks=pytest.mark.noop),<br \/>\n])<br \/>\ndef test_platform_specific(value):<br \/>\n    assert True<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u4e8c\u3001Fixture\u8fdb\u9636<\/h2>\n<h3>Fixture\u4f5c\u7528\u57df<\/h3>\n<p><code>`<\/code>python<br \/>\nimport pytest<\/p>\n<h1>function\u7ea7\u522b\uff1a\u6bcf\u4e2a\u6d4b\u8bd5\u51fd\u6570\u8c03\u7528\u4e00\u6b21<\/h1>\n<p>@pytest.fixture(scope=&#8221;function&#8221;)<br \/>\ndef func_fixture():<br \/>\n    return &#8220;function&#8221;<\/p>\n<h1>class\u7ea7\u522b\uff1a\u6bcf\u4e2a\u6d4b\u8bd5\u7c7b\u8c03\u7528\u4e00\u6b21<\/h1>\n<p>@pytest.fixture(scope=&#8221;class&#8221;)<br \/>\ndef class_fixture():<br \/>\n    return &#8220;class&#8221;<\/p>\n<h1>module\u7ea7\u522b\uff1a\u6bcf\u4e2a\u6a21\u5757\u8c03\u7528\u4e00\u6b21<\/h1>\n<p>@pytest.fixture(scope=&#8221;module&#8221;)<br \/>\ndef module_fixture():<br \/>\n    return &#8220;module&#8221;<\/p>\n<h1>session\u7ea7\u522b\uff1a\u6574\u4e2a\u6d4b\u8bd5\u4f1a\u8bdd\u8c03\u7528\u4e00\u6b21<\/h1>\n<p>@pytest.fixture(scope=&#8221;session&#8221;)<br \/>\ndef session_fixture():<br \/>\n    return &#8220;session&#8221;<\/p>\n<h1>package\u7ea7\u522b\uff1a\u6bcf\u4e2a\u5305\u8c03\u7528\u4e00\u6b21<\/h1>\n<p>@pytest.fixture(scope=&#8221;package&#8221;)<br \/>\ndef package_fixture():<br \/>\n    return &#8220;package&#8221;<br \/>\n<code>`<\/code><\/p>\n<h3>Fixture\u4f9d\u8d56<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>Fixture\u53ef\u4ee5\u4f9d\u8d56\u5176\u4ed6Fixture<\/h1>\n<p>@pytest.fixture<br \/>\ndef db_connection():<br \/>\n    conn = connect_to_db()<br \/>\n    yield conn<br \/>\n    conn.close()<\/p>\n<p>@pytest.fixture<br \/>\ndef user_data(db_connection):<br \/>\n    # \u4f9d\u8d56db_connection<br \/>\n    return db_connection.query(&#8220;SELECT * FROM users&#8221;)<\/p>\n<p>def test_query(user_data):<br \/>\n    # \u81ea\u52a8\u83b7\u53d6user_data\uff0c\u95f4\u63a5\u83b7\u53d6db_connection<br \/>\n    assert len(user_data) &gt; 0<br \/>\n<code>`<\/code><\/p>\n<h3>Fixture\u81ea\u52a8\u6267\u884c<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>autouse=True\uff1a\u81ea\u52a8\u5e94\u7528\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u5f15\u7528<\/h1>\n<p>@pytest.fixture(autouse=True)<br \/>\ndef setup_teardown():<br \/>\n    print(&#8220;\u6d4b\u8bd5\u524d\u51c6\u5907&#8221;)<br \/>\n    yield<br \/>\n    print(&#8220;\u6d4b\u8bd5\u540e\u6e05\u7406&#8221;)<\/p>\n<p>def test_example():<br \/>\n    # \u4e0d\u9700\u8981\u4f20\u5165setup_teardown\uff0c\u81ea\u52a8\u6267\u884c<br \/>\n    assert True<br \/>\n<code>`<\/code><\/p>\n<h3>Fixture\u53c2\u6570\u5316<\/h3>\n<p><code>`<\/code>python<br \/>\n@pytest.fixture(params=[&#8220;chrome&#8221;, &#8220;firefox&#8221;, &#8220;edge&#8221;])<br \/>\ndef browser(request):<br \/>\n    # request.param\u83b7\u53d6\u5f53\u524d\u53c2\u6570\u503c<br \/>\n    driver = get_driver(request.param)<br \/>\n    yield driver<br \/>\n    driver.quit()<\/p>\n<p>def test_with_browser(browser):<br \/>\n    browser.get(&#8220;https:\/\/example.com&#8221;)<br \/>\n    assert browser.title<br \/>\n<code>`<\/code><\/p>\n<h3>conftest.py\u5171\u4eabFixture<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>tests\/conftest.py<\/h1>\n<p>import pytest<br \/>\nimport requests<\/p>\n<p>@pytest.fixture(scope=&#8221;session&#8221;)<br \/>\ndef api_client():<br \/>\n    &#8220;&#8221;&#8221;API\u6d4b\u8bd5\u5ba2\u6237\u7aef&#8221;&#8221;&#8221;<br \/>\n    session = requests.Session()<br \/>\n    session.headers.update({&#8220;Authorization&#8221;: &#8220;Bearer test-token&#8221;})<br \/>\n    return session<\/p>\n<p>@pytest.fixture(scope=&#8221;session&#8221;)<br \/>\ndef test_config():<br \/>\n    &#8220;&#8221;&#8221;\u6d4b\u8bd5\u914d\u7f6e&#8221;&#8221;&#8221;<br \/>\n    return {<br \/>\n        &#8220;base_url&#8221;: &#8220;https:\/\/api.example.com&#8221;,<br \/>\n        &#8220;timeout&#8221;: 30<br \/>\n    }<br \/>\n<code>`<\/code><\/p>\n<p>\u6240\u6709\u6d4b\u8bd5\u6587\u4ef6\u90fd\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9bFixture\uff1a<br \/>\n<code>`<\/code>python<\/p>\n<h1>tests\/api\/test_users.py<\/h1>\n<p>def test_get_users(api_client, test_config):<br \/>\n    resp = api_client.get(f&#8221;{test_config[&#8216;base_url&#8217;]}\/users&#8221;)<br \/>\n    assert resp.status_code == 200<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u4e09\u3001\u94a9\u5b50\u51fd\u6570\uff08Hooks\uff09<\/h2>\n<p>Pytest\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u94a9\u5b50\u51fd\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6d4b\u8bd5\u6d41\u7a0b\u3002<\/p>\n<h3>\u5e38\u7528\u94a9\u5b50\u51fd\u6570<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>conftest.py<\/h1>\n<p>import pytest<\/p>\n<h1>\u6d4b\u8bd5\u5f00\u59cb\u524d<\/h1>\n<p>def pytest_configure(config):<br \/>\n    print(&#8220;Pytest\u914d\u7f6e\u9636\u6bb5&#8221;)<br \/>\n    config.addinivalue_line(&#8220;markers&#8221;, &#8220;slow:\u6807\u8bb0\u6162\u6d4b\u8bd5&#8221;)<\/p>\n<h1>\u6d4b\u8bd5\u4f1a\u8bdd\u5f00\u59cb<\/h1>\n<p>def pytest_sessionstart(session):<br \/>\n    print(&#8220;\u6d4b\u8bd5\u4f1a\u8bdd\u5f00\u59cb&#8221;)<\/p>\n<h1>\u6d4b\u8bd5\u4f1a\u8bdd\u7ed3\u675f<\/h1>\n<p>def pytest_sessionfinish(session, exitstatus):<br \/>\n    print(f&#8221;\u6d4b\u8bd5\u4f1a\u8bdd\u7ed3\u675f\uff0c\u72b6\u6001\uff1a{exitstatus}&#8221;)<\/p>\n<h1>\u6536\u96c6\u6d4b\u8bd5\u524d<\/h1>\n<p>def pytest_collection_modifyitems(config, items):<br \/>\n    # \u53ef\u4ee5\u4fee\u6539\u6d4b\u8bd5\u5217\u8868<br \/>\n    for item in items:<br \/>\n        if &#8220;slow&#8221; in item.keywords:<br \/>\n            item.add_marker(pytest.mark.skip(reason=&#8221;\u8df3\u8fc7\u6162\u6d4b\u8bd5&#8221;))<\/p>\n<h1>\u6bcf\u4e2a\u6d4b\u8bd5\u6267\u884c\u524d<\/h1>\n<p>def pytest_runtest_setup(item):<br \/>\n    print(f&#8221;\u51c6\u5907\u6267\u884c\uff1a{item.name}&#8221;)<\/p>\n<h1>\u6bcf\u4e2a\u6d4b\u8bd5\u6267\u884c\u540e<\/h1>\n<p>def pytest_runtest_teardown(item, nextitem):<br \/>\n    print(f&#8221;\u6e05\u7406\u6d4b\u8bd5\uff1a{item.name}&#8221;)<\/p>\n<h1>\u6d4b\u8bd5\u5931\u8d25\u65f6<\/h1>\n<p>def pytest_runtest_logreport(report):<br \/>\n    if report.failed:<br \/>\n        print(f&#8221;\u6d4b\u8bd5\u5931\u8d25\uff1a{report.nodeid}&#8221;)<br \/>\n        # \u53ef\u4ee5\u5728\u8fd9\u91cc\u505a\u5931\u8d25\u622a\u56fe\u3001\u65e5\u5fd7\u8bb0\u5f55\u7b49<br \/>\n<code>`<\/code><\/p>\n<h3>\u81ea\u5b9a\u4e49\u6d4b\u8bd5\u62a5\u544a<\/h3>\n<p><code>`<\/code>python<br \/>\ndef pytest_runtest_makereport(item, call):<br \/>\n    &#8220;&#8221;&#8221;\u751f\u6210\u6d4b\u8bd5\u62a5\u544a&#8221;&#8221;&#8221;<br \/>\n    if call.when == &#8220;call&#8221;:<br \/>\n        # \u6d4b\u8bd5\u6267\u884c\u9636\u6bb5<br \/>\n        if call.excinfo is not None:<br \/>\n            # \u6d4b\u8bd5\u5931\u8d25<br \/>\n            item.session.failed_tests.append(item.nodeid)<br \/>\n        else:<br \/>\n            # \u6d4b\u8bd5\u6210\u529f<br \/>\n            item.session.passed_tests.append(item.nodeid)<\/p>\n<h1>\u5b58\u50a8\u6d4b\u8bd5\u7ed3\u679c<\/h1>\n<p>@pytest.fixture(scope=&#8221;session&#8221;, autouse=True)<br \/>\ndef store_results(request):<br \/>\n    request.session.failed_tests = []<br \/>\n    request.session.passed_tests = []<\/p>\n<p>    yield<\/p>\n<p>    # \u4f1a\u8bdd\u7ed3\u675f\u540e\u7edf\u8ba1<br \/>\n    print(f&#8221;n\u6210\u529f\uff1a{len(request.session.passed_tests)}&#8221;)<br \/>\n    print(f&#8221;\u5931\u8d25\uff1a{len(request.session.failed_tests)}&#8221;)<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u56db\u3001\u81ea\u5b9a\u4e49\u63d2\u4ef6\u5f00\u53d1<\/h2>\n<h3>\u521b\u5efa\u63d2\u4ef6\u6587\u4ef6<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>pytest_custom_plugin.py<\/h1>\n<p>import pytest<\/p>\n<p>class CustomPlugin:<br \/>\n    &#8220;&#8221;&#8221;\u81ea\u5b9a\u4e49Pytest\u63d2\u4ef6&#8221;&#8221;&#8221;<\/p>\n<p>    def pytest_configure(self, config):<br \/>\n        &#8220;&#8221;&#8221;\u914d\u7f6e\u9636\u6bb5&#8221;&#8221;&#8221;<br \/>\n        self.results = {&#8220;passed&#8221;: 0, &#8220;failed&#8221;: 0, &#8220;skipped&#8221;: 0}<\/p>\n<p>    def pytest_runtest_logreport(self, report):<br \/>\n        &#8220;&#8221;&#8221;\u8bb0\u5f55\u6d4b\u8bd5\u7ed3\u679c&#8221;&#8221;&#8221;<br \/>\n        if report.when == &#8220;call&#8221;:<br \/>\n            if report.passed:<br \/>\n                self.results[&#8220;passed&#8221;] += 1<br \/>\n            elif report.failed:<br \/>\n                self.results[&#8220;failed&#8221;] += 1<br \/>\n            else:<br \/>\n                self.results[&#8220;skipped&#8221;] += 1<\/p>\n<p>    def pytest_sessionfinish(self, session, exitstatus):<br \/>\n        &#8220;&#8221;&#8221;\u8f93\u51fa\u81ea\u5b9a\u4e49\u62a5\u544a&#8221;&#8221;&#8221;<br \/>\n        print(&#8220;n&#8221; + &#8220;=&#8221;*50)<br \/>\n        print(&#8220;\u81ea\u5b9a\u4e49\u6d4b\u8bd5\u62a5\u544a&#8221;)<br \/>\n        print(&#8220;=&#8221;*50)<br \/>\n        print(f&#8221;\u901a\u8fc7\uff1a{self.results[&#8216;passed&#8217;]}&#8221;)<br \/>\n        print(f&#8221;\u5931\u8d25\uff1a{self.results[&#8216;failed&#8217;]}&#8221;)<br \/>\n        print(f&#8221;\u8df3\u8fc7\uff1a{self.results[&#8216;skipped&#8217;]}&#8221;)<br \/>\n        print(&#8220;=&#8221;*50)<\/p>\n<h1>\u6ce8\u518c\u63d2\u4ef6<\/h1>\n<p>@pytest.fixture<br \/>\ndef custom_plugin(request):<br \/>\n    return CustomPlugin()<br \/>\n<code>`<\/code><\/p>\n<h3>\u4f7f\u7528\u63d2\u4ef6<\/h3>\n<p><code>`<\/code>bash<\/p>\n<h1>\u65b9\u5f0f1\uff1a\u547d\u4ee4\u884c\u6307\u5b9a<\/h1>\n<p>pytest -p pytest_custom_plugin<\/p>\n<h1>\u65b9\u5f0f2\uff1a\u914d\u7f6e\u6587\u4ef6<\/h1>\n<h1>pytest.ini<\/h1>\n<p>[pytest]<br \/>\naddopts = -p pytest_custom_plugin<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u4e94\u3001Allure\u62a5\u544a\u96c6\u6210<\/h2>\n<h3>\u5b89\u88c5\u914d\u7f6e<\/h3>\n<p><code>`<\/code>bash<br \/>\npip install allure-pytest<br \/>\n<code>`<\/code><\/p>\n<h3>\u57fa\u7840\u7528\u6cd5<\/h3>\n<p><code>`<\/code>python<br \/>\nimport pytest<br \/>\nimport allure<\/p>\n<p>@allure.feature(&#8220;\u7528\u6237\u7ba1\u7406&#8221;)<br \/>\n@allure.story(&#8220;\u767b\u5f55\u529f\u80fd&#8221;)<br \/>\n@allure.title(&#8220;\u9a8c\u8bc1\u7528\u6237\u767b\u5f55\u6210\u529f&#8221;)<br \/>\ndef test_login_success():<br \/>\n    &#8220;&#8221;&#8221;\u6d4b\u8bd5\u767b\u5f55\u6210\u529f&#8221;&#8221;&#8221;<br \/>\n    with allure.step(&#8220;\u6253\u5f00\u767b\u5f55\u9875\u9762&#8221;):<br \/>\n        # \u5b9e\u9645\u64cd\u4f5c<br \/>\n        pass<\/p>\n<p>    with allure.step(&#8220;\u8f93\u5165\u7528\u6237\u540d\u5bc6\u7801&#8221;):<br \/>\n        allure.attach(&#8220;\u7528\u6237\u540d&#8221;, &#8220;testuser&#8221;)<br \/>\n        allure.attach(&#8220;\u5bc6\u7801&#8221;, &#8220;password123&#8221;)<\/p>\n<p>    with allure.step(&#8220;\u70b9\u51fb\u767b\u5f55\u6309\u94ae&#8221;):<br \/>\n        pass<\/p>\n<p>    with allure.step(&#8220;\u9a8c\u8bc1\u767b\u5f55\u6210\u529f&#8221;):<br \/>\n        assert True<\/p>\n<p>@allure.feature(&#8220;\u7528\u6237\u7ba1\u7406&#8221;)<br \/>\n@allure.story(&#8220;\u767b\u5f55\u529f\u80fd&#8221;)<br \/>\n@allure.severity(allure.severity_level.CRITICAL)<br \/>\ndef test_login_failure():<br \/>\n    &#8220;&#8221;&#8221;\u6d4b\u8bd5\u767b\u5f55\u5931\u8d25&#8221;&#8221;&#8221;<br \/>\n    allure.attach.file(&#8220;screenshot.png&#8221;, name=&#8221;\u5931\u8d25\u622a\u56fe&#8221;, attachment_type=allure.attachment_type.PNG)<br \/>\n    assert False<br \/>\n<code>`<\/code><\/p>\n<h3>\u751f\u6210\u62a5\u544a<\/h3>\n<p><code>`<\/code>bash<\/p>\n<h1>\u8fd0\u884c\u6d4b\u8bd5\uff0c\u751f\u6210\u62a5\u544a\u6570\u636e<\/h1>\n<p>pytest &#8211;alluredir=.\/allure-results<\/p>\n<h1>\u542f\u52a8\u62a5\u544a\u670d\u52a1<\/h1>\n<p>allure serve .\/allure-results<\/p>\n<h1>\u751f\u6210\u9759\u6001\u62a5\u544a<\/h1>\n<p>allure generate .\/allure-results -o .\/allure-report<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u516d\u3001\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h2>\n<h3>\u5e76\u884c\u6d4b\u8bd5<\/h3>\n<p><code>`<\/code>bash<br \/>\npip install pytest-xdist<\/p>\n<h1>\u4f7f\u7528\u591a\u8fdb\u7a0b\u5e76\u884c<\/h1>\n<p>pytest -n auto  # \u81ea\u52a8\u68c0\u6d4bCPU\u6838\u6570<br \/>\npytest -n 4     # \u4f7f\u75284\u4e2a\u8fdb\u7a0b<br \/>\n<code>`<\/code><\/p>\n<h3>\u53ea\u8fd0\u884c\u5931\u8d25\u7684\u6d4b\u8bd5<\/h3>\n<p><code>`<\/code>bash<\/p>\n<h1>\u7b2c\u4e00\u6b21\u8fd0\u884c\uff08\u6709\u5931\u8d25\uff09<\/h1>\n<p>pytest<\/p>\n<h1>\u53ea\u91cd\u65b0\u8fd0\u884c\u5931\u8d25\u7684\u6d4b\u8bd5<\/h1>\n<p>pytest &#8211;lf<\/p>\n<h1>\u5148\u8fd0\u884c\u5931\u8d25\u7684\uff0c\u518d\u8fd0\u884c\u5176\u4ed6\u7684<\/h1>\n<p>pytest &#8211;ff<br \/>\n<code>`<\/code><\/p>\n<h3>\u5feb\u901f\u5931\u8d25<\/h3>\n<p><code>`<\/code>bash<\/p>\n<h1>\u9047\u5230\u7b2c\u4e00\u4e2a\u5931\u8d25\u5c31\u505c\u6b62<\/h1>\n<p>pytest -x<\/p>\n<h1>N\u6b21\u5931\u8d25\u540e\u505c\u6b62<\/h1>\n<p>pytest &#8211;maxfail=3<br \/>\n<code>`<\/code><\/p>\n<h3>\u6d4b\u8bd5\u5206\u7ec4<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>\u6309\u6807\u8bb0\u5206\u7ec4\u6d4b\u8bd5<\/h1>\n<p>@pytest.mark.slow<br \/>\ndef test_slow_1():<br \/>\n    pass<\/p>\n<p>@pytest.mark.fast<br \/>\ndef test_fast_1():<br \/>\n    pass<\/p>\n<h1>\u8fd0\u884c\u7279\u5b9a\u6807\u8bb0\u7684\u6d4b\u8bd5<\/h1>\n<p>pytest -m slow    # \u53ea\u8fd0\u884cslow\u6807\u8bb0\u7684\u6d4b\u8bd5<br \/>\npytest -m &#8220;not slow&#8221;  # \u8fd0\u884c\u9664slow\u5916\u7684\u6d4b\u8bd5<br \/>\npytest -m &#8220;slow and fast&#8221;  # \u540c\u65f6\u6709\u8fd9\u4e24\u4e2a\u6807\u8bb0\u7684\u6d4b\u8bd5<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u4e03\u3001\u5b9e\u6218\uff1a\u5b8c\u6574\u6d4b\u8bd5\u6846\u67b6\u793a\u4f8b<\/h2>\n<h3>\u9879\u76ee\u7ed3\u6784<\/h3>\n<p><code>`<\/code><br \/>\ntests\/<br \/>\n\u251c\u2500\u2500 conftest.py          # \u5171\u4eabFixture\u548c\u94a9\u5b50<br \/>\n\u251c\u2500\u2500 test_data\/           # \u6d4b\u8bd5\u6570\u636e<br \/>\n\u2502   \u251c\u2500\u2500 users.yaml<br \/>\n\u2502   \u2514\u2500\u2500 products.yaml<br \/>\n\u251c\u2500\u2500 api\/                 # API\u6d4b\u8bd5<br \/>\n\u2502   \u251c\u2500\u2500 test_users.py<br \/>\n\u2502   \u251c\u2500\u2500 test_products.py<br \/>\n\u251c\u2500\u2500 web\/                 # Web\u6d4b\u8bd5<br \/>\n\u2502   \u251c\u2500\u2500 test_login.py<br \/>\n\u2502   \u2514\u2500\u2500 test_dashboard.py<br \/>\n\u2514\u2500\u2500 utils\/               # \u5de5\u5177\u51fd\u6570<br \/>\n    \u251c\u2500\u2500 api_client.py<br \/>\n    \u251c\u2500\u2500 db_helper.py<br \/>\n<code>`<\/code><\/p>\n<h3>conftest.py\u5b8c\u6574\u793a\u4f8b<\/h3>\n<p><code>`<\/code>python<br \/>\nimport pytest<br \/>\nimport yaml<br \/>\nimport requests<br \/>\nfrom selenium import webdriver<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<\/p>\n<p>def load_yaml_data(file_path):<br \/>\n    &#8220;&#8221;&#8221;\u52a0\u8f7dYAML\u6d4b\u8bd5\u6570\u636e&#8221;&#8221;&#8221;<br \/>\n    with open(file_path, &#8220;r&#8221;, encoding=&#8221;utf-8&#8243;) as f:<br \/>\n        return yaml.safe_load(f)<\/p>\n<h1>============ Session\u7ea7\u522bFixture ============<\/h1>\n<p>@pytest.fixture(scope=&#8221;session&#8221;)<br \/>\ndef test_config():<br \/>\n    &#8220;&#8221;&#8221;\u5168\u5c40\u6d4b\u8bd5\u914d\u7f6e&#8221;&#8221;&#8221;<br \/>\n    return {<br \/>\n        &#8220;api_base_url&#8221;: &#8220;https:\/\/api.test.com&#8221;,<br \/>\n        &#8220;web_base_url&#8221;: &#8220;https:\/\/web.test.com&#8221;,<br \/>\n        &#8220;timeout&#8221;: 30,<br \/>\n        &#8220;retry&#8221;: 3<br \/>\n    }<\/p>\n<p>@pytest.fixture(scope=&#8221;session&#8221;)<br \/>\ndef api_session():<br \/>\n    &#8220;&#8221;&#8221;API\u8bf7\u6c42Session&#8221;&#8221;&#8221;<br \/>\n    session = requests.Session()<br \/>\n    session.headers.update({&#8220;Content-Type&#8221;: &#8220;application\/json&#8221;})<br \/>\n    yield session<br \/>\n    session.close()<\/p>\n<h1>============ Module\u7ea7\u522bFixture ============<\/h1>\n<p>@pytest.fixture(scope=&#8221;module&#8221;)<br \/>\ndef user_data():<br \/>\n    &#8220;&#8221;&#8221;\u7528\u6237\u6d4b\u8bd5\u6570\u636e&#8221;&#8221;&#8221;<br \/>\n    return load_yaml_data(&#8220;test_data\/users.yaml&#8221;)<\/p>\n<h1>============ Function\u7ea7\u522bFixture ============<\/h1>\n<p>@pytest.fixture<br \/>\ndef browser():<br \/>\n    &#8220;&#8221;&#8221;Web\u6d4f\u89c8\u5668Driver&#8221;&#8221;&#8221;<br \/>\n    options = webdriver.ChromeOptions()<br \/>\n    options.add_argument(&#8220;&#8211;headless&#8221;)<br \/>\n    driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)<br \/>\n    driver.maximize_window()<br \/>\n    yield driver<br \/>\n    driver.quit()<\/p>\n<p>@pytest.fixture<br \/>\ndef auth_token(api_session, test_config):<br \/>\n    &#8220;&#8221;&#8221;\u83b7\u53d6\u8ba4\u8bc1Token&#8221;&#8221;&#8221;<br \/>\n    resp = api_session.post(<br \/>\n        f&#8221;{test_config[&#8216;api_base_url&#8217;]}\/auth\/login&#8221;,<br \/>\n        json={&#8220;username&#8221;: &#8220;test&#8221;, &#8220;password&#8221;: &#8220;test&#8221;}<br \/>\n    )<br \/>\n    return resp.json()[&#8220;token&#8221;]<\/p>\n<h1>============ \u94a9\u5b50\u51fd\u6570 ============<\/h1>\n<p>def pytest_configure(config):<br \/>\n    &#8220;&#8221;&#8221;\u914d\u7f6e\u9636\u6bb5&#8221;&#8221;&#8221;<br \/>\n    config.addinivalue_line(&#8220;markers&#8221;, &#8220;api: API\u6d4b\u8bd5&#8221;)<br \/>\n    config.addinivalue_line(&#8220;markers&#8221;, &#8220;web: Web\u6d4b\u8bd5&#8221;)<br \/>\n    config.addinivalue_line(&#8220;markers&#8221;, &#8220;slow: \u6162\u6d4b\u8bd5\uff0c\u9ed8\u8ba4\u8df3\u8fc7&#8221;)<\/p>\n<p>def pytest_collection_modifyitems(config, items):<br \/>\n    &#8220;&#8221;&#8221;\u4fee\u6539\u6d4b\u8bd5\u5217\u8868&#8221;&#8221;&#8221;<br \/>\n    # \u6dfb\u52a0\u6d4b\u8bd5\u540d\u79f0\u7684\u4e2d\u6587ID\u652f\u6301<br \/>\n    for item in items:<br \/>\n        item._nodeid = item._nodeid.encode(&#8220;utf-8&#8221;).decode(&#8220;unicode_escape&#8221;)<br \/>\n<code>`<\/code><\/p>\n<h3>\u6d4b\u8bd5\u6587\u4ef6\u793a\u4f8b<\/h3>\n<p><code>`<\/code>python<br \/>\nimport pytest<br \/>\nimport allure<\/p>\n<p>@allure.feature(&#8220;\u7528\u6237\u6a21\u5757&#8221;)<br \/>\nclass TestUsers:<\/p>\n<p>    @pytest.mark.api<br \/>\n    @allure.story(&#8220;\u83b7\u53d6\u7528\u6237\u5217\u8868&#8221;)<br \/>\n    def test_get_users(self, api_session, test_config, auth_token):<br \/>\n        &#8220;&#8221;&#8221;\u6d4b\u8bd5\u83b7\u53d6\u7528\u6237\u5217\u8868&#8221;&#8221;&#8221;<br \/>\n        headers = {&#8220;Authorization&#8221;: f&#8221;Bearer {auth_token}&#8221;}<br \/>\n        resp = api_session.get(<br \/>\n            f&#8221;{test_config[&#8216;api_base_url&#8217;]}\/users&#8221;,<br \/>\n            headers=headers<br \/>\n        )<br \/>\n        assert resp.status_code == 200<br \/>\n        assert len(resp.json()) &gt; 0<\/p>\n<p>    @pytest.mark.api<br \/>\n    @pytest.mark.parametrize(&#8220;user_id&#8221;, [1, 2, 3])<br \/>\n    @allure.story(&#8220;\u83b7\u53d6\u5355\u4e2a\u7528\u6237&#8221;)<br \/>\n    def test_get_user(self, api_session, test_config, auth_token, user_id):<br \/>\n        &#8220;&#8221;&#8221;\u6d4b\u8bd5\u83b7\u53d6\u5355\u4e2a\u7528\u6237&#8221;&#8221;&#8221;<br \/>\n        headers = {&#8220;Authorization&#8221;: f&#8221;Bearer {auth_token}&#8221;}<br \/>\n        resp = api_session.get(<br \/>\n            f&#8221;{test_config[&#8216;api_base_url&#8217;]}\/users\/{user_id}&#8221;,<br \/>\n            headers=headers<br \/>\n        )<br \/>\n        assert resp.status_code == 200<br \/>\n        assert &#8220;id&#8221; in resp.json()<\/p>\n<p>    @pytest.mark.web<br \/>\n    @allure.story(&#8220;\u7528\u6237\u767b\u5f55\u9875\u9762&#8221;)<br \/>\n    def test_login_page(self, browser, test_config):<br \/>\n        &#8220;&#8221;&#8221;\u6d4b\u8bd5\u767b\u5f55\u9875\u9762&#8221;&#8221;&#8221;<br \/>\n        browser.get(f&#8221;{test_config[&#8216;web_base_url&#8217;]}\/login&#8221;)<br \/>\n        assert &#8220;\u767b\u5f55&#8221; in browser.title<\/p>\n<p>        username = browser.find_element(&#8220;id&#8221;, &#8220;username&#8221;)<br \/>\n        password = browser.find_element(&#8220;id&#8221;, &#8220;password&#8221;)<\/p>\n<p>        username.send_keys(&#8220;testuser&#8221;)<br \/>\n        password.send_keys(&#8220;password&#8221;)<br \/>\n        browser.find_element(&#8220;id&#8221;, &#8220;submit&#8221;).click()<\/p>\n<p>        assert &#8220;dashboard&#8221; in browser.current_url<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u516b\u3001Pytest\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p><code>`<\/code>ini<\/p>\n<h1>pytest.ini<\/h1>\n<p>[pytest]<\/p>\n<h1>\u6d4b\u8bd5\u76ee\u5f55<\/h1>\n<p>testpaths = tests<\/p>\n<h1>\u6d4b\u8bd5\u6587\u4ef6\u547d\u540d\u6a21\u5f0f<\/h1>\n<p>python_files = test_*.py *_test.py<br \/>\npython_classes = Test*<br \/>\npython_functions = test_*<\/p>\n<h1>\u547d\u4ee4\u884c\u9009\u9879<\/h1>\n<p>addopts =<br \/>\n    -v<br \/>\n    &#8211;tb=short<br \/>\n    &#8211;strict-markers<br \/>\n    &#8211;alluredir=.\/allure-results<\/p>\n<h1>\u6807\u8bb0\u6ce8\u518c<\/h1>\n<p>markers =<br \/>\n    api: API\u63a5\u53e3\u6d4b\u8bd5<br \/>\n    web: Web\u9875\u9762\u6d4b\u8bd5<br \/>\n    slow: \u6162\u6d4b\u8bd5<br \/>\n    smoke: \u5192\u70df\u6d4b\u8bd5<\/p>\n<h1>\u65e5\u5fd7\u914d\u7f6e<\/h1>\n<p>log_cli = true<br \/>\nlog_cli_level = INFO<br \/>\nlog_file = pytest.log<br \/>\nlog_file_level = DEBUG<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Pytest\u9ad8\u7ea7\u529f\u80fd\u603b\u7ed3\uff1a<\/p>\n<p>| \u529f\u80fd | \u7528\u9014 |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;|<br \/>\n| \u53c2\u6570\u5316 | \u51cf\u5c11\u91cd\u590d\u4ee3\u7801\uff0c\u8986\u76d6\u66f4\u591a\u573a\u666f |<br \/>\n| Fixture\u4f5c\u7528\u57df | \u63a7\u5236\u8d44\u6e90\u521b\u5efa\u65f6\u673a\uff0c\u63d0\u9ad8\u6548\u7387 |<br \/>\n| \u94a9\u5b50\u51fd\u6570 | \u81ea\u5b9a\u4e49\u6d4b\u8bd5\u6d41\u7a0b\uff0c\u6269\u5c55\u529f\u80fd |<br \/>\n| \u63d2\u4ef6\u5f00\u53d1 | \u6df1\u5ea6\u5b9a\u5236\uff0c\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42 |<br \/>\n| Allure\u62a5\u544a | \u7f8e\u5316\u6d4b\u8bd5\u62a5\u544a\uff0c\u53ef\u89c6\u5316\u5c55\u793a |<br \/>\n| pytest-xdist | \u5e76\u884c\u6d4b\u8bd5\uff0c\u63d0\u5347\u901f\u5ea6 |<\/p>\n<p>\u638c\u63e1\u8fd9\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u4f60\u7684Pytest\u6846\u67b6\u4f1a\u66f4\u5f3a\u5927\u3001\u66f4\u4e13\u4e1a\u3002<\/p>\n<p>&#8212;<\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<ul>\n<li>Pytest\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/docs.pytest.org\/<\/li>\n<li>Allure\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/docs.qameta.io\/allure\/<\/li>\n<li>pytest-xdist\uff1ahttps:\/\/pytest-xdist.readthedocs.io\/<\/li>\n<li>RunnerGo\u535a\u5ba2\u7cfb\u5217<\/li>\n<\/ul>\n<p>&#8212;<\/p>\n<p><strong>\u4e0b\u671f\u9884\u544a\uff1aPostman\u9ad8\u7ea7\u6280\u5de7\u63ed\u79d8 &#8211; \u4ece\u63a5\u53e3\u6d4b\u8bd5\u5230\u81ea\u52a8\u5316Mock\u670d\u52a1\u642d\u5efa<\/strong><\/p>\n<p>&gt; \u4f5c\u8005\uff1a\u963f\u6cfddebug\uff0c5\u5e74Python\u6d4b\u8bd5\u7ecf\u9a8c\uff0c\u4e13\u6ce8\u4e8ePytest\u6846\u67b6\u548c\u81ea\u52a8\u5316\u6d4b\u8bd5\u67b6\u6784\u8bbe\u8ba1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pytest\u9ad8\u7ea7\u7528\u6cd5\u5168\u653b\u7565 &#8211; \u53c2\u6570\u5316\u3001\u94a9\u5b50\u51fd\u6570\u4e0e\u63d2\u4ef6\u5f00\u53d1\u5b9e\u6218 &gt; \u4f5c\u8005\uff1a\u963f\u6cfddebug &#8212; \u524d\u8a00 \u5927\u5bb6\u597d\uff0c\u6211\u662f\u963f\u6cfddebug\u3002\u505aPython\u81ea\u52a8\u5316\u6d4b\u8bd55\u5e74\u4e86\uff0cPytes&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33"}],"version-history":[{"count":0,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}