pytest实现多进程与多线程运行

1、pytest-parallel

  安装: pip install pytest-parallel

  常用参数配置:

  --workers=n:多进程运行需要加此参数,  n是进程数。默认为1

  --tests-per-worker=n:多线程需要添加此参数,n是线程数

  如果两个参数都配置了,就是进程并行,每个进程最多n个线程,总线程数:进程数*线程数

  注意:在windows上进程数永远为1。

     需要使用 if __name__ == "__main__":,在dos中运行会报错

  实现:

  

import pytest
def test_03(start,open_web1):
    print('测试用例3操作')
def test_04(start,open_web1):
    print('测试用例4操作')

if __name__ == "__main__":
    pytest.main(["-s", "test_1.py",'--workers=2', '--tests-per-worker=4'])

2、pytest-xdist

  安装:pip install pytest-xdist

  不支持多线程

  常用参数配置:

  -n=*:*代表进程数

  

注意:

    1、pytest-parallel加了多线程处理后,最后执行时间是运行时间最长的线程的时间。

    2、在windows下想用多进程的选pytst-xdist; 想用多线程的选pytest-parallel

原文地址:https://www.cnblogs.com/aiyumo/p/13451697.html