gpt4 book ai didi

python - 在pytest中并行运行参数化测试

转载 作者:太空狗 更新时间:2023-10-30 00:14:13 24 4
gpt4 key购买 nike

我想并行运行参数化测试函数。这适用于并发测试场景。同一测试用例在设备中以不同参数并行运行。完成一个测试函数的所有参数化变体后,我想继续下一个。

如果我们以这个简单的例子为例,我想并行运行 test_even 的所有 4 个实例,然后移动到 test_odd。

@pytest.mark.parametrize("x", range(4))
def test_even(x):
assert x % 2 == 0
@pytest.mark.parametrize("x", range(4))
def test_odd(x):
assert x % 2 != 0

可以在pytest中做吗?我检查了 xdist,但找不到这种类型的支持。有人可以就如何在 pytest 中实现这一点给出一些指示吗?

最佳答案

我建议使用 xdist ,从他们的网站上可以:

The pytest-xdist plugin extends pytest with new test execution modes, the most used being distributing tests across multiple CPUs to speed up test execution...

默认情况下,它还会并行化您的参数化测试。

这是一个例子:

import pytest

@pytest.mark.parametrize("foo", range(10))
def test_bar(foo):
assert True
pytest -n 2 -v
================================================================================================ test session starts ================================================================================================
platform linux -- Python 3.10.4, pytest-7.1.2, pluggy-1.0.0 -- /tmp/test/venv/bin/python3
cachedir: .pytest_cache
rootdir: /tmp/test
plugins: xdist-2.5.0, forked-1.4.0
[gw0] linux Python 3.10.4 cwd: /tmp/test
[gw1] linux Python 3.10.4 cwd: /tmp/test
[gw0] Python 3.10.4 (main, Apr 2 2022, 09:04:19) [GCC 11.2.0]
[gw1] Python 3.10.4 (main, Apr 2 2022, 09:04:19) [GCC 11.2.0]
gw0 [10] / gw1 [10]
scheduling tests via LoadScheduling

test/test_foo.py::test_bar[1]
test/test_foo.py::test_bar[0]
[gw1] [ 10%] PASSED test/test_foo.py::test_bar[1]
[gw0] [ 20%] PASSED test/test_foo.py::test_bar[0]
test/test_foo.py::test_bar[3]
[gw1] [ 30%] PASSED test/test_foo.py::test_bar[3]
test/test_foo.py::test_bar[2]
test/test_foo.py::test_bar[4]
[gw0] [ 40%] PASSED test/test_foo.py::test_bar[2]
[gw1] [ 50%] PASSED test/test_foo.py::test_bar[4]
test/test_foo.py::test_bar[6]
test/test_foo.py::test_bar[5]
[gw1] [ 60%] PASSED test/test_foo.py::test_bar[6]
[gw0] [ 70%] PASSED test/test_foo.py::test_bar[5]
test/test_foo.py::test_bar[8]
test/test_foo.py::test_bar[7]
[gw1] [ 80%] PASSED test/test_foo.py::test_bar[8]
test/test_foo.py::test_bar[9]
[gw0] [ 90%] PASSED test/test_foo.py::test_bar[7]
[gw1] [100%] PASSED test/test_foo.py::test_bar[9]

================================================================================================ 10 passed in 0.28s =================================================================================================

关于python - 在pytest中并行运行参数化测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29560212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com