首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何制作参数化另一夹具的夹具

如何制作参数化另一夹具的夹具
EN

Stack Overflow用户
提问于 2021-01-21 00:44:59
回答 1查看 147关注 0票数 3

我有很多通过pytest管理的参数化的固定装置。

有时,我希望使用夹具的测试不必担心参数的应用。

是否可以制作一个参数化另一个夹具的夹具?

代码语言:javascript
复制
import pytest

class Foo:
    def __init__(self, a: int, b: int):
        pass

@pytest.fixture
def foo(a: int, b: int) -> Foo:
    return Foo(a, b)

@pytest.fixture
@pytest.mark.parametrize("a, b", [(2, 3)])  # How can I do this?
def fixture_parametrizing_another_fixture(foo: Foo) -> Foo:
    return foo

# I don't want to parametrize here, I want the fixture already set up
def test_with_second_fixture(fixture_parametrizing_another_fixture: Foo):
    pass
EN

回答 1

Stack Overflow用户

发布于 2021-01-21 17:55:10

我不认为你可以用你想做的方式来做这件事,但也许结合正常函数使用夹具参数就足够了,例如:

代码语言:javascript
复制
...
def foo(a: int, b: int) -> Foo:
    return Foo(a, b)

@pytest.fixture(params=[(3, 2)])
def parametrized_fixture1(request) -> Foo:
    yield foo(request.param[0], request.param[1])

@pytest.fixture(params=[(5, 6), (7, 8)])
def parametrized_fixture2(request) -> Foo:
    yield foo(request.param[0], request.param[1])

def test_with_second_fixture1(parametrized_fixture1: Foo):
    # one test with (3,2)
    pass

def test_with_second_fixture2(parametrized_fixture2: Foo):
    # two tests
    pass

当然,只有当您想要在多个测试中使用相同的参数时,这才有意义。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65819690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档