詳解pytest分布式執(zhí)行插件?pytest-xdist?的高級用法
想要使用多個(gè)CPU核心來進(jìn)行測試,可以使用 -n 參數(shù)( 或者 --numprocesses)
(使用8個(gè)核心來跑測試用例)
pytest -n 8
使用 -n auto
參數(shù)可以利用電腦的所有核心來跑測試用例
測試時(shí)使用的算法可以根據(jù)--dist
命令參數(shù)定制:
--dist load
(默認(rèn)選項(xiàng)):給每個(gè)CPU核心隨機(jī)分配用例,不保證執(zhí)行順序。--dist loadscope
:對于測試函數(shù),測試按模塊分組,對于測試方法,測試按類分組。每組作為一個(gè)整體分配給可用的worker。這保證了組中的所有測試都在同一進(jìn)程中運(yùn)行。如果的模塊級或類級fixtures,這將非常有用。按類分組優(yōu)先于按模塊分組。--dist loadfile
: 測試用例按其所在文件分組。每組作為一個(gè)整體分配給可用的worker。這保證了文件中的所有測試都在同一個(gè)輔助進(jìn)程中運(yùn)行。--dist loadgroup
: 測試按xdist_group標(biāo)記分組。每組作為一個(gè)整體分配給可用的執(zhí)行器。這保證了具有相同xdist_ group名稱的所有測試都在同一個(gè)worker中運(yùn)行。
@pytest.mark.xdist_group(name="group1") def test1(): pass class TestA: @pytest.mark.xdist_group("group1") def test2(): pass-
這將確保test1和TestA::test2將在同一個(gè)worker中運(yùn)行。沒有xdist_ group標(biāo)記的測試在--dist=load模式下正常運(yùn)行。
--dist no
:正常的pytest執(zhí)行模式,一次運(yùn)行一個(gè)測試(完全沒有分發(fā))。
例子:
項(xiàng)目目錄結(jié)構(gòu)
xdist_test.py
import logging import pytest class TestXdist(object): @pytest.mark.xdist_group("group1") def test_one(self): logging.info("1") assert True @pytest.mark.xdist_group("group1") def test_two(self): logging.info("2") assert True @pytest.mark.xdist_group("group2") def test_three(self): logging.info("3") assert True @pytest.mark.xdist_group("group2") def test_four(self): logging.info("4") assert True
xdist_dummy_test.py
import logging import pytest class TestXdist(object): @pytest.mark.run(order=1) @pytest.mark.xdist_group("group1") def test_dummy_one(self): logging.info("d1") assert True @pytest.mark.xdist_group("group1") @pytest.mark.run(order=2) def test_dummy_two(self): logging.info("d2") assert True @pytest.mark.run(order=3) @pytest.mark.xdist_group("group2") def test_dummy_three(self): logging.info("d3") assert True @pytest.mark.xdist_group("group2") @pytest.mark.run(order=4) def test_dummy_four(self): logging.info("d4") assert True
(venv) ? pytest pytest -n auto --dist loadscope =========================================================================== test session starts =========================================================================== platform darwin -- Python 3.8.9, pytest-7.1.2, pluggy-1.0.0 rootdir: /Users/spock/PycharmProjects/pytest, configfile: pytest.ini plugins: xdist-2.5.0, forked-1.4.0, ordering-0.6 [gw0] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw1] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw2] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw3] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw4] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw5] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw6] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw7] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8] scheduling tests via LoadScopeScheduling test2/xdist_dummy_test.py::TestXdist::test_dummy_one test/xdist_test.py::TestXdist::test_one [gw0] [ 12%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_one [gw1] [ 25%] PASSED test/xdist_test.py::TestXdist::test_one test2/xdist_dummy_test.py::TestXdist::test_dummy_two test/xdist_test.py::TestXdist::test_two [gw1] [ 37%] PASSED test/xdist_test.py::TestXdist::test_two test/xdist_test.py::TestXdist::test_three [gw0] [ 50%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_two test2/xdist_dummy_test.py::TestXdist::test_dummy_three [gw1] [ 62%] PASSED test/xdist_test.py::TestXdist::test_three test/xdist_test.py::TestXdist::test_four [gw0] [ 75%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_three test2/xdist_dummy_test.py::TestXdist::test_dummy_four [gw1] [ 87%] PASSED test/xdist_test.py::TestXdist::test_four [gw0] [100%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_four ============================================================================ 8 passed in 0.40s ============================================================================
(venv) ? pytest pytest -n auto --dist loadfile =========================================================================== test session starts =========================================================================== platform darwin -- Python 3.8.9, pytest-7.1.2, pluggy-1.0.0 rootdir: /Users/spock/PycharmProjects/pytest, configfile: pytest.ini plugins: xdist-2.5.0, forked-1.4.0, ordering-0.6 [gw0] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw1] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw2] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw3] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw4] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw5] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw6] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw7] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8] scheduling tests via LoadFileScheduling test/xdist_test.py::TestXdist::test_one test2/xdist_dummy_test.py::TestXdist::test_dummy_one [gw1] [ 12%] PASSED test/xdist_test.py::TestXdist::test_one [gw0] [ 25%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_one test/xdist_test.py::TestXdist::test_two test2/xdist_dummy_test.py::TestXdist::test_dummy_two [gw1] [ 37%] PASSED test/xdist_test.py::TestXdist::test_two [gw0] [ 50%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_two test2/xdist_dummy_test.py::TestXdist::test_dummy_three test/xdist_test.py::TestXdist::test_three [gw1] [ 62%] PASSED test/xdist_test.py::TestXdist::test_three [gw0] [ 75%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_three test2/xdist_dummy_test.py::TestXdist::test_dummy_four test/xdist_test.py::TestXdist::test_four [gw1] [ 87%] PASSED test/xdist_test.py::TestXdist::test_four [gw0] [100%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_four ============================================================================ 8 passed in 0.38s ============================================================================
(venv) ? pytest pytest -n auto --dist loadgroup =========================================================================== test session starts =========================================================================== platform darwin -- Python 3.8.9, pytest-7.1.2, pluggy-1.0.0 rootdir: /Users/spock/PycharmProjects/pytest, configfile: pytest.ini plugins: xdist-2.5.0, forked-1.4.0, ordering-0.6 [gw0] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw1] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw2] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw3] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw4] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw5] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw6] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] [gw7] Python 3.8.9 (default, May 17 2022, 12:55:41) -- [Clang 13.1.6 (clang-1316.0.21.2.5)] gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8] scheduling tests via LoadGroupScheduling test2/xdist_dummy_test.py::TestXdist::test_dummy_three@group2 test2/xdist_dummy_test.py::TestXdist::test_dummy_one@group1 [gw1] [ 12%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_three@group2 test2/xdist_dummy_test.py::TestXdist::test_dummy_four@group2 [gw0] [ 25%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_one@group1 [gw1] [ 37%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_four@group2 test2/xdist_dummy_test.py::TestXdist::test_dummy_two@group1 [gw0] [ 50%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_two@group1 test/xdist_test.py::TestXdist::test_three@group2 [gw1] [ 62%] PASSED test/xdist_test.py::TestXdist::test_three@group2 test/xdist_test.py::TestXdist::test_four@group2 [gw1] [ 75%] PASSED test/xdist_test.py::TestXdist::test_four@group2 test/xdist_test.py::TestXdist::test_one@group1 [gw0] [ 87%] PASSED test/xdist_test.py::TestXdist::test_one@group1 test/xdist_test.py::TestXdist::test_two@group1 [gw0] [100%] PASSED test/xdist_test.py::TestXdist::test_two@group1 ============================================================================ 8 passed in 0.40s ============================================================================
到此這篇關(guān)于pytest分布式執(zhí)行插件 pytest-xdist 的高級用法的文章就介紹到這了,更多相關(guān)pytest分布式執(zhí)行插件 pytest-xdist內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VScode中不同目錄間python庫函數(shù)的調(diào)用
本文主要介紹了VScode中不同目錄間python庫函數(shù)的調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python異步爬蟲實(shí)現(xiàn)原理與知識總結(jié)
之前有很多小伙伴想看Python異步爬蟲的有關(guān)知識總結(jié),這次它來了,文中有非常詳細(xì)的代碼示例與注釋,即使對剛開始學(xué)python的小伙伴也很友好,,需要的朋友可以參考下2021-05-05python腳本調(diào)用iftop 統(tǒng)計(jì)業(yè)務(wù)應(yīng)用流量的思路詳解
這篇文章主要介紹了python腳本調(diào)用iftop 統(tǒng)計(jì)業(yè)務(wù)應(yīng)用流量的思路詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10解決Pyinstaller 打包exe文件 取消dos窗口(黑框框)的問題
今天小編就為大家分享一篇解決Pyinstaller 打包exe文件 取消dos窗口(黑框框)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06pytorch常用數(shù)據(jù)類型所占字節(jié)數(shù)對照表一覽
這篇文章主要介紹了pytorch常用數(shù)據(jù)類型所占字節(jié)數(shù)對照表一覽,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05python程序?qū)崿F(xiàn)BTC(比特幣)挖礦的完整代碼
這篇文章主要介紹了python程序?qū)崿F(xiàn)BTC(比特幣)挖礦的完整代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Python 3.8中實(shí)現(xiàn)functools.cached_property功能
這篇文章主要介紹了Python 3.8中實(shí)現(xiàn)functools.cached_property功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05