文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

详解pytest分布式执行插件 pytest-xdist 的高级用法

2024-04-02 19:55

关注

想要使用多个CPU核心来进行测试,可以使用 -n 参数( 或者 --numprocesses)
(使用8个核心来跑测试用例)

pytest -n 8

使用 -n auto 参数可以利用电脑的所有核心来跑测试用例
测试时使用的算法可以根据--dist命令参数定制:

@pytest.mark.xdist_group(name="group1")
def test1():
    pass

class TestA:
    @pytest.mark.xdist_group("group1")
    def test2():
        pass- 

这将确保test1和TestA::test2将在同一个worker中运行。没有xdist_ group标记的测试在--dist=load模式下正常运行。

例子:
项目目录结构

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 ============================================================================

到此这篇关于pytest分布式执行插件 pytest-xdist 的高级用法的文章就介绍到这了,更多相关pytest分布式执行插件 pytest-xdist内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