PyTest Use Cases

Test if an expected error is raised

def take_int(n: int) -> None:
if not isinstance(n, int):
raise TypeError(f"You gave me something bad: {n}")
print("Good, int is given:)")
import pytest

def test_type_error():
with pytest.raises(TypeError):
def test_zero_division():
with pytest.raises(TypeError) as error_info:
assert "You gave me something bad" in str(excinfo.value)

Comparing floating-point values

0.1 + 0.2 == 0.3
from pytest import approxassert 0.1 + 0.2 == approx(0.3)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store