| 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="testcase-objects.html" /> |
| 13 | <link rel="prev" href="legacy-unit-tests.html" /> |
| 14 | <link rel="parent" href="module-unittest.html" /> |
| 15 | <link rel="next" href="testcase-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.3.4 Classes and functions </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.3.3 Re-using old test" |
| 25 | href="legacy-unit-tests.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.3 unittest " |
| 28 | href="module-unittest.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.5 TestCase Objects" |
| 31 | href="testcase-objects.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="legacy-unit-tests.html">5.3.3 Re-using old test</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-unittest.html">5.3 unittest </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="testcase-objects.html">5.3.5 TestCase Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION007340000000000000000"></A><A NAME="unittest-contents"></A> |
| 56 | <BR> |
| 57 | 5.3.4 Classes and functions |
| 58 | |
| 59 | </H2> |
| 60 | |
| 61 | <P> |
| 62 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 63 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1128' xml:id='l2h-1128' class="class">TestCase</tt></b>(</nobr></td> |
| 64 | <td><var></var>)</td></tr></table></dt> |
| 65 | <dd> |
| 66 | Instances of the <tt class="class">TestCase</tt> class represent the smallest |
| 67 | testable units in a set of tests. This class is intended to be used |
| 68 | as a base class, with specific tests being implemented by concrete |
| 69 | subclasses. This class implements the interface needed by the test |
| 70 | runner to allow it to drive the test, and methods that the test code |
| 71 | can use to check for and report various kinds of failures. |
| 72 | </dl> |
| 73 | |
| 74 | <P> |
| 75 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 76 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1129' xml:id='l2h-1129' class="class">FunctionTestCase</tt></b>(</nobr></td> |
| 77 | <td><var>testFunc</var><big>[</big><var>, |
| 78 | setUp</var><big>[</big><var>, tearDown</var><big>[</big><var>, description</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 79 | <dd> |
| 80 | This class implements the portion of the <tt class="class">TestCase</tt> interface |
| 81 | which allows the test runner to drive the test, but does not provide |
| 82 | the methods which test code can use to check and report errors. |
| 83 | This is used to create test cases using legacy test code, allowing |
| 84 | it to be integrated into a <tt class="module"><a href="module-unittest.html">unittest</a></tt>-based test |
| 85 | framework. |
| 86 | </dl> |
| 87 | |
| 88 | <P> |
| 89 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 90 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1130' xml:id='l2h-1130' class="class">TestSuite</tt></b>(</nobr></td> |
| 91 | <td><var></var><big>[</big><var>tests</var><big>]</big><var></var>)</td></tr></table></dt> |
| 92 | <dd> |
| 93 | This class represents an aggregation of individual tests cases and |
| 94 | test suites. The class presents the interface needed by the test |
| 95 | runner to allow it to be run as any other test case, but all the |
| 96 | contained tests and test suites are executed. Additional methods |
| 97 | are provided to add test cases and suites to the aggregation. If |
| 98 | <var>tests</var> is given, it must be a sequence of individual tests that |
| 99 | will be added to the suite. |
| 100 | </dl> |
| 101 | |
| 102 | <P> |
| 103 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 104 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1131' xml:id='l2h-1131' class="class">TestLoader</tt></b>(</nobr></td> |
| 105 | <td><var></var>)</td></tr></table></dt> |
| 106 | <dd> |
| 107 | This class is responsible for loading tests according to various |
| 108 | criteria and returning them wrapped in a <tt class="class">TestSuite</tt>. |
| 109 | It can load all tests within a given module or <tt class="class">TestCase</tt> |
| 110 | class. When loading from a module, it considers all |
| 111 | <tt class="class">TestCase</tt>-derived classes. For each such class, it creates |
| 112 | an instance for each method with a name beginning with the string |
| 113 | "<tt class="samp">test</tt>". |
| 114 | </dl> |
| 115 | |
| 116 | <P> |
| 117 | <dl><dt><b><tt id='l2h-1132' xml:id='l2h-1132'>defaultTestLoader</tt></b></dt> |
| 118 | <dd> |
| 119 | Instance of the <tt class="class">TestLoader</tt> class which can be shared. If no |
| 120 | customization of the <tt class="class">TestLoader</tt> is needed, this instance can |
| 121 | always be used instead of creating new instances. |
| 122 | </dd></dl> |
| 123 | |
| 124 | <P> |
| 125 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 126 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1133' xml:id='l2h-1133' class="class">TextTestRunner</tt></b>(</nobr></td> |
| 127 | <td><var></var><big>[</big><var>stream</var><big>[</big><var>, |
| 128 | descriptions</var><big>[</big><var>, verbosity</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 129 | <dd> |
| 130 | A basic test runner implementation which prints results on standard |
| 131 | output. It has a few configurable parameters, but is essentially |
| 132 | very simple. Graphical applications which run test suites should |
| 133 | provide alternate implementations. |
| 134 | </dl> |
| 135 | |
| 136 | <P> |
| 137 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 138 | <td><nobr><b><tt id='l2h-1134' xml:id='l2h-1134' class="function">main</tt></b>(</nobr></td> |
| 139 | <td><var></var><big>[</big><var>module</var><big>[</big><var>, |
| 140 | defaultTest</var><big>[</big><var>, argv</var><big>[</big><var>, |
| 141 | testRunner</var><big>[</big><var>, testRunner</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 142 | <dd> |
| 143 | A command-line program that runs a set of tests; this is primarily |
| 144 | for making test modules conveniently executable. The simplest use |
| 145 | for this function is: |
| 146 | |
| 147 | <P> |
| 148 | <div class="verbatim"><pre> |
| 149 | if __name__ == '__main__': |
| 150 | unittest.main() |
| 151 | </pre></div> |
| 152 | </dl> |
| 153 | |
| 154 | <P> |
| 155 | In some cases, the existing tests may have be written using the |
| 156 | <tt class="module"><a href="module-doctest.html">doctest</a></tt> module. If so, that module provides a |
| 157 | <tt class="class">DocTestSuite</tt> class that can automatically build |
| 158 | <tt class="class">unittest.TestSuite</tt> instances from the existing test code. |
| 159 | |
| 160 | <span class="versionnote">New in version 2.3.</span> |
| 161 | |
| 162 | <P> |
| 163 | |
| 164 | <DIV CLASS="navigation"> |
| 165 | <div class='online-navigation'> |
| 166 | <p></p><hr /> |
| 167 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 168 | <tr> |
| 169 | <td class='online-navigation'><a rel="prev" title="5.3.3 Re-using old test" |
| 170 | href="legacy-unit-tests.html"><img src='../icons/previous.png' |
| 171 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 172 | <td class='online-navigation'><a rel="parent" title="5.3 unittest " |
| 173 | href="module-unittest.html"><img src='../icons/up.png' |
| 174 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 175 | <td class='online-navigation'><a rel="next" title="5.3.5 TestCase Objects" |
| 176 | href="testcase-objects.html"><img src='../icons/next.png' |
| 177 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 178 | <td align="center" width="100%">Python Library Reference</td> |
| 179 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 180 | href="contents.html"><img src='../icons/contents.png' |
| 181 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 182 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 183 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 184 | <td class='online-navigation'><a rel="index" title="Index" |
| 185 | href="genindex.html"><img src='../icons/index.png' |
| 186 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 187 | </tr></table> |
| 188 | <div class='online-navigation'> |
| 189 | <b class="navlabel">Previous:</b> |
| 190 | <a class="sectref" rel="prev" href="legacy-unit-tests.html">5.3.3 Re-using old test</A> |
| 191 | <b class="navlabel">Up:</b> |
| 192 | <a class="sectref" rel="parent" href="module-unittest.html">5.3 unittest </A> |
| 193 | <b class="navlabel">Next:</b> |
| 194 | <a class="sectref" rel="next" href="testcase-objects.html">5.3.5 TestCase Objects</A> |
| 195 | </div> |
| 196 | </div> |
| 197 | <hr /> |
| 198 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 199 | </DIV> |
| 200 | <!--End of Navigation Panel--> |
| 201 | <ADDRESS> |
| 202 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 203 | </ADDRESS> |
| 204 | </BODY> |
| 205 | </HTML> |