I am looking to use hypothesis builds function to autogenerate fixtures for testing a serialization algorithm for dataclasses.
A simplified example of what I aim to do for one class is:
@given(builds(MyClass, **use_infer_for_attributes_with_default_values(MyClass)))
def test_serialization(instance):
assert instance == deserialize(serialize(instance))
but I'd like to be able to apply the same test to an arbitrary number of classes, in essence parametrizing the hypothesis test.
Ideally, after collecting all classes for which the serialisation test makes sense and I'd like to run it for each class sequentially.
I thought of building a strategy with one_of but I guess it does not scale? I have about 20-30 classes to test.
Do you have any idea of how I could achieve this? I am using pytest as test runner.
There are two good ways to do this:
one_of - it will scale up just fine, and in fact I've used it across several thousand inner strategies before! The caveat is that you may want to turn up the max_examples setting too, because you'd only have an average of four-ish tests per class otherwise.data() strategy to turn a class into a strategy (and then get an example) within your test function. You can then supply the classes with @pytest.mark.parametrize, which means you'll run the same number of tests for each - and get separate failing examples if multiple classes are broken. The downside is that the messages printed on failure aren't quite as nice.I've used both approaches in the past, and been happy with either. Hope that helps!
The second solution is basically what I was after. Thanks :)