python - pytest如何制作一个参数化另一个夹具的夹具
问题描述
我有很多通过pytest
.
有时,我希望使用夹具进行测试而不必担心应用参数。
是否可以制作一个参数化另一个夹具的夹具?
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
解决方案
我不认为你可以按照你想要的方式做到这一点,但也许将夹具参数与普通函数结合使用就足够了,例如:
...
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
当然,这仅在您想对多个测试使用相同的参数时才有意义。
推荐阅读
- sql - 如何在表类型为时间序列的表中显示多个列?
- functional-programming - 布尔值作为函数并构造 if-then-else
- go - 如何构建 Go 应用程序以生成多个二进制文件?
- mysql - epoch_time 的转换在 mysql 中给出 '1970-01-01'
- npm - 我无法更新我的 npm 版本
- java - java-如何生成一个6位随机十六进制值
- sed - 替换每行中出现的前 3 个字符
- r - 返回的行名满足 rle 函数的条件
- c# - 无法在 asp.net 中使用 7zip 归档和取消归档文件
- ios - Testflight 与 App Store 安装(不丢失数据)