当前位置:   article > 正文

win-python-pytest(pytest-parallel/pytest-xdist)自动化测试多线程的验证_warning: the scripts py.test.exe and pytest.exe ar

warning: the scripts py.test.exe and pytest.exe are installed

win11下的python pytest多线程测试 

用到的测试脚本 demo.py

  1. import time
  2. import pytest
  3. def test_01():
  4. time.sleep(1)
  5. print('测试用例1操作')
  6. def test_02():
  7. time.sleep(1)
  8. print('测试用例2操作')
  9. def test_03():
  10. time.sleep(1)
  11. print('测试用例3操作')
  12. def test_04():
  13. time.sleep(1)
  14. print('测试用例4操作')
  15. def test_05():
  16. time.sleep(1)
  17. print('测试用例5操作')
  18. def test_06():
  19. time.sleep(1)
  20. print('测试用例6操作')

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

  1. pip install pytest-xdist
  2. 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.

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

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

解决办法:

(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

 

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

利用pytest-xdist :4进程运行

pytest -s -v demo.py -n 4

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

综上可得出:

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

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

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

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

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

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

  1. import time
  2. import pytest
  3. @pytest.mark.run(order=6)
  4. def test_01():
  5. time.sleep(1)
  6. print('测试用例1操作')
  7. @pytest.mark.run(order=5)
  8. def test_02():
  9. time.sleep(1)
  10. print('测试用例2操作')
  11. @pytest.mark.run(order=4)
  12. def test_03():
  13. time.sleep(1)
  14. print('测试用例3操作')
  15. @pytest.mark.run(order=3)
  16. def test_04():
  17. time.sleep(1)
  18. print('测试用例4操作')
  19. @pytest.mark.run(order=2)
  20. def test_05():
  21. time.sleep(1)
  22. print('测试用例5操作')
  23. @pytest.mark.run(order=1)
  24. def test_06():
  25. time.sleep(1)
  26. print('测试用例6操作')
  27. if __name__ == "__main__":
  28. # pytest.main(["-s", __file__, '--workers=1', '--tests-per-worker=4'])
  29. # pytest.main(['-s', __file__, '-n=4'])
  30. pytest.main(["-s", "-v", "demo.py"])
pytest -s -v [demo.py](http://demo.py) -n 4

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

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

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

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

具体错误为:

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/649586
推荐阅读
相关标签
  

闽ICP备14008679号