New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inject fixtures into given or composite? #1658
Comments
Uh.
No. The differing execution models and scoping rules make this impossible. What to do insteadUse a factory pattern! Just like fixtures can return factories, you can also define a strategy that generates a customised factory for each test case: @composite
def builder(draw, ...):
value = draw(...)
return functools.partial(build_using_fixture, non_fixture_arg=value)
@given(build=builder())
def test(some_fixture, build):
# Each test case will get a `build` function with different
# pre-filled arguments, which will be shrunk as usual.
x = build(some_fixture)
hypothesis.note(f'x = {repr(x)}') # extra reporting
assert something(x) Hope that helps - feel free to ask any follow-up questions. |
Opps on my usage of That's a good example thanks! Since the number of things I need to |
Passing Shrinking can be tricky, but the baseline advice is that Hypothesis is really, really good at shrinking. Three bits of advice:
TLDR: pay attention to composition, and otherwise ignore shrinking. |
I'm running various integration tests (that interact with a database), and so I need some global state (like an ID generator) to persist across tests. Normally I use fixtures for this in pytest. So for example:
With hypothesis, I've switched to using
@given
and specifying the things I normally built inside the test, but I'm not sure how to accomplish this in the same way with a fixture. I've read: https://hypothesis.works/articles/hypothesis-pytest-fixtures/, but it doesn't work if I would have needed the fixture to build the input data. So I was looking for something like (which doesn't work):If I used
@composite
, then I can get it done...but now I'm back to not being able to cleanly see what the data by inspecting only the decorator. Might something like this be possible?
The text was updated successfully, but these errors were encountered: