使用 given with parametrize
Using given with parametrize
我想知道是否可以使用 given
参数来自 pytest 的 parametrize
函数。
示例:
import pytest
from hypothesis import given
from hypothesis import strategies as st
@st.composite
def my_strategy(draw, attribute):
# Body of my strategy
return # Something...
@pytest.mark.parametrize("attribute", [1, 2, 3])
@given(my_strategy(attribute))
def test_foo(strategy):
pass
在 @given(my_strategy(attribute))
上,我希望 attribute
将成为参数化的属性,并在每个 运行 中生成新的 my_strategy
以及想要的 attribute
我该怎么做?
我能想到的一种可能的解决方法是在测试中构建策略并使用 data
strategy 来绘制示例,例如
import pytest
from hypothesis import given
from hypothesis import strategies as st
@st.composite
def my_strategy(draw, attribute):
# Body of my strategy
return # Something...
@given(data=st.data())
@pytest.mark.parametrize("attribute", [1, 2, 3])
def test_foo(attribute, data):
strategy = my_strategy(attribute)
example = data.draw(strategy)
... # rest of the test
但我认为最好的方法是编写策略而不将其与 mark.parametrize
:
@given(st.sampled_from([1, 2, 3]).flatmap(my_strategy))
def test_foo(example):
... # rest of the test
我想知道是否可以使用 given
参数来自 pytest 的 parametrize
函数。
示例:
import pytest
from hypothesis import given
from hypothesis import strategies as st
@st.composite
def my_strategy(draw, attribute):
# Body of my strategy
return # Something...
@pytest.mark.parametrize("attribute", [1, 2, 3])
@given(my_strategy(attribute))
def test_foo(strategy):
pass
在 @given(my_strategy(attribute))
上,我希望 attribute
将成为参数化的属性,并在每个 运行 中生成新的 my_strategy
以及想要的 attribute
我该怎么做?
我能想到的一种可能的解决方法是在测试中构建策略并使用 data
strategy 来绘制示例,例如
import pytest
from hypothesis import given
from hypothesis import strategies as st
@st.composite
def my_strategy(draw, attribute):
# Body of my strategy
return # Something...
@given(data=st.data())
@pytest.mark.parametrize("attribute", [1, 2, 3])
def test_foo(attribute, data):
strategy = my_strategy(attribute)
example = data.draw(strategy)
... # rest of the test
但我认为最好的方法是编写策略而不将其与 mark.parametrize
:
@given(st.sampled_from([1, 2, 3]).flatmap(my_strategy))
def test_foo(example):
... # rest of the test