Tests

doctest

In [3]:
def durchschnitt(values):
    """Berechnung des arithmetischen Mittels einer Liste von Zahlen
    
    >>> durchschnitt([20, 30, 70])
    40.0
    
    """
    return sum(values)/len(values)

import doctest
doctest.testmod(verbose=True)
Trying:
    durchschnitt([20, 30, 70])
Expecting:
    40.0
ok
1 items had no tests:
    __main__
1 items passed all tests:
   1 tests in __main__.durchschnitt
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
Out[3]:
TestResults(failed=0, attempted=1)
In [4]:
def durchschnitt(values):
    """Berechnung des arithmetischen Mittels einer Liste von Zahlen
    
    >>> durchschnitt([20, 30, 70])
    40.0
    
    >>> durchschnitt([2.6, 4.2, 6.8, 9.4])
    5.75
    
    """
    return sum(values)/len(values)

import doctest
doctest.testmod(verbose=True)
Trying:
    durchschnitt([20, 30, 70])
Expecting:
    40.0
ok
Trying:
    durchschnitt([2.6, 4.2, 6.8, 9.4])
Expecting:
    5.75
ok
1 items had no tests:
    __main__
1 items passed all tests:
   2 tests in __main__.durchschnitt
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
Out[4]:
TestResults(failed=0, attempted=2)
In [5]:
durchschnitt(2.6,4.2,6.8,9.4)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-838a0e52c5a5> in <module>()
----> 1 durchschnitt(2.6,4.2,6.8,9.4)

TypeError: durchschnitt() takes 1 positional argument but 4 were given
In [6]:
def durchschnitt(values):
    """Berechnung des arithmetischen Mittels einer Liste von Zahlen
    
    >>> durchschnitt([20, 30, 70])
    40.0
    
    >>> durchschnitt([2.6, 4.2, 6.8, 9.4])
    5.75
    
    >>> durchschnitt(2.6, 4.2, 6.8, 9.4)
    Traceback (most recent call last):
    ...
    TypeError: durchschnitt() takes 1 positional argument but 4 were given
    
    """
    return sum(values)/len(values)

import doctest
doctest.testmod(verbose=True)
Trying:
    durchschnitt([20, 30, 70])
Expecting:
    40.0
ok
Trying:
    durchschnitt([2.6, 4.2, 6.8, 9.4])
Expecting:
    5.75
ok
Trying:
    durchschnitt(2.6, 4.2, 6.8, 9.4)
Expecting:
    Traceback (most recent call last):
    ...
    TypeError: durchschnitt() takes 1 positional argument but 4 were given
ok
1 items had no tests:
    __main__
1 items passed all tests:
   3 tests in __main__.durchschnitt
3 tests in 2 items.
3 passed and 0 failed.
Test passed.
Out[6]:
TestResults(failed=0, attempted=3)