Test one specific example in Hypothesis

34 views Asked by At

Sometimes when I run pytest + hypothesis and one property-based test fails, I want to run that particular example with breakpoints etc.

@given(a=st.integers())
def test(a):
    assert a != 4

Currently, I take the test function definition, add that particular case as @pytest.mark.parametrize and comment out all the hypothesis parametrizations. It is a bit annoying, because the argument structure of parametrize is somewhat different from @example.

# @given(a=st.integers())
@pytest.mark.parametrize("a", 4)
def test(a):
    breakpoint()
    assert a != 4

I tried just letting it stand as @example and commenting out the @given, but hypothesis does not permit that.

# @given(a=st.integers())
@example(a=4)
def test(a):
    breakpoint()
    assert a != 4

What is the most convenient way to debug exactly one example from a property-based test using hypothesis and pytest?

1

There are 1 answers

0
Zac Hatfield-Dodds On

You'll want to use the phases setting, probably via a settings profile if you don't want to edit source code to add a decorator:

# conventionally in your conftest.py
settings.register_profile("explicit", phases=[Phase.explicit])

settings.load_profile("explicit")  # or `pytest --hypothesis-profile=explicit`

@settings(phases=[Phase.explicit])  # an expedient alternative to profiles
@given(a=st.integers())
@example(a=4)
def test(a):
    breakpoint()
    assert a != 4