Hypothesis

Hypothesis ist eine Bibliothek, mit der ihr Tests schreiben könnt, die aus einer Quelle von Beispielen :term:` parametrisiert <Parameter> werden. Anschließend werden einfache und verständliche Beispiele generiert, die dazu verwendet werden können, eure Tests fehlschlagen zu lassen und Fehler mit wenig Aufwand zu finden.

  1. Installiert Hypothesis:

    $ python -m pip install hypothesis
    
    C:> python -m pip install hypothesis
    

    Alternativ kann Hypothesis auch mit Erweiterungen installiert werden, z.B.:

    $ python -m pip install hypothesis[numpy,pandas]
    
    C:> python -m pip install hypothesis[numpy,pandas]
    
  2. Schreibt einen Test:

    1. Importe:

      1import pytest
      2from hypothesis import given
      3from hypothesis.strategies import floats, lists
      
    2. Testen:

      6@given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
      7def test_mean(ls):
      8    mean = sum(ls) / len(ls)
      9    assert min(ls) <= mean <= max(ls)
      
  3. Test durchführen:

    $ python -m pytest test_hypothesis.py
    ============================= test session starts ==============================
    platform darwin -- Python 3.13.0, pytest-8.3.3, pluggy-1.5.0
    rootdir: /Users/veit/cusy/trn/python-basics/docs/test
    plugins: hypothesis-6.114.1
    collected 1 item
    
    test_hypothesis.py F                                                     [100%]
    
    =================================== FAILURES ===================================
    __________________________________ test_mean ___________________________________
    
        @given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
    >   def test_mean(ls):
    
    test_hypothesis.py:6:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    
    ls = [9.9792015476736e+291, 1.7976931348623157e+308]
    
        @given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
        def test_mean(ls):
            mean = sum(ls) / len(ls)
    >       assert min(ls) <= mean <= max(ls)
    E       assert inf <= 1.7976931348623157e+308
    E        +  where 1.7976931348623157e+308 = max([9.9792015476736e+291, 1.7976931348623157e+308])
    
    test_hypothesis.py:8: AssertionError
    ---------------------------------- Hypothesis ----------------------------------
    Falsifying example: test_mean(
        ls=[9.9792015476736e+291, 1.7976931348623157e+308],
    )
    =========================== short test summary info ============================
    FAILED test_hypothesis.py::test_mean - assert inf <= 1.7976931348623157e+308
    ============================== 1 failed in 0.44s ===============================
    
    C:> python -m pytest test_hypothesis.py
    ============================= test session starts ==============================
    platform win32 -- Python 3.13.0, pytest-8.3.3, pluggy-1.5.0
    rootdir: C:\Users\veit\python-basics\docs\test
    plugins: plugins: hypothesis-6.114.1
    collected 1 item
    
    test_hypothesis.py F                                                     [100%]
    
    =================================== FAILURES ===================================
    __________________________________ test_mean ___________________________________
    
        @given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
    >   def test_mean(ls):
    
    test_hypothesis.py:6:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    
    ls = [9.9792015476736e+291, 1.7976931348623157e+308]
    
        @given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
        def test_mean(ls):
            mean = sum(ls) / len(ls)
    >       assert min(ls) <= mean <= max(ls)
    E       assert inf <= 1.7976931348623157e+308
    E        +  where 1.7976931348623157e+308 = max([9.9792015476736e+291, 1.7976931348623157e+308])
    
    test_hypothesis.py:8: AssertionError
    ---------------------------------- Hypothesis ----------------------------------
    Falsifying example: test_mean(
        ls=[9.9792015476736e+291, 1.7976931348623157e+308],
    )
    =========================== short test summary info ============================
    FAILED test_hypothesis.py::test_mean - assert inf <= 1.7976931348623157e+308
    ============================== 1 failed in 0.44s ===============================