Simple testing

https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement

INTRODUCTION

Simple tests can be placed in the same file as the code tested. Tests should be automated [print() is not enough].


# Content of the file fib1.py

def fibonacci(n):
    old, new = 0, 1
    for _ in range(n):
        old, new = new, old + new
    return old

if __name__ == "__main__":   # we can import without tests

    assert fibonacci(0) == 0
    assert fibonacci(1) == 1
    assert fibonacci(2) == 1
    assert fibonacci(10) == 55

    results = [(0, 0), (1, 1), (2, 1), (10, 55)]
    for a, b in results:
        assert fibonacci(a) == b

    print("tests passed")

# To execute tests:

$ python fib1.py
tests passed