Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / writing-tests.html
CommitLineData
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="next" href="regrtest.html" />
13<link rel="prev" href="module-test.html" />
14<link rel="parent" href="module-test.html" />
15<link rel="next" href="regrtest.html" />
16<meta name='aesop' content='information' />
17<title>5.4.1 Writing Unit Tests for the test package</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="5.4 test "
25 href="module-test.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="5.4 test "
28 href="module-test.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="5.4.2 Running tests using"
31 href="regrtest.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="module-test.html">5.4 test </A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="module-test.html">5.4 test </A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="regrtest.html">5.4.2 Running tests using</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION007410000000000000000"></A><A NAME="writing-tests"></A>
56<BR>
575.4.1 Writing Unit Tests for the <tt class="module">test</tt> package
58</H2>
59
60<P>
61It is preferred that tests for the <tt class="module">test</tt> package use the
62<tt class="module"><a href="module-unittest.html">unittest</a></tt> module and follow a few guidelines.
63One is to have the name of all the test methods start with "<tt class="samp">test_</tt>" as
64well as the module's name.
65This is needed so that the methods are recognized by the test driver as
66test methods.
67Also, no documentation string for the method should be included.
68A comment (such as
69"<tt class="samp"># Tests function returns only True or False</tt>") should be used to provide
70documentation for test methods.
71This is done because documentation strings get printed out if they exist and
72thus what test is being run is not stated.
73
74<P>
75A basic boilerplate is often used:
76
77<P>
78<div class="verbatim"><pre>
79import unittest
80from test import test_support
81
82class MyTestCase1(unittest.TestCase):
83
84 # Only use setUp() and tearDown() if necessary
85
86 def setUp(self):
87 ... code to execute in preparation for tests ...
88
89 def tearDown(self):
90 ... code to execute to clean up after tests ...
91
92 def test_feature_one(self):
93 # Test feature one.
94 ... testing code ...
95
96 def test_feature_two(self):
97 # Test feature two.
98 ... testing code ...
99
100 ... more test methods ...
101
102class MyTestCase2(unittest.TestCase):
103 ... same structure as MyTestCase1 ...
104
105... more test classes ...
106
107def test_main():
108 test_support.run_unittest(MyTestCase1,
109 MyTestCase2,
110 ... list other tests ...
111 )
112
113if __name__ == '__main__':
114 test_main()
115</pre></div>
116
117<P>
118This boilerplate code allows the testing suite to be run by
119<tt class="module">test.regrtest</tt> as well as on its own as a script.
120
121<P>
122The goal for regression testing is to try to break code.
123This leads to a few guidelines to be followed:
124
125<P>
126
127<UL>
128<LI>The testing suite should exercise all classes, functions, and
129 constants.
130 This includes not just the external API that is to be presented to the
131 outside world but also "private" code.
132</LI>
133<LI>Whitebox testing (examining the code being tested when the tests are
134 being written) is preferred.
135 Blackbox testing (testing only the published user interface) is not
136 complete enough to make sure all boundary and edge cases are tested.
137</LI>
138<LI>Make sure all possible values are tested including invalid ones.
139 This makes sure that not only all valid values are acceptable but also
140 that improper values are handled correctly.
141</LI>
142<LI>Exhaust as many code paths as possible.
143 Test where branching occurs and thus tailor input to make sure as many
144 different paths through the code are taken.
145</LI>
146<LI>Add an explicit test for any bugs discovered for the tested code.
147 This will make sure that the error does not crop up again if the code is
148 changed in the future.
149</LI>
150<LI>Make sure to clean up after your tests (such as close and remove all
151 temporary files).
152</LI>
153<LI>Import as few modules as possible and do it as soon as possible.
154 This minimizes external dependencies of tests and also minimizes possible
155 anomalous behavior from side-effects of importing a module.
156</LI>
157<LI>Try to maximize code reuse.
158 On occasion, tests will vary by something as small as what type
159 of input is used.
160 Minimize code duplication by subclassing a basic test class with a class
161 that specifies the input:
162<div class="verbatim"><pre>
163class TestFuncAcceptsSequences(unittest.TestCase):
164
165 func = mySuperWhammyFunction
166
167 def test_func(self):
168 self.func(self.arg)
169
170class AcceptLists(TestFuncAcceptsSequences):
171 arg = [1,2,3]
172
173class AcceptStrings(TestFuncAcceptsSequences):
174 arg = 'abc'
175
176class AcceptTuples(TestFuncAcceptsSequences):
177 arg = (1,2,3)
178</pre></div>
179</LI>
180</UL>
181
182<P>
183<div class="seealso">
184 <p class="heading">See Also:</p>
185
186<dl compact="compact" class="seetitle">
187 <dt><em class="citetitle"
188 >Test Driven Development</em></dt>
189 <dd>A book by Kent Beck on writing tests before code.</dd>
190 </dl>
191</div>
192
193<P>
194
195<DIV CLASS="navigation">
196<div class='online-navigation'>
197<p></p><hr />
198<table align="center" width="100%" cellpadding="0" cellspacing="2">
199<tr>
200<td class='online-navigation'><a rel="prev" title="5.4 test "
201 href="module-test.html"><img src='../icons/previous.png'
202 border='0' height='32' alt='Previous Page' width='32' /></A></td>
203<td class='online-navigation'><a rel="parent" title="5.4 test "
204 href="module-test.html"><img src='../icons/up.png'
205 border='0' height='32' alt='Up One Level' width='32' /></A></td>
206<td class='online-navigation'><a rel="next" title="5.4.2 Running tests using"
207 href="regrtest.html"><img src='../icons/next.png'
208 border='0' height='32' alt='Next Page' width='32' /></A></td>
209<td align="center" width="100%">Python Library Reference</td>
210<td class='online-navigation'><a rel="contents" title="Table of Contents"
211 href="contents.html"><img src='../icons/contents.png'
212 border='0' height='32' alt='Contents' width='32' /></A></td>
213<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
214 border='0' height='32' alt='Module Index' width='32' /></a></td>
215<td class='online-navigation'><a rel="index" title="Index"
216 href="genindex.html"><img src='../icons/index.png'
217 border='0' height='32' alt='Index' width='32' /></A></td>
218</tr></table>
219<div class='online-navigation'>
220<b class="navlabel">Previous:</b>
221<a class="sectref" rel="prev" href="module-test.html">5.4 test </A>
222<b class="navlabel">Up:</b>
223<a class="sectref" rel="parent" href="module-test.html">5.4 test </A>
224<b class="navlabel">Next:</b>
225<a class="sectref" rel="next" href="regrtest.html">5.4.2 Running tests using</A>
226</div>
227</div>
228<hr />
229<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
230</DIV>
231<!--End of Navigation Panel-->
232<ADDRESS>
233See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
234</ADDRESS>
235</BODY>
236</HTML>