概述

开启pytest之旅

环境依赖

pytest(7.4.0)

pytest-html(3.2.0)

pytest-ordering(0.6)

pytest-xdist(3.3.1)

allure-pytest (2.13.2)

目录

│ conftest.py fixtures
│ pytest.ini pytest
│ README.md
│ run.py
│ test_class.py
│ test_func.py
│ test_mark_func.py
│ test_simulate_many_case.py
├─report pytest-html
│ │ report.html
│ │
│ └─assets
│ style.css

脚本

 pytest默认是单进程执行用例脚本,当用例数量比较多时,单进程执行耗时会比较长。可以用pytest-xdist分布式执行用例。

 方式一:本地多进程执行   pytest -n X,X代表指定用于执行的cup个数,一般是服务器核数的1/2。X也可以指定为auto,即使用电脑的所有核数

 方式二:远程分布式执行

  (1)ssh 方式
   pytest -d --tx ssh=user_name@server_ip//python=指定服务器的python解释器路径//chdir=指定服务器的工作路径 --rsyncdir=本地需同步的目录 --tx sh=user_name@server_i   p//python=指定服务器的python解释器路径//chdir=指定服务器的工作路径
   如:pytest -d --rsyncdir=./ --tx ssh=root@123.57.138.224//python=/usr/python37/bin/python3.7//chdir=/usr/python-script
   前置条件:使用ssh方式前,需要配置免密登录服务器
    1.windows本机生成私钥、公钥,命令行执行:ssh-keygen,提示输入密码直接回车。密钥生成的目录C:/Users/dearw/.ssh/id_rsa(私钥),C:/Users/dearw/.ssh/id_rs    a.pub(公钥)
    2.将公钥拷贝到linux服务器,命令行执行:ssh-id-copy username@remote-server后,输入username的密码。拷贝到 cd ~/.ssh/authorized_keys
    3.检查是否设置成功,重新打开cmd窗口,输入:ssh username@remote-server回车,如果远程登录成功,则开通成功

  (2)socket方式
   前置条件:开启worker执行机的socket服务,python3 /usr/python37/lib/python3.7/site-packages/execnet/script/socketserver.py
   pytest -d -m smoke --rsyncdir G:/src/pytest-template --tx socket=123.57.138.224:5000 pytest-template,不用指定python编译器和工作目录chdir

 备注:未指定工作目录chdir时,目录为当前用户目录下创建pyexecnetcache,如/root/pyexecnetcache/

扩展

  1. pytest-suger,显示进度条,立即输出错误。pip install pytest-suger,安装后用pytest命令行执行,即生效。备注:pytest.main([])执行不生效
  2. pytest-tldr,简化stream输出,安装后用pytest命令行执行,即生效。备注:pytest.main([])执行生效