| 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="module-test.html" /> |
| 13 | <link rel="prev" href="module-doctest.html" /> |
| 14 | <link rel="parent" href="misc.html" /> |
| 15 | <link rel="next" href="node164.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.3 unittest -- Unit testing framework</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.2.8 Soapbox" |
| 25 | href="doctest-soapbox.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. Miscellaneous Services" |
| 28 | href="misc.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.3.1 Basic example" |
| 31 | href="node164.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="doctest-soapbox.html">5.2.8 Soapbox</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node164.html">5.3.1 Basic example</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION007300000000000000000"> |
| 56 | 5.3 <tt class="module">unittest</tt> -- |
| 57 | Unit testing framework</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-unittest"></A> |
| 62 | |
| 63 | <P> |
| 64 | |
| 65 | <span class="versionnote">New in version 2.1.</span> |
| 66 | |
| 67 | <P> |
| 68 | The Python unit testing framework, often referred to as ``PyUnit,'' is |
| 69 | a Python language version of JUnit, by Kent Beck and Erich Gamma. |
| 70 | JUnit is, in turn, a Java version of Kent's Smalltalk testing |
| 71 | framework. Each is the de facto standard unit testing framework for |
| 72 | its respective language. |
| 73 | |
| 74 | <P> |
| 75 | PyUnit supports test automation, sharing of setup and shutdown code |
| 76 | for tests, aggregation of tests into collections, and independence of |
| 77 | the tests from the reporting framework. The <tt class="module">unittest</tt> module |
| 78 | provides classes that make it easy to support these qualities for a |
| 79 | set of tests. |
| 80 | |
| 81 | <P> |
| 82 | To achieve this, PyUnit supports some important concepts: |
| 83 | |
| 84 | <P> |
| 85 | <dl class="definitions"> |
| 86 | <dt><b><a id='l2h-1124' xml:id='l2h-1124'>test fixture</a></b></dt> |
| 87 | <dd> |
| 88 | A <i class="dfn">test fixture</i> represents the preparation needed to perform one |
| 89 | or more tests, and any associate cleanup actions. This may involve, |
| 90 | for example, creating temporary or proxy databases, directories, or |
| 91 | starting a server process. |
| 92 | |
| 93 | <P> |
| 94 | <dt><b><a id='l2h-1125' xml:id='l2h-1125'>test case</a></b></dt> |
| 95 | <dd> |
| 96 | A <i class="dfn">test case</i> is the smallest unit of testing. It checks for a |
| 97 | specific response to a particular set of inputs. PyUnit provides a |
| 98 | base class, <tt class="class">TestCase</tt>, which may be used to create new test |
| 99 | cases. You may provide your own implementation that does not subclass |
| 100 | from <tt class="class">TestCase</tt>, of course. |
| 101 | |
| 102 | <P> |
| 103 | <dt><b><a id='l2h-1126' xml:id='l2h-1126'>test suite</a></b></dt> |
| 104 | <dd> |
| 105 | A <i class="dfn">test suite</i> is a collection of test cases, test suites, or |
| 106 | both. It is used to aggregate tests that should be executed |
| 107 | together. |
| 108 | |
| 109 | <P> |
| 110 | <dt><b><a id='l2h-1127' xml:id='l2h-1127'>test runner</a></b></dt> |
| 111 | <dd> |
| 112 | A <i class="dfn">test runner</i> is a component which orchestrates the execution of |
| 113 | tests and provides the outcome to the user. The runner may use a |
| 114 | graphical interface, a textual interface, or return a special value to |
| 115 | indicate the results of executing the tests. |
| 116 | </dl> |
| 117 | |
| 118 | <P> |
| 119 | The test case and test fixture concepts are supported through the |
| 120 | <tt class="class">TestCase</tt> and <tt class="class">FunctionTestCase</tt> classes; the former |
| 121 | should be used when creating new tests, and the latter can be used when |
| 122 | integrating existing test code with a PyUnit-driven framework. When |
| 123 | building test fixtures using <tt class="class">TestCase</tt>, the <tt class="method">setUp()</tt> |
| 124 | and <tt class="method">tearDown()</tt> methods can be overridden to provide |
| 125 | initialization and cleanup for the fixture. With |
| 126 | <tt class="class">FunctionTestCase</tt>, existing functions can be passed to the |
| 127 | constructor for these purposes. When the test is run, the |
| 128 | fixture initialization is run first; if it succeeds, the cleanup |
| 129 | method is run after the test has been executed, regardless of the |
| 130 | outcome of the test. Each instance of the <tt class="class">TestCase</tt> will only |
| 131 | be used to run a single test method, so a new fixture is created for |
| 132 | each test. |
| 133 | |
| 134 | <P> |
| 135 | Test suites are implemented by the <tt class="class">TestSuite</tt> class. This |
| 136 | class allows individual tests and test suites to be aggregated; when |
| 137 | the suite is executed, all tests added directly to the suite and in |
| 138 | ``child'' test suites are run. |
| 139 | |
| 140 | <P> |
| 141 | A test runner is an object that provides a single method, |
| 142 | <tt class="method">run()</tt>, which accepts a <tt class="class">TestCase</tt> or <tt class="class">TestSuite</tt> |
| 143 | object as a parameter, and returns a result object. The class |
| 144 | <tt class="class">TestResult</tt> is provided for use as the result object. PyUnit |
| 145 | provide the <tt class="class">TextTestRunner</tt> as an example test runner which |
| 146 | reports test results on the standard error stream by default. |
| 147 | Alternate runners can be implemented for other environments (such as |
| 148 | graphical environments) without any need to derive from a specific |
| 149 | class. |
| 150 | |
| 151 | <P> |
| 152 | <div class="seealso"> |
| 153 | <p class="heading">See Also:</p> |
| 154 | |
| 155 | <dl compact="compact" class="seemodule"> |
| 156 | <dt>Module <b><tt class="module"><a href="module-doctest.html">doctest</a></tt>:</b> |
| 157 | <dd>Another test-support module with a very |
| 158 | different flavor. |
| 159 | </dl> |
| 160 | <dl compact="compact" class="seetitle"> |
| 161 | <dt><em class="citetitle"><a href="http://pyunit.sourceforge.net/" |
| 162 | >PyUnit Web Site</a></em></dt> |
| 163 | <dd>The |
| 164 | source for further information on PyUnit.</dd> |
| 165 | </dl> |
| 166 | <dl compact="compact" class="seetitle"> |
| 167 | <dt><em class="citetitle"><a href="http://www.XProgramming.com/testfram.htm" |
| 168 | >Simple Smalltalk |
| 169 | Testing: With Patterns</a></em></dt> |
| 170 | <dd>Kent Beck's original paper on |
| 171 | testing frameworks using the pattern shared by |
| 172 | <tt class="module">unittest</tt>.</dd> |
| 173 | </dl> |
| 174 | </div> |
| 175 | |
| 176 | <P> |
| 177 | |
| 178 | <p><br /></p><hr class='online-navigation' /> |
| 179 | <div class='online-navigation'> |
| 180 | <!--Table of Child-Links--> |
| 181 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 182 | |
| 183 | <UL CLASS="ChildLinks"> |
| 184 | <LI><A href="node164.html">5.3.1 Basic example</a> |
| 185 | <LI><A href="organizing-tests.html">5.3.2 Organizing test code</a> |
| 186 | <LI><A href="legacy-unit-tests.html">5.3.3 Re-using old test code</a> |
| 187 | <LI><A href="unittest-contents.html">5.3.4 Classes and functions</a> |
| 188 | <LI><A href="testcase-objects.html">5.3.5 TestCase Objects</a> |
| 189 | <LI><A href="testsuite-objects.html">5.3.6 TestSuite Objects</a> |
| 190 | <LI><A href="testresult-objects.html">5.3.7 TestResult Objects</a> |
| 191 | <LI><A href="testloader-objects.html">5.3.8 TestLoader Objects</a> |
| 192 | </ul> |
| 193 | <!--End of Table of Child-Links--> |
| 194 | </div> |
| 195 | |
| 196 | <DIV CLASS="navigation"> |
| 197 | <div class='online-navigation'> |
| 198 | <p></p><hr /> |
| 199 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 200 | <tr> |
| 201 | <td class='online-navigation'><a rel="prev" title="5.2.8 Soapbox" |
| 202 | href="doctest-soapbox.html"><img src='../icons/previous.png' |
| 203 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 204 | <td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services" |
| 205 | href="misc.html"><img src='../icons/up.png' |
| 206 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 207 | <td class='online-navigation'><a rel="next" title="5.3.1 Basic example" |
| 208 | href="node164.html"><img src='../icons/next.png' |
| 209 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 210 | <td align="center" width="100%">Python Library Reference</td> |
| 211 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 212 | href="contents.html"><img src='../icons/contents.png' |
| 213 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 214 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 215 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 216 | <td class='online-navigation'><a rel="index" title="Index" |
| 217 | href="genindex.html"><img src='../icons/index.png' |
| 218 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 219 | </tr></table> |
| 220 | <div class='online-navigation'> |
| 221 | <b class="navlabel">Previous:</b> |
| 222 | <a class="sectref" rel="prev" href="doctest-soapbox.html">5.2.8 Soapbox</A> |
| 223 | <b class="navlabel">Up:</b> |
| 224 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> |
| 225 | <b class="navlabel">Next:</b> |
| 226 | <a class="sectref" rel="next" href="node164.html">5.3.1 Basic example</A> |
| 227 | </div> |
| 228 | </div> |
| 229 | <hr /> |
| 230 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 231 | </DIV> |
| 232 | <!--End of Navigation Panel--> |
| 233 | <ADDRESS> |
| 234 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 235 | </ADDRESS> |
| 236 | </BODY> |
| 237 | </HTML> |