ToB企服应用市场:ToB评测及商务社交产业平台

标题: pytest分布式执行插件 pytest-xdist 的高级用法 [打印本页]

作者: 前进之路    时间: 2022-9-16 17:14
标题: pytest分布式执行插件 pytest-xdist 的高级用法
想要使用多个CPU核心来进行测试,可以使用 -n 参数( 或者 --numprocesses)
(使用8个核心来跑测试用例)
  1. pytest -n 8
复制代码
使用 -n auto 参数可以利用电脑的所有核心来跑测试用例
测试时使用的算法可以根据--dist命令参数定制:
  1. @pytest.mark.xdist_group(name="group1")
  2. def test1():
  3.     pass
  4. class TestA:
  5.     @pytest.mark.xdist_group("group1")
  6.     def test2():
  7.         pass-
复制代码
这将确保test1和TestA::test2将在同一个worker中运行。没有xdist_ group标记的测试在--dist=load模式下正常运行。
例子:
项目目录结构

xdist_test.py
  1. import logging
  2. import pytest
  3. class TestXdist(object):
  4.     @pytest.mark.xdist_group("group1")
  5.     def test_one(self):
  6.         logging.info("1")
  7.         assert True
  8.     @pytest.mark.xdist_group("group1")
  9.     def test_two(self):
  10.         logging.info("2")
  11.         assert True
  12.     @pytest.mark.xdist_group("group2")
  13.     def test_three(self):
  14.         logging.info("3")
  15.         assert True
  16.     @pytest.mark.xdist_group("group2")
  17.     def test_four(self):
  18.         logging.info("4")
  19.         assert True
复制代码
xdist_dummy_test.py
  1. import logging
  2. import pytest
  3. class TestXdist(object):
  4.     @pytest.mark.run(order=1)
  5.     @pytest.mark.xdist_group("group1")
  6.     def test_dummy_one(self):
  7.         logging.info("d1")
  8.         assert True
  9.     @pytest.mark.xdist_group("group1")
  10.     @pytest.mark.run(order=2)
  11.     def test_dummy_two(self):
  12.         logging.info("d2")
  13.         assert True
  14.     @pytest.mark.run(order=3)
  15.     @pytest.mark.xdist_group("group2")
  16.     def test_dummy_three(self):
  17.         logging.info("d3")
  18.         assert True
  19.     @pytest.mark.xdist_group("group2")
  20.     @pytest.mark.run(order=4)
  21.     def test_dummy_four(self):
  22.         logging.info("d4")
  23.         assert True
