Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Doctest for bad module

In this case we have a function that works for some input and returns incorrect results for other input.

It can still have documentation with working examples:

def fib(n):
    """
    Fibonacci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

    >>> fib(7)
    8

    >>> fib(10)
    34

    >>> fib(42)
    165580141
    """
    fibs = [0, 1]
    for _ in range(n-2):
        fibs.append(fibs[-1] + fibs[-2])
    return fibs[-1]

We can run the tests and they will pass.

python -m doctest src/examples/testing/bad/fibonacci.py

Exit code on Linux and macOS:

$ echo $?
0

Exit code on Windows:

> echo %ERRORLEVEL%
0

What if someone reports the bug? What shall we do?