python多线程执行测试用例_pytest多进程执行测试用例

pytest多进程执行测试用例

创建待测用例

import time

import pytest

def test_01():

print("test 1 begin")

assert 1==1

time.sleep(6)

print("test 1 begin")

def test_02():

print("test 2 begin")

time.sleep(6)

print("test 2 finish")

assert 0 == 1

print("test 2 assert")

调用pytest多进程执行测试用例

if __name__=="__main__":

pytest.main(['test_pytest.py','-n=auto'])

-n=auto根据cpu核心数选择进程数,使用此功能需要安装pytest-xdist

执行结果

============================= test session starts =============================

platform win32 -- Python 3.6.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1

rootdir: E:\PycharmProjects\untitled1\AutoTest\test

plugins: forked-1.3.0, html-2.1.1, metadata-1.10.0, xdist-2.1.0

gw0 I / gw1 I / gw2 I / gw3 I

gw0 [2] / gw1 [2] / gw2 [2] / gw3 [2]

.F [100%]

================================== FAILURES ===================================

___________________________________ test_02 ___________________________________

[gw1] win32 -- Python 3.6.7 E:\PycharmProjects\untitled1\venv\Scripts\python.exe

def test_02():

print("test 2 begin")

time.sleep(6)

print("test 2 finish")

> assert 0 == 1

E assert 0 == 1

test_pytest.py:13: AssertionError

---------------------------- Captured stdout call -----------------------------

test 2 begin

test 2 finish

=========================== short test summary info ===========================

FAILED test_pytest.py::test_02 - assert 0 == 1

========================= 1 failed, 1 passed in 8.78s =========================

Process finished with exit code 0

从结果看assert断言失败后的print(“test 2 assert”)没有被Capture

---------------------------- Captured stdout call -----------------------------

test 2 begin

test 2 finish

原文链接:https://blog.csdn.net/weixin_43555427/article/details/108413215

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytest中,可以使用pytest-parallel插件来支持多线程执行用例。该插件可以在mac或linux系统中运行,并通过设置--workers参数来指定线程数。然而,在Windows系统中,pytest-parallel只支持单个进程执行,即--workers参数只能设置为1。\[1\] 另外一个可选的插件是pytest-xdist,它支持多进程执行用例。pytest-xdist可以通过独特的测试执行模式扩展pytest,实现测试运行的并行化。它可以利用多个CPU或主机来加速测试执行,节省自动化回归测试的时间。此外,pytest-xdist还支持跨平台覆盖,可以在不同的Python解释器或不同的平台上并行运行测试。\[2\] 在使用多线程执行用例时,可能会遇到fixture函数执行多次的问题。特别是当fixture函数的范围设置为session时,会导致fixture函数在每个线程中都执行,从而导致一些问题,比如token过期。为了解决这个问题,可以考虑将fixture函数的范围设置为其他级别,如function或class,以确保在测试用例的session中仅执行一次。\[3\] #### 引用[.reference_title] - *1* [11、pytest多线程多进程执行用例](https://blog.csdn.net/weixin_43877527/article/details/128535493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [接口测试多线程执行用例-Pytest-xdist](https://blog.csdn.net/oqqChin12/article/details/127489055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值