use decorators to delay the application of given
#7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The idea is to use the less complicated form of metaprogramming in python (decorators) to delay the call to
hypothesis.given
. This is done by applying the function decoratordelayed_given
, which stores strategies and partial strategy functions passed to it in a new property on the test function.Then when a test class is created, we apply the class decorator
initialize_tests
that iterates over the test methods, accesses the property we just defined, evaluates any partial strategy functions with a fixed set of parameters, passes the result togiven
and finally replaces the test function with its decorated version.As an example, this would amount to something similar to:
Assuming this works properly (I'll still have to write tests), we can define test classes like this:
and they'd be used like this:
This is obviously a bit more complex than normal code, but the end result does not look too magical to me. What do you think?
cc @TomNicholas, @Zac-HD