gpt4 book ai didi

python - 如何在 pytest 中两次使用参数化的依赖 fixture ?

转载 作者:太空狗 更新时间:2023-10-29 21:59:08 25 4
gpt4 key购买 nike

我试图在单个测试中多次使用参数化 fixture ,目的是获取其所有值的笛卡尔积。

https://stackoverflow.com/a/39444098/102441展示了如何对一个简单的 fixture 执行此操作:

import pytest

@pytest.fixture(params=[0, 1, 2])
def first(request):
return request.param

second = first

# runs 3x3 = 9 times
def test_double_fixture(first, second):
assert False, '{} {}'.format(first, second)

但是,如果参数化来自依赖 fixture ,则此方法会失败:

import pytest

@pytest.fixture(params=[0, 1, 2])
def integer(request):
return request.param

@pytest.fixture
def squared_integer(integer):
return integer * integer

@pytest.fixture
def first(squared_integer):
return squared_integer

second = first

# runs only 3 times
def test_double_fixture(first, second):
assert False, '{} {}'.format(first, second)

我怎样才能像简单示例那样运行 3x3 测试?

最佳答案

这是 pytest 的正确行为。因为您在其他灯具内部使用了 integer。要了解会发生什么,请使用 --setup-show 标志检查 pytest。你会看到类似这样的东西:

        SETUP    F integer[0]
SETUP F squared_integer (fixtures used: integer)
SETUP F first (fixtures used: squared_integer)
SETUP F second (fixtures used: squared_integer)
Test/test_54044536_3.py::test_double_fixture[0] (fixtures used: first, integer, second, squared_integer)F
TEARDOWN F second
TEARDOWN F first
TEARDOWN F squared_integer
TEARDOWN F integer[0]

因此整数值仅适用于 squared_integer 函数。

为了回答您的问题,我们可以将您的代码重构为固定装置和一个函数。这看起来像:

import pytest

def squared_integer(integer):
return integer * integer


@pytest.fixture(params=[0, 1, 2])
def first(request):
return squared_integer(request.param)

second = first

# runs only 3 times
def test_double_fixture(first, second):
assert False, '{} {}'.format(first, second)

你将有 9 个这样的测试:

        SETUP    F first[0]
SETUP F second[0]
Test/test_54044536_2.py::test_double_fixture[0-0] (fixtures used: first, second)F
TEARDOWN F second[0]
TEARDOWN F first[0]
SETUP F first[0]
SETUP F second[1]
Test/test_54044536_2.py::test_double_fixture[0-1] (fixtures used: first, second)F
TEARDOWN F second[1]
TEARDOWN F first[0]
SETUP F first[0]
SETUP F second[2]
Test/test_54044536_2.py::test_double_fixture[0-2] (fixtures used: first, second)F
TEARDOWN F second[2]
TEARDOWN F first[0]
SETUP F first[1]
SETUP F second[0]
Test/test_54044536_2.py::test_double_fixture[1-0] (fixtures used: first, second)F
TEARDOWN F second[0]
TEARDOWN F first[1]
SETUP F first[1]
SETUP F second[1]
Test/test_54044536_2.py::test_double_fixture[1-1] (fixtures used: first, second)F
TEARDOWN F second[1]
TEARDOWN F first[1]
SETUP F first[1]
SETUP F second[2]
Test/test_54044536_2.py::test_double_fixture[1-2] (fixtures used: first, second)F
TEARDOWN F second[2]
TEARDOWN F first[1]
SETUP F first[2]
SETUP F second[0]
Test/test_54044536_2.py::test_double_fixture[2-0] (fixtures used: first, second)F
TEARDOWN F second[0]
TEARDOWN F first[2]
SETUP F first[2]
SETUP F second[1]
Test/test_54044536_2.py::test_double_fixture[2-1] (fixtures used: first, second)F
TEARDOWN F second[1]
TEARDOWN F first[2]
SETUP F first[2]
SETUP F second[2]
Test/test_54044536_2.py::test_double_fixture[2-2] (fixtures used: first, second)F
TEARDOWN F second[2]
TEARDOWN F first[2]

关于python - 如何在 pytest 中两次使用参数化的依赖 fixture ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54044536/

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