当前位置 : 主页 > 编程语言 > python >

pytest-xdist之其他用法:dist模式、运行方式、配置文件

来源:互联网 收集:自由互联 发布时间:2022-07-05
前言 在上面三篇文章中,我们尝试了使用pytest-xdist来做WEB分布式自动化测试、APP分布式自动化测试。在这篇文章中,对于pytest一些其他的语法,比如load模式、each模式、同步运行、直接

前言

在上面三篇文章中,我们尝试了使用pytest-xdist来做WEB分布式自动化测试、APP分布式自动化测试。在这篇文章中,对于pytest一些其他的语法,比如load模式、each模式、同步运行、直接运行、配置文件等做一说明

项目环境

角色

系统

Python版本

ip

master

Centos7.6

v3.8.0

192.168.0.109

worker1

Centos7.6

v3.8.0

192.168.0.126

worker2

Centos7.6

v3.8.0

192.168.0.136

项目结构

test_demo.py是一个计算乘法的程序,通过pytest参数化方法循环100次。在本例中,直接使用ssh的方式

Test_Demo
|--test_demo.py
|--pytest.ini
|--main.py

同步运行:无配置文件

pytest -d --tx 'ssh=root@192.168.0.126//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache' --tx 'ssh=root@192.168.0.136//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache' --rsyncdir ./TestDemo/

pytest-xdist之其他用法:dist模式、运行方式、配置文件_python

同步运行:有配置文件

现在我们往配置文件中加入
现在我们往配置文件pytest.ini中加入ssh的配置信息

# pytest.ini
[pytest]
addopts = --tx 'ssh=root@192.168.0.126//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache' --tx 'ssh=root@192.168.0.136//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache --rsyncdir ./

然后使用命令执行,注意使用配置文件的时候,系统所在路径是在工程TestDemo目录下,否则配置文件不会生效

pytest -d

pytest-xdist之其他用法:dist模式、运行方式、配置文件_python_02
还可以用--dist=load的方式运行

pytest --dist=load

pytest-xdist之其他用法:dist模式、运行方式、配置文件_python_03

最后以--dist=each的方式运行

pytest --dist=each

load和each的方式在这里看的比较清楚,很明显,each是每个worker都执行了一遍所有的用例
pytest-xdist之其他用法:dist模式、运行方式、配置文件_配置文件_04

直接运行:无配置文件

这里说的直接运行,指的是前一次已经同步一次了,在worker上的/opt/pyexecnetcache目录下已经存在了测试用例,因此直接可以运行

pytest -d --tx 'ssh=root@192.168.0.126//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache' --tx 'ssh=root@192.168.0.136//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache'

pytest-xdist之其他用法:dist模式、运行方式、配置文件_centos_05

直接运行:有配置文件

现在我们往配置文件pytest.ini中加入ssh的配置信息

# pytest.ini
[pytest]
addopts = --tx 'ssh=root@192.168.0.126//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache' --tx 'ssh=root@192.168.0.136//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache

然后运行命令

pytest -d

pytest-xdist之其他用法:dist模式、运行方式、配置文件_配置文件_06

还可以用--dist=load的方式运行

pytest --dist=load

pytest-xdist之其他用法:dist模式、运行方式、配置文件_python_07
最后以--dist=each的方式运行

pytest --dist=each

pytest-xdist之其他用法:dist模式、运行方式、配置文件_配置文件_08
load和each的方式在这里看的比较清楚,很明显,each是每个worker都执行了一遍所有的用例

直接运行:main.py

修改main.py,加入--dist参数

# main.py
import pytest

pytest.main([
"--dist", "load"
])

pytest-xdist之其他用法:dist模式、运行方式、配置文件_配置文件_09
这是为什么呢?是因为直接运行main.py不会加载pytest.ini中的配置,所以要把pytest.ini注释掉
pytest-xdist之其他用法:dist模式、运行方式、配置文件_python_10
然后修改main.py

# main.py
import pytest

pytest.main([
"--dist", "load"
"--tx", "ssh=root@192.168.0.126//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache",
"--tx", "ssh=root@192.168.0.136//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache"
])

pytest-xdist之其他用法:dist模式、运行方式、配置文件_python_11

还可以使用​​each​​

# main.py
import pytest

pytest.main([
"--dist", "each"
"--tx", "ssh=root@192.168.0.126//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache",
"--tx", "ssh=root@192.168.0.136//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache"
])

pytest-xdist之其他用法:dist模式、运行方式、配置文件_配置文件_12

配置rsync

在上面的例子中,是直接把这一串​​--tx 'ssh=root@192.168.0.126//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache' --tx 'ssh=root@192.168.0.136//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache --rsyncdir ./​​​放在配置文件中,如果想分开配置,也是可以的
修改pytest.ini,加入​​​rsyncdirs=./​​,表示同步的是当前父目录TestDemo下的所有文件

# pytest.ini
[pytest]
addopts = --tx ssh=root@192.168.0.126//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache --tx ssh=root@192.168.0.136//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache
rsyncdirs=./

pytest-xdist之其他用法:dist模式、运行方式、配置文件_配置文件_13
如果你想忽略某些文件,比如main.py,可以使用​​​rsyncignore​​

# pytest.ini
[pytest]
addopts = --tx ssh=root@192.168.0.126//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache --tx ssh=root@192.168.0.136//python=/opt/Python-3.8.0/bin/python3.8//chdir=/opt/pyexecnetcache
rsyncdirs=./
rsyncignore=./main.py

可以看到,两个worker中都没有同步main.py
master
pytest-xdist之其他用法:dist模式、运行方式、配置文件_配置文件_14
worker1
pytest-xdist之其他用法:dist模式、运行方式、配置文件_配置文件_15
worker2
pytest-xdist之其他用法:dist模式、运行方式、配置文件_python_16

参考文章

​​《pytest-xdist官网》​​


上一篇:selenium定位多个嵌套iframe
下一篇:没有了
网友评论