[Python] Hypothesis: test defensive programming

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I'm writing tests with Hypothesis to test functions with restricted range. Consider the following toy example:

    import math
    from hypothesis import assume, example, given

    def inc(value: float) -> float:
    assert not math.isnan(value)
    return value + 1

    @given(st.floats())
    def test_inc(value: float):
    assume(not math.isnan(value))
    if(value == math.inf or value == -math.inf):
    assert inc(value) == value
    else:
    assert inc(value) > value


    Here I'm testing the behaviour for finite and infinite inputs, but not NaN. I would like to test that out-of-range values, such as NaN, correctly throw an AssertionError.

    Is there a pattern built into Hypothesis where I can test this? Ideally I'd like an approach where I don't compromise the coverage of within-range values.

    Continue reading...

Compartilhe esta Página