我想并行运行参数化测试功能.这适用于并发测试场景.相同的测试用例与设备中的不同参数并行运行.完成一个测试函数的所有参数化变体后,我想继续下一个.
如果我们采用这个简单的例子,我想平行运行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
Run Code Online (Sandbox Code Playgroud)
在pytest中可以做到吗?我查了xdist,但找不到这种支持.有人可以就如何在pytest中实现这一点给出一些指示吗?
我建议使用xdist,从他们的网站来看它确实如此:
pytest-xdist 插件使用新的测试执行模式扩展了 pytest,最常用的是将测试分布在多个 CPU 上以加快测试执行速度...
默认情况下,它还会并行化您的参数化测试。
这是一个例子:
import pytest
@pytest.mark.parametrize("foo", range(10))
def test_bar(foo):
assert True
Run Code Online (Sandbox Code Playgroud)
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 =================================================================================================
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1704 次 |
| 最近记录: |