""" Exercise 3.7 from "A primer on... write a function that computes a sum, and a test function for verifying that the function works as intended. """ def sum_1k(M): s = 0 for k in range(1, M+1): s += 1 / k return s def test_sum_1k(): tol = 1e-10 expected = 11 / 6 computed = sum_1k(3) success = abs(expected - computed) < tol assert success """ Run with pytest: Terminal> pytest sum_func.py ============================= test session starts ============================== platform darwin -- Python 3.9.7, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 rootdir: /Users/sundnes/Desktop/IN1900_10_sept plugins: anyio-2.2.0, mock-3.10.0, cov-3.0.0 collected 1 item sum_func.py . [100%] ============================== 1 passed in 0.02s =============================== """