示例工程 29-testing-pytest

29-testing-pytest/06-hypothesis-property/test_hypothesis_property.py

python · 0.6 KB

"""test_hypothesis_property.py"""
 
from __future__ import annotations
 
import pytest
 
pytest.importorskip("hypothesis")
 
from hypothesis import given
from hypothesis import strategies as st
from hypothesis_property import decode, encode, main
 
 
@given(st.text())
def test_utf8_roundtrip(text: str) -> None:
    assert decode(encode(text)) == text
 
 
def test_invalid_utf8_raises() -> None:
    with pytest.raises(UnicodeDecodeError):
        decode(b"\xff")
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert "roundtrip: True" in out