2010-01-02 07:59:27 -06:00
|
|
|
Testing the doctest extension
|
|
|
|
=============================
|
|
|
|
|
|
|
|
Simple doctest blocks
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
>>> 1+1
|
|
|
|
2
|
|
|
|
>>> 1/0
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ZeroDivisionError: integer division or modulo by zero
|
|
|
|
|
|
|
|
|
|
|
|
Special directives
|
|
|
|
------------------
|
|
|
|
|
|
|
|
* doctest
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> 1+1
|
|
|
|
2
|
|
|
|
>>> 1/0
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ZeroDivisionError: integer division or modulo by zero
|
|
|
|
|
|
|
|
* testcode/testoutput
|
|
|
|
|
|
|
|
.. testcode::
|
|
|
|
|
2010-06-19 14:50:00 -05:00
|
|
|
print(1+1)
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. testoutput::
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
.. testcode::
|
|
|
|
|
|
|
|
1/0
|
|
|
|
|
|
|
|
.. testoutput::
|
|
|
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ZeroDivisionError: integer division or modulo by zero
|
|
|
|
|
|
|
|
* testsetup
|
|
|
|
|
|
|
|
.. testsetup:: *
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
def squared(x):
|
|
|
|
return x * x
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
>>> squared(2)
|
|
|
|
4
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. testcode::
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
print(squared(2))
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. testoutput::
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
4
|
2010-01-02 07:59:27 -06:00
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
>>> squared(2)
|
|
|
|
4
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
* options for testcode/testoutput blocks
|
|
|
|
|
|
|
|
.. testcode::
|
|
|
|
:hide:
|
|
|
|
|
2010-06-19 14:50:00 -05:00
|
|
|
print('Output text.')
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. testoutput::
|
|
|
|
:hide:
|
|
|
|
:options: +NORMALIZE_WHITESPACE
|
|
|
|
|
|
|
|
Output text.
|
|
|
|
|
|
|
|
* grouping
|
|
|
|
|
|
|
|
.. testsetup:: group1
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
def add(x, y):
|
|
|
|
return x + y
|
2010-01-02 07:59:27 -06:00
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
|
|
|
|
``add`` is now known in "group1", but not in others.
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. doctest:: group1
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
>>> add(1, 1)
|
|
|
|
2
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. doctest:: group2
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
>>> add(1, 1)
|
2010-01-02 07:59:27 -06:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2010-08-14 12:52:04 -05:00
|
|
|
NameError: name 'add' is not defined
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
Interleaving testcode/testoutput:
|
|
|
|
|
|
|
|
.. testcode:: group1
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
print(squared(3))
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. testcode:: group2
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
print(squared(4))
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. testoutput:: group1
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
9
|
2010-01-02 07:59:27 -06:00
|
|
|
|
|
|
|
.. testoutput:: group2
|
|
|
|
|
2010-08-14 12:52:04 -05:00
|
|
|
16
|
2011-01-03 14:20:29 -06:00
|
|
|
|
|
|
|
|
|
|
|
.. testcleanup:: *
|
|
|
|
|
|
|
|
import test_doctest
|
|
|
|
test_doctest.cleanup_call()
|