Reputation: 61289
I'm looking for a way to make hypothesis do the following:
@given(device=st.test_every_value_of(["cpu", "gpu"])
def test_a_thing(self, device):
do_something_with_device(device)
But I haven't found a strategy like st.test_every_value_of
.
What's a good way of doing this?
Upvotes: 0
Views: 394
Reputation: 61289
It turns out there are two potential solutions.
The first is to use st.sampled_from
:
@given(device=st.sampled_from(["cpu", "gpu"]))
def test_a_thing(self, device):
do_something_with_device(device)
st.sampled_from
is not guaranteed to test all the values passed to it. However, Hypothesis, by default, generates 100 test cases from the specified strategies. If st.sampled_from
contains >100 items or if it's used in conjunction with other strategies then it may not test all values; however, for smaller tests it may work fine.
The better solution is probably to use pytest's parameterize:
import pytest
@pytest.mark.parameterize("device", ["cpu", "gpu"])
def test_a_thing(self, device):
do_something_with_device(device)
If this is used in conjunction with hypothesis then hypothesis will run 100 tests for each instance generated by parameterize.
Upvotes: 1