Pytest单元测试系列[v1.0.0][pytest-xdist]

并行运行测试用例

pytest-xdist:Run Tests in Parallel

[https://pypi.python.org/pypi/pytest-xdist]
在自动化测试中有些资源只能同时被一个测试用例访问,如果不需要同时使用同一个资源,那么测试用例便可以并行执行
执行命令pip install pytest-xdist安装插件

E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-xdist
Collecting pytest-xdist
  Downloading https://files.pythonhosted.org/packages/9f/cc/371b2e6dfbf4e8df07b04e310dd6ea0b3f367e257d1e6cb516b25bc4af1b/pytest_xdist-1.29.0-py2.py3-none-any.whl
Collecting pytest-forked (from pytest-xdist)
  Downloading https://files.pythonhosted.org/packages/3f/55/ef92c340e723495dbee91d749903d2b7950b49c501943296257246d7d880/pytest_forked-1.0.2-py2.py3-none-any.whl
Requirement already satisfied: six in c:\python37\lib\site-packages (from pytest-xdist) (1.12.0)
Requirement already satisfied: pytest>=4.4.0 in c:\python37\lib\site-packages (from pytest-xdist) (4.5.0)
Collecting execnet>=1.1 (from pytest-xdist)
  Downloading https://files.pythonhosted.org/packages/77/1a/f69e1f73bc36f55d3273afd1c52936def71ac67d9c5215be3a4ca3a45577/execnet-1.6.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=4.4.0->pytest-xdist) (41.0.1)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.8.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.4.1)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (19.1.0)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.11.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.3.0)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.1.7)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (7.0.0)
Collecting apipkg>=1.4 (from execnet>=1.1->pytest-xdist)
  Downloading https://files.pythonhosted.org/packages/67/08/4815a09603fc800209431bec5b8bd2acf2f95abdfb558a44a42507fb94da/apipkg-1.5-py2.py3-none-any.whl
Installing collected packages: pytest-forked, apipkg, execnet, pytest-xdist
Successfully installed apipkg-1.5 execnet-1.6.0 pytest-forked-1.0.2 pytest-xdist-1.29.0

使用pytest-xdist执行测试

E:\Programs\Python\Python_Pytest\TestScripts>pytest -n auto
================================ test session starts ================================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, repeat-0.8.0, forked-1.0.2, allure-pytest-2.6.3
gw0 [17] / gw1 [17] / gw2 [17] / gw3 [17]
...........FF..F.                                                                                                                                                    [100%]
=========================== FAILURES ================================================
________________________________________ test_true ____________________________________________
[gw2] win32 -- Python 3.7.3 c:\python37\python.exe

    def test_true():
>       assert not is_prime(7)
E       assert not True
E        +  where True = is_prime(7)

test_asserts.py:65: AssertionError
__________________________________________ test_add4 ___________________________________________
[gw3] win32 -- Python 3.7.3 c:\python37\python.exe

    def test_add4():
>       assert add(17,22) >= 50
E       assert 39 >= 50
E        +  where 39 = add(17, 22)

test_asserts.py:34: AssertionError
______________________________ test_not_equal _____________________________________________
[gw1] win32 -- Python 3.7.3 c:\python37\python.exe

    def test_not_equal():
>       assert (1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff

test_one.py:9: AssertionError
============================== 3 failed, 14 passed, 4 warnings in 2.20 seconds ===============================

参数说明

-n auto 自动侦测系统里的CPU数目
-n numprocesses 指定运行测试的处理器进程数

你可能感兴趣的:(Pytest单元测试系列[v1.0.0][pytest-xdist])