We’ve got pretty complex builder and factory classes/methods to build test/domain data for our unit tests using Hypothesis. Now for our integration tests, we’d like to use the same functionality, the only difference is that they should run only once regardless of the result of the test. One solution is to call example()
on the composite strategy in the test (or test fixture) which works fine, but it prints a warning asking not to do it.
I also tried to force Hypothesis to run the test only once:
<code># This is for demo purposes, the actual factory class is much more complicated.
def foo(foo: int):
print("calling foo")
return foo
@given(st.builds(foo, st.integers()))
@settings(max_examples=1, phases=[Phase.generate])
def test_foo(x: int):
print("running test_foo")
assert False
</code>
<code># This is for demo purposes, the actual factory class is much more complicated.
def foo(foo: int):
print("calling foo")
return foo
@given(st.builds(foo, st.integers()))
@settings(max_examples=1, phases=[Phase.generate])
def test_foo(x: int):
print("running test_foo")
assert False
</code>
# This is for demo purposes, the actual factory class is much more complicated.
def foo(foo: int):
print("calling foo")
return foo
@given(st.builds(foo, st.integers()))
@settings(max_examples=1, phases=[Phase.generate])
def test_foo(x: int):
print("running test_foo")
assert False
But still runs it twice:
<code>------------------ Captured stdout call ------------------
calling foo
running test_foo
calling foo
running test_foo
</code>
<code>------------------ Captured stdout call ------------------
calling foo
running test_foo
calling foo
running test_foo
</code>
------------------ Captured stdout call ------------------
calling foo
running test_foo
calling foo
running test_foo
Now my question is:
- Is it possible to get an example from a strategy without actually using
@given
? - If not, how can tell Hypothesis to run the test only one time?
- Also why is it running the test twice anyway, where the max_example is one and there’s no shrinking?
Thanks.