复制代码
  1. (venv) ➜  pytest pytest -n auto --dist loadscope
  2. =========================================================================== test session starts ===========================================================================
  3. platform darwin -- Python 3.8.9, pytest-7.1.2, pluggy-1.0.0
  4. rootdir: /Users/spock/PycharmProjects/pytest, configfile: pytest.ini
  5. plugins: xdist-2.5.0, forked-1.4.0, ordering-0.6
  6. [gw0] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  7. [gw1] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  8. [gw2] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  9. [gw3] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  10. [gw4] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  11. [gw5] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  12. [gw6] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  13. [gw7] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  14. gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8]
  15. scheduling tests via LoadScopeScheduling
  16. test2/xdist_dummy_test.py::TestXdist::test_dummy_one
  17. test/xdist_test.py::TestXdist::test_one
  18. [gw0] [ 12%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_one
  19. [gw1] [ 25%] PASSED test/xdist_test.py::TestXdist::test_one
  20. test2/xdist_dummy_test.py::TestXdist::test_dummy_two
  21. test/xdist_test.py::TestXdist::test_two
  22. [gw1] [ 37%] PASSED test/xdist_test.py::TestXdist::test_two
  23. test/xdist_test.py::TestXdist::test_three
  24. [gw0] [ 50%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_two
  25. test2/xdist_dummy_test.py::TestXdist::test_dummy_three
  26. [gw1] [ 62%] PASSED test/xdist_test.py::TestXdist::test_three
  27. test/xdist_test.py::TestXdist::test_four
  28. [gw0] [ 75%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_three
  29. test2/xdist_dummy_test.py::TestXdist::test_dummy_four
  30. [gw1] [ 87%] PASSED test/xdist_test.py::TestXdist::test_four
  31. [gw0] [100%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_four
  32. ============================================================================ 8 passed in 0.40s ============================================================================
复制代码
  1. (venv) ➜  pytest pytest -n auto --dist loadfile
  2. =========================================================================== test session starts ===========================================================================
  3. platform darwin -- Python 3.8.9, pytest-7.1.2, pluggy-1.0.0
  4. rootdir: /Users/spock/PycharmProjects/pytest, configfile: pytest.ini
  5. plugins: xdist-2.5.0, forked-1.4.0, ordering-0.6
  6. [gw0] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  7. [gw1] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  8. [gw2] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  9. [gw3] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  10. [gw4] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  11. [gw5] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  12. [gw6] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  13. [gw7] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  14. gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8]
  15. scheduling tests via LoadFileScheduling
  16. test/xdist_test.py::TestXdist::test_one
  17. test2/xdist_dummy_test.py::TestXdist::test_dummy_one
  18. [gw1] [ 12%] PASSED test/xdist_test.py::TestXdist::test_one
  19. [gw0] [ 25%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_one
  20. test/xdist_test.py::TestXdist::test_two
  21. test2/xdist_dummy_test.py::TestXdist::test_dummy_two
  22. [gw1] [ 37%] PASSED test/xdist_test.py::TestXdist::test_two
  23. [gw0] [ 50%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_two
  24. test2/xdist_dummy_test.py::TestXdist::test_dummy_three
  25. test/xdist_test.py::TestXdist::test_three
  26. [gw1] [ 62%] PASSED test/xdist_test.py::TestXdist::test_three
  27. [gw0] [ 75%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_three
  28. test2/xdist_dummy_test.py::TestXdist::test_dummy_four
  29. test/xdist_test.py::TestXdist::test_four
  30. [gw1] [ 87%] PASSED test/xdist_test.py::TestXdist::test_four
  31. [gw0] [100%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_four
  32. ============================================================================ 8 passed in 0.38s ============================================================================
复制代码
  1. (venv) ➜  pytest pytest -n auto --dist loadgroup
  2. =========================================================================== test session starts ===========================================================================
  3. platform darwin -- Python 3.8.9, pytest-7.1.2, pluggy-1.0.0
  4. rootdir: /Users/spock/PycharmProjects/pytest, configfile: pytest.ini
  5. plugins: xdist-2.5.0, forked-1.4.0, ordering-0.6
  6. [gw0] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  7. [gw1] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  8. [gw2] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  9. [gw3] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  10. [gw4] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  11. [gw5] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  12. [gw6] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  13. [gw7] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
  14. gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8]
  15. scheduling tests via LoadGroupScheduling
  16. test2/xdist_dummy_test.py::TestXdist::test_dummy_three@group2
  17. test2/xdist_dummy_test.py::TestXdist::test_dummy_one@group1
  18. [gw1] [ 12%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_three@group2
  19. test2/xdist_dummy_test.py::TestXdist::test_dummy_four@group2
  20. [gw0] [ 25%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_one@group1
  21. [gw1] [ 37%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_four@group2
  22. test2/xdist_dummy_test.py::TestXdist::test_dummy_two@group1
  23. [gw0] [ 50%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_two@group1
  24. test/xdist_test.py::TestXdist::test_three@group2
  25. [gw1] [ 62%] PASSED test/xdist_test.py::TestXdist::test_three@group2
  26. test/xdist_test.py::TestXdist::test_four@group2
  27. [gw1] [ 75%] PASSED test/xdist_test.py::TestXdist::test_four@group2
  28. test/xdist_test.py::TestXdist::test_one@group1
  29. [gw0] [ 87%] PASSED test/xdist_test.py::TestXdist::test_one@group1
  30. test/xdist_test.py::TestXdist::test_two@group1
  31. [gw0] [100%] PASSED test/xdist_test.py::TestXdist::test_two@group1
  32. ============================================================================ 8 passed in 0.40s ============================================================================
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4