zoukankan      html  css  js  c++  java
  • 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官网》

  • 相关阅读:
    多产品代码架构
    PMBOK 指南 第四章 项目整合管理(4.1-4.3)
    PMBOK 指南 第三章 项目经理的角色
    PMBOK 指南 第二章 项目运行环境
    初识PMP PMBOK初解(指南第一章引论)
    c++ 拷贝构造函数(重点在内含指针的浅拷贝和深拷贝)
    C++中的try throw catch 异常处理
    source insight 3 常用设置
    C++ map insert 另一个map的子集
    PMP学习笔记--11项目干系人管理
  • 原文地址:https://www.cnblogs.com/my_captain/p/12790779.html
Copyright © 2011-2022 走看看