win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证

win11下的python pytest多线程测试 

用到的测试脚本 demo.py

import time

import pytest


def test_01():
    time.sleep(1)
    print('测试用例1操作')


def test_02():
    time.sleep(1)
    print('测试用例2操作')


def test_03():
    time.sleep(1)
    print('测试用例3操作')


def test_04():
    time.sleep(1)
    print('测试用例4操作')


def test_05():
    time.sleep(1)
    print('测试用例5操作')


def test_06():
    time.sleep(1)
    print('测试用例6操作')

前置条件:已经安装 pytest-xdist 和 pytest-parallel,python版本3.8

pip install pytest-xdist
pip install pytest-parallel

遇到的问题:

1.win的cmd中出现警告信息WARNING: The scripts py.test.exe and pytest.exe are installed in 'C:\Users\Franciz\AppData\Roaming\Python\Python38\Scripts' which is not on PATH.

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第1张图片

 解决办法:将此路径添加为环境变量,问题原因是python下载的第三方包放在了一个默认的目录下,这个目录不是环境变量,会导致你的一些命令在cmd中无效;此解决方法也适用于解决 pytest在命令行中使用显示 不是内部命令.

2.pytest-parallel 多线程 报错 INTERNALERRO,一般是因为你安装了最新版 0.0.11

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第2张图片

解决办法:

(1)回退版本0.0.10

pip uninstall pytest-parallel   
pip install "pytest-parallel==0.0.10"

成功运行的示例:

利用pytest-parallel:1进程4线程运行

pytest -s -v demo.py --workers 1 --tests-per-worker 4

 win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第3张图片

运行两次对比:可以发现用例执行的规则是乱序. 

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第4张图片

利用pytest-xdist :4进程运行

pytest -s -v demo.py -n 4

成功执行的示例(运行了两次),可以发现用例执行的规则是乱序.

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第5张图片

综上可得出:

如果想分布式/多线程 执行用例,用例设计必须遵循以下原则:

(1)、用例之间都是独立的,

(2)、用例a不要去依赖用例b,

(3)、用例执行没先后顺序,

(4)、随机都能执行每个用例都能独立运行成功每个用例都能重复运行,不影响其它用例。

3.增加插件:pytest-ordering 用于控制用例的执行顺序

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第6张图片

import time

import pytest


@pytest.mark.run(order=6)
def test_01():
    time.sleep(1)
    print('测试用例1操作')


@pytest.mark.run(order=5)
def test_02():
    time.sleep(1)
    print('测试用例2操作')


@pytest.mark.run(order=4)
def test_03():
    time.sleep(1)
    print('测试用例3操作')


@pytest.mark.run(order=3)
def test_04():
    time.sleep(1)
    print('测试用例4操作')


@pytest.mark.run(order=2)
def test_05():
    time.sleep(1)
    print('测试用例5操作')


@pytest.mark.run(order=1)
def test_06():
    time.sleep(1)
    print('测试用例6操作')


if __name__ == "__main__":
    # pytest.main(["-s", __file__, '--workers=1', '--tests-per-worker=4'])
    # pytest.main(['-s', __file__, '-n=4'])
    pytest.main(["-s", "-v", "demo.py"])

pytest -s -v [demo.py](http://demo.py) -n 4

多线程运行后查看结果,仍然是乱序执行case

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第7张图片

pytest -s -v [demo.py](http://demo.py) --workers 1 --tests-per-worker 4

多线程运行后查看结果,也是乱序执行case

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第8张图片

发现新问题 当前版本的 pytest-parallel 和pytest-xdist 在python3.9上不兼容

具体错误为:

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第9张图片

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/.../venv/lib/python3.9/site-packages/_pytest/main.py", line 255, in wrap_session
INTERNALERROR>     config.hook.pytest_sessionstart(session=session)
INTERNALERROR>   File "/.../venv/lib/python3.9/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/.../venv/lib/python3.9/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/.../venv/lib/python3.9/site-packages/pluggy/manager.py", line 84, in 
INTERNALERROR>     self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
INTERNALERROR>   File "/.../venv/lib/python3.9/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/.../venv/lib/python3.9/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/.../venv/lib/python3.9/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/.../venv/lib/python3.9/site-packages/pytest_parallel/__init__.py", line 219, in pytest_sessionstart
INTERNALERROR>     os.environ = ThreadLocalEnviron(os.environ)
INTERNALERROR>   File "/.../venv/lib/python3.9/site-packages/pytest_parallel/__init__.py", line 122, in __init__
INTERNALERROR>     env.putenv,
INTERNALERROR> AttributeError: '_Environ' object has no attribute 'putenv'

将demo.py复制一份为demo1.py      然后同时运行两个文件的用例,也是乱序

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第10张图片

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_第11张图片

你可能感兴趣的:(自动化测试,python,自动化)