summaryrefslogtreecommitdiffstats
path: root/tests/roots/test-ext-doctest/doctest.txt
blob: 04780cfbbca43c8e7fbf454225488698b76e0b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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::

     print(1+1)

  .. testoutput::

     2

  .. testcode::

     1/0

  .. testoutput::

     Traceback (most recent call last):
       ...
     ZeroDivisionError: integer division or modulo by zero

* testsetup

  .. testsetup:: *

     def squared(x):
     	return x * x

  .. doctest::

     >>> squared(2)
     4

  .. testcode::

     print(squared(2))

  .. testoutput::

     4

  >>> squared(2)
  4

* options for doctest/testcode/testoutput blocks

  .. testcode::
     :hide:

     print('Output         text.')

  .. testoutput::
     :hide:
     :options: +NORMALIZE_WHITESPACE

     Output text.

  .. doctest::
     :pyversion: >= 2.0

     >>> a = 3
     >>> a
     3

  .. doctest::
     :pyversion: < 2.0

     >>> a = 3
     >>> a
     4

* grouping

  .. testsetup:: group1

     def add(x, y):
         return x + y


  ``add`` is now known in "group1", but not in others.

  .. doctest:: group1

     >>> add(1, 1)
     2

  .. doctest:: group2

     >>> add(1, 1)
     Traceback (most recent call last):
       ...
     NameError: name 'add' is not defined

  Interleaving testcode/testoutput:

  .. testcode:: group1

     print(squared(3))

  .. testcode:: group2

     print(squared(4))

  .. testoutput:: group1

     9

  .. testoutput:: group2

     16


.. testcleanup:: *

   from tests import test_ext_doctest
   test_ext_doctest.cleanup_call()

non-ASCII result
----------------

>>> print('umlauts: äöü.')
umlauts: äöü.
>>> print('Japanese: 日本語')
Japanese: 日本語

keep control char in raw string
-------------------------------

.. doctest::

    >>> print('one\ntwo')
    one
    two
    >>> print(r'one\ntwo')
    one\ntwo