pytest-xdist的dist模式如何配置及运行?

2026-05-21 21:232阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1121个文字,预计阅读时间需要5分钟。

pytest-xdist的dist模式如何配置及运行?

前言:在上面三篇文章中,我们尝试了使用pytest-xdist来进行WEB分布式自动化测试、APP分布式自动化测试。在这篇文章中,我们将对pytest的一些其他语法进行介绍,例如load模式、each模式、同步运行、直接运行等。

前言

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

项目环境

角色

系统

Python版本

pytest-xdist的dist模式如何配置及运行?

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.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


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

pytest --dist=load

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

pytest --dist=each

load和each的方式在这里看的比较清楚,很明显,each是每个worker都执行了一遍所有的用例

直接运行:无配置文件

这里说的直接运行,指的是前一次已经同步一次了,在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.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

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

pytest --dist=load


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

pytest --dist=each


load和each的方式在这里看的比较清楚,很明显,each是每个worker都执行了一遍所有的用例

直接运行:main.py

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

# main.py
import pytest

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


这是为什么呢?是因为直接运行main.py不会加载pytest.ini中的配置,所以要把pytest.ini注释掉

然后修改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"
])

还可以使用​​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"
])

配置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=./


如果你想忽略某些文件,比如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

worker1

worker2

参考文章

​​《pytest-xdist官网》​​


本文共计1121个文字,预计阅读时间需要5分钟。

pytest-xdist的dist模式如何配置及运行?

前言:在上面三篇文章中,我们尝试了使用pytest-xdist来进行WEB分布式自动化测试、APP分布式自动化测试。在这篇文章中,我们将对pytest的一些其他语法进行介绍,例如load模式、each模式、同步运行、直接运行等。

前言

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

项目环境

角色

系统

Python版本

pytest-xdist的dist模式如何配置及运行?

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.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


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

pytest --dist=load

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

pytest --dist=each

load和each的方式在这里看的比较清楚,很明显,each是每个worker都执行了一遍所有的用例

直接运行:无配置文件

这里说的直接运行,指的是前一次已经同步一次了,在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.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

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

pytest --dist=load


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

pytest --dist=each


load和each的方式在这里看的比较清楚,很明显,each是每个worker都执行了一遍所有的用例

直接运行:main.py

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

# main.py
import pytest

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


这是为什么呢?是因为直接运行main.py不会加载pytest.ini中的配置,所以要把pytest.ini注释掉

然后修改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"
])

还可以使用​​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"
])

配置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=./


如果你想忽略某些文件,比如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

worker1

worker2

参考文章

​​《pytest-xdist官网》​​