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