Doctests

[1]:
import doctest


def add(a, b):
    """
    This is a test:
    >>> add(7,6)
    13
    """
    return a + b


doctest.testmod(verbose=True)
Trying:
    add(7,6)
Expecting:
    13
ok
1 items had no tests:
    __main__
1 items passed all tests:
   1 tests in __main__.add
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
[1]:
TestResults(failed=0, attempted=1)

Debugging

[2]:
import doctest


doctest.testmod()


def multiply(a, b):
    """
    This is a test:
    >>> multiply(2, 2)
    5
    """
    import pdb

    pdb.set_trace()
    return a * b
  1. import pdb importiert den Python Debugger

  2. pdb.set_trace() erstellt einen sog. Breakpoint, der den Python-Debugger startet.