0
点赞
收藏
分享

微信扫一扫

Pytest----多进程并行执行自动化测试脚本

1、不使用并发执行用例方式

如下有十个用例,每个用例中等待5秒,使用pytest顺序执行

import time

def test_testcase_01():
time.sleep(5)

def test_testcase_02():
time.sleep(5)

def test_testcase_03():
time.sleep(5)

def test_testcase_04():
time.sleep(5)

def test_testcase_05():
time.sleep(5)

def test_testcase_06():
time.sleep(5)

def test_testcase_07():
time.sleep(5)

def test_testcase_08():
time.sleep(5)

def test_testcase_09():
time.sleep(5)

def test_testcase_10():
time.sleep(5)

执行结果如下:耗时50秒

Pytest----多进程并行执行自动化测试脚本_当前目录

2、使用并发执行方式

  • 安装pytest-xdist插件 pip install pytest-xdist
  • 使用 pytest -n auto 默认自动检查系统cpu个数,然后进行并发
    执行结果如下:
  • Pytest----多进程并行执行自动化测试脚本_python_02

  • 也可以指定并发数 pytest -n 2 即并发数2执行
    执行结果如下:
  • Pytest----多进程并行执行自动化测试脚本_python_03

  • 在当前目录下创建一个conftest.py,内容如下,然后使用pytest -n auto

import pytest
import time

@pytest.fixture(scope="session",autouse=True)
def fixture_demo():
time.sleep(10)

执行结果如下:

Pytest----多进程并行执行自动化测试脚本_当前目录_04


和上面的结果对比分析可以发现,在并发执行的场景下,pytest仍然遵守session级别的fixture只执行一次的原则,因为和上次执行多耗时10秒,而此次conftest.py中的fixture等待了10秒,所以说明这个fixture只执行了一次


举报

相关推荐

0 条评论