Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="doctest-options.html" /> | |
13 | <link rel="parent" href="doctest-how-it-works.html" /> | |
14 | <link rel="next" href="doctest-basic-api.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>5.2.3.6 Warnings</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="5.2.3.5 Option Flags and" | |
24 | href="doctest-options.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="5.2.3 How It Works" | |
27 | href="doctest-how-it-works.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="5.2.4 Basic API" | |
30 | href="doctest-basic-api.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="doctest-options.html">5.2.3.5 Option Flags and</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="doctest-how-it-works.html">5.2.3 How It Works</A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="doctest-basic-api.html">5.2.4 Basic API</A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H3><A NAME="SECTION007236000000000000000"></A><A NAME="doctest-warnings"></A> | |
55 | <BR> | |
56 | 5.2.3.6 Warnings | |
57 | </H3> | |
58 | ||
59 | <P> | |
60 | <tt class="module"><a href="module-doctest.html">doctest</a></tt> is serious about requiring exact matches in expected | |
61 | output. If even a single character doesn't match, the test fails. This | |
62 | will probably surprise you a few times, as you learn exactly what Python | |
63 | does and doesn't guarantee about output. For example, when printing a | |
64 | dict, Python doesn't guarantee that the key-value pairs will be printed | |
65 | in any particular order, so a test like | |
66 | ||
67 | <P> | |
68 | <div class="verbatim"><pre> | |
69 | >>> foo() | |
70 | {"Hermione": "hippogryph", "Harry": "broomstick"} | |
71 | </pre></div> | |
72 | ||
73 | <P> | |
74 | is vulnerable! One workaround is to do | |
75 | ||
76 | <P> | |
77 | <div class="verbatim"><pre> | |
78 | >>> foo() == {"Hermione": "hippogryph", "Harry": "broomstick"} | |
79 | True | |
80 | </pre></div> | |
81 | ||
82 | <P> | |
83 | instead. Another is to do | |
84 | ||
85 | <P> | |
86 | <div class="verbatim"><pre> | |
87 | >>> d = foo().items() | |
88 | >>> d.sort() | |
89 | >>> d | |
90 | [('Harry', 'broomstick'), ('Hermione', 'hippogryph')] | |
91 | </pre></div> | |
92 | ||
93 | <P> | |
94 | There are others, but you get the idea. | |
95 | ||
96 | <P> | |
97 | Another bad idea is to print things that embed an object address, like | |
98 | ||
99 | <P> | |
100 | <div class="verbatim"><pre> | |
101 | >>> id(1.0) # certain to fail some of the time | |
102 | 7948648 | |
103 | >>> class C: pass | |
104 | >>> C() # the default repr() for instances embeds an address | |
105 | <__main__.C instance at 0x00AC18F0> | |
106 | </pre></div> | |
107 | ||
108 | <P> | |
109 | The <tt class="constant">ELLIPSIS</tt> directive gives a nice approach for the last | |
110 | example: | |
111 | ||
112 | <P> | |
113 | <div class="verbatim"><pre> | |
114 | >>> C() #doctest: +ELLIPSIS | |
115 | <__main__.C instance at 0x...> | |
116 | </pre></div> | |
117 | ||
118 | <P> | |
119 | Floating-point numbers are also subject to small output variations across | |
120 | platforms, because Python defers to the platform C library for float | |
121 | formatting, and C libraries vary widely in quality here. | |
122 | ||
123 | <P> | |
124 | <div class="verbatim"><pre> | |
125 | >>> 1./7 # risky | |
126 | 0.14285714285714285 | |
127 | >>> print 1./7 # safer | |
128 | 0.142857142857 | |
129 | >>> print round(1./7, 6) # much safer | |
130 | 0.142857 | |
131 | </pre></div> | |
132 | ||
133 | <P> | |
134 | Numbers of the form <code>I/2.**J</code> are safe across all platforms, and I | |
135 | often contrive doctest examples to produce numbers of that form: | |
136 | ||
137 | <P> | |
138 | <div class="verbatim"><pre> | |
139 | >>> 3./4 # utterly safe | |
140 | 0.75 | |
141 | </pre></div> | |
142 | ||
143 | <P> | |
144 | Simple fractions are also easier for people to understand, and that makes | |
145 | for better documentation. | |
146 | ||
147 | <P> | |
148 | ||
149 | <DIV CLASS="navigation"> | |
150 | <div class='online-navigation'> | |
151 | <p></p><hr /> | |
152 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
153 | <tr> | |
154 | <td class='online-navigation'><a rel="prev" title="5.2.3.5 Option Flags and" | |
155 | href="doctest-options.html"><img src='../icons/previous.png' | |
156 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
157 | <td class='online-navigation'><a rel="parent" title="5.2.3 How It Works" | |
158 | href="doctest-how-it-works.html"><img src='../icons/up.png' | |
159 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
160 | <td class='online-navigation'><a rel="next" title="5.2.4 Basic API" | |
161 | href="doctest-basic-api.html"><img src='../icons/next.png' | |
162 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
163 | <td align="center" width="100%">Python Library Reference</td> | |
164 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
165 | href="contents.html"><img src='../icons/contents.png' | |
166 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
167 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
168 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
169 | <td class='online-navigation'><a rel="index" title="Index" | |
170 | href="genindex.html"><img src='../icons/index.png' | |
171 | border='0' height='32' alt='Index' width='32' /></A></td> | |
172 | </tr></table> | |
173 | <div class='online-navigation'> | |
174 | <b class="navlabel">Previous:</b> | |
175 | <a class="sectref" rel="prev" href="doctest-options.html">5.2.3.5 Option Flags and</A> | |
176 | <b class="navlabel">Up:</b> | |
177 | <a class="sectref" rel="parent" href="doctest-how-it-works.html">5.2.3 How It Works</A> | |
178 | <b class="navlabel">Next:</b> | |
179 | <a class="sectref" rel="next" href="doctest-basic-api.html">5.2.4 Basic API</A> | |
180 | </div> | |
181 | </div> | |
182 | <hr /> | |
183 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
184 | </DIV> | |
185 | <!--End of Navigation Panel--> | |
186 | <ADDRESS> | |
187 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
188 | </ADDRESS> | |
189 | </BODY> | |
190 | </HTML> |