Solution: Pytest test math functions



examples/pytest/test_math.py
import math

def test_gcd():
    assert math.gcd(6, 9) == 3
    assert math.gcd(17, 9) == 1

def test_ceil():
    assert math.ceil(0) == 0
    assert math.ceil(0.1) == 1
    assert math.ceil(-0.1) == 0

def test_factorial():
    assert math.factorial(0) == 1
    assert math.factorial(1) == 1
    assert math.factorial(2) == 2
    assert math.factorial(3) == 6

examples/pytest/test_math_exceptions.py
import math
import pytest

def test_math():
    with pytest.raises(Exception) as exinfo:
        math.factorial(-1)
    assert exinfo.type == ValueError
    assert str(exinfo.value) == 'factorial() not defined for negative values'


    with pytest.raises(Exception) as exinfo:
        math.factorial(1.2)
    assert exinfo.type == ValueError
    assert str(exinfo.value) == 'factorial() only accepts integral values'