Hypothesis: parametrize hypothesis test

Created on 3 Apr 2019  路  2Comments  路  Source: HypothesisWorks/hypothesis

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.

question

All 2 comments

There are two good ways to do this:

  1. Just use 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.
  2. Use the 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 :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

garry-jeromson picture garry-jeromson  路  3Comments

pckroon picture pckroon  路  4Comments

Zac-HD picture Zac-HD  路  7Comments

Zac-HD picture Zac-HD  路  4Comments

sobolevn picture sobolevn  路  8Comments