在 VS Code 中运行参数化 pytest 有时会并行运行,是否有设置可以防止这种情况?

Gar*_*thC 7 pytest visual-studio-code

使用 VS Code 测试框架运行 pytest 时,根据我触发参数化测试的方式,它可以并行运行(多个测试会话),也可以串行运行(一个测试会话)。

以前(较旧的 VS Code 版本)我相信它总是串联运行。有这方面的设置吗?

当我从测试资源管理器 (1) 调用它时,它会串行运行。当我从编辑器 (2) 调用测试时,它会并行运行。

在此输入图像描述

我添加了 pytest arg '-rP' 来观察输出。环境:Python 3.8.10、pytest 6.2.5、VS Code 1.67.1、

测试示例:

import pytest
from datetime import datetime
from time import sleep

@pytest.mark.parametrize("descriptor", ["test1", "test2", "test3"])
def test_first(descriptor):
    print("start {0}: {1}".format(descriptor, datetime.now()))
    sleep(10)
    print("end {0}: {1}".format(descriptor, datetime.now()))
Run Code Online (Sandbox Code Playgroud)

从测试资源管理器 (1) 运行的示例输出:

============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: c:\Github\test
plugins: cov-3.0.0
collected 3 items

test_parallel_or_series.py ...                                           [100%]

=================================== PASSES ====================================
______________________________ test_first[test1] ______________________________
---------------------------- Captured stdout call -----------------------------
start test1: 2022-05-11 11:29:50.078870
end test1: 2022-05-11 11:30:00.083041
______________________________ test_first[test2] ______________________________
---------------------------- Captured stdout call -----------------------------
start test2: 2022-05-11 11:30:00.092023
end test2: 2022-05-11 11:30:10.093613
______________________________ test_first[test3] ______________________________
---------------------------- Captured stdout call -----------------------------
start test3: 2022-05-11 11:30:10.095437
end test3: 2022-05-11 11:30:20.099773
- generated xml file: C:\Users\User\AppData\Local\Temp\tmp-139884Q1NL33DkM8E.xml -
============================= 3 passed in 30.07s ==============================
Run Code Online (Sandbox Code Playgroud)

从编辑器 (2) 运行的示例输出:

============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: c:\Github\test
plugins: cov-3.0.0
rootdir: c:\Github\test
plugins: cov-3.0.0
collected 1 item

test_parallel_or_series.py collected 1 item

test_parallel_or_series.py rootdir: c:\Github\test
plugins: cov-3.0.0
collected 1 item

test_parallel_or_series.py ...                                             [100%]                                             [100%]                                             [100%]

=================================== PASSES ====================================
______________________________ test_first[test2] ______________________________
---------------------------- Captured stdout call -----------------------------
start test2: 2022-05-11 11:29:04.905957
end test2: 2022-05-11 11:29:14.913812


- generated xml file: C:\Users\User\AppData\Local\Temp\tmp-13988TVCwRoFnH3Ys.xml -
============================= 1 passed in 10.06s ==============================
=================================== PASSES ====================================
______________________________ test_first[test3] ______________________________
---------------------------- Captured stdout call -----------------------------
start test3: 2022-05-11 11:29:04.905957
end test3: 2022-05-11 11:29:14.913812


- generated xml file: C:\Users\User\AppData\Local\Temp\tmp-13988GLF4W4rdCe5N.xml -
=================================== PASSES ====================================
______________________________ test_first[test1] ______________________________
---------------------------- Captured stdout call -----------------------------
start test1: 2022-05-11 11:29:04.911930
end test1: 2022-05-11 11:29:14.913812
- generated xml file: C:\Users\User\AppData\Local\Temp\tmp-13988xfPcGZdiQWs9.xml -
============================= 1 passed in 10.06s ==============================
============================= 1 passed in 10.06s ==============================
Run Code Online (Sandbox Code Playgroud)

Gar*_*thC 1

即将发布的版本包括测试框架的重写。使用新框架运行测试不再导致观察到的行为。相反,测试会按预期在单个测试会话中连续运行。

您可以在新框架正式发布之前通过以下方式运行它:

将此设置添加到您的用户 settings.json "python.experiments.optInto": ["pythonTestAdapter"]

更多详细信息请参阅发行卡。感谢 @red888 在 vscode-python 存储库上报告了这一点。