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


前言

在上面三篇文章中,我們嘗試了使用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.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官網》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM