| 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="organizing-tests.html" /> |
| 13 | <link rel="prev" href="module-unittest.html" /> |
| 14 | <link rel="parent" href="module-unittest.html" /> |
| 15 | <link rel="next" href="organizing-tests.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.3.1 Basic example </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 unittest " |
| 25 | href="module-unittest.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.2 Organizing test code" |
| 31 | href="organizing-tests.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-unittest.html">5.3 unittest </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="organizing-tests.html">5.3.2 Organizing test code</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION007310000000000000000"></A><A NAME="minimal-example"></A> |
| 56 | <BR> |
| 57 | 5.3.1 Basic example |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | The <tt class="module">unittest</tt> module provides a rich set of tools for |
| 62 | constructing and running tests. This section demonstrates that a |
| 63 | small subset of the tools suffice to meet the needs of most users. |
| 64 | |
| 65 | <P> |
| 66 | Here is a short script to test three functions from the |
| 67 | <tt class="module"><a href="module-random.html">random</a></tt> module: |
| 68 | |
| 69 | <P> |
| 70 | <div class="verbatim"><pre> |
| 71 | import random |
| 72 | import unittest |
| 73 | |
| 74 | class TestSequenceFunctions(unittest.TestCase): |
| 75 | |
| 76 | def setUp(self): |
| 77 | self.seq = range(10) |
| 78 | |
| 79 | def testshuffle(self): |
| 80 | # make sure the shuffled sequence does not lose any elements |
| 81 | random.shuffle(self.seq) |
| 82 | self.seq.sort() |
| 83 | self.assertEqual(self.seq, range(10)) |
| 84 | |
| 85 | def testchoice(self): |
| 86 | element = random.choice(self.seq) |
| 87 | self.assert_(element in self.seq) |
| 88 | |
| 89 | def testsample(self): |
| 90 | self.assertRaises(ValueError, random.sample, self.seq, 20) |
| 91 | for element in random.sample(self.seq, 5): |
| 92 | self.assert_(element in self.seq) |
| 93 | |
| 94 | if __name__ == '__main__': |
| 95 | unittest.main() |
| 96 | </pre></div> |
| 97 | |
| 98 | <P> |
| 99 | A testcase is created by subclassing <tt class="class">unittest.TestCase</tt>. |
| 100 | The three individual tests are defined with methods whose names start with |
| 101 | the letters "<tt class="samp">test</tt>". This naming convention informs the test runner |
| 102 | about which methods represent tests. |
| 103 | |
| 104 | <P> |
| 105 | The crux of each test is a call to <tt class="method">assertEqual()</tt> to |
| 106 | check for an expected result; <tt class="method">assert_()</tt> to verify a condition; |
| 107 | or <tt class="method">assertRaises()</tt> to verify that an expected exception gets |
| 108 | raised. These methods are used instead of the <tt class="keyword">assert</tt> statement |
| 109 | so the test runner can accumulate all test results and produce a report. |
| 110 | |
| 111 | <P> |
| 112 | When a <tt class="method">setUp()</tt> method is defined, the test runner will run that |
| 113 | method prior to each test. Likewise, if a <tt class="method">tearDown()</tt> method is |
| 114 | defined, the test runner will invoke that method after each test. In the |
| 115 | example, <tt class="method">setUp()</tt> was used to create a fresh sequence for each test. |
| 116 | |
| 117 | <P> |
| 118 | The final block shows a simple way to run the tests. |
| 119 | <tt class="function">unittest.main()</tt> provides a command line interface to the |
| 120 | test script. When run from the command line, the above script |
| 121 | produces an output that looks like this: |
| 122 | |
| 123 | <P> |
| 124 | <div class="verbatim"><pre> |
| 125 | ... |
| 126 | ---------------------------------------------------------------------- |
| 127 | Ran 3 tests in 0.000s |
| 128 | |
| 129 | OK |
| 130 | </pre></div> |
| 131 | |
| 132 | <P> |
| 133 | Instead of <tt class="function">unittest.main()</tt>, there are other ways to run the tests |
| 134 | with a finer level of control, less terse output, and no requirement to be |
| 135 | run from the command line. For example, the last two lines may be replaced |
| 136 | with: |
| 137 | |
| 138 | <P> |
| 139 | <div class="verbatim"><pre> |
| 140 | suite = unittest.makeSuite(TestSequenceFunctions) |
| 141 | unittest.TextTestRunner(verbosity=2).run(suite) |
| 142 | </pre></div> |
| 143 | |
| 144 | <P> |
| 145 | Running the revised script from the interpreter or another script |
| 146 | produces the following output: |
| 147 | |
| 148 | <P> |
| 149 | <div class="verbatim"><pre> |
| 150 | testchoice (__main__.TestSequenceFunctions) ... ok |
| 151 | testsample (__main__.TestSequenceFunctions) ... ok |
| 152 | testshuffle (__main__.TestSequenceFunctions) ... ok |
| 153 | |
| 154 | ---------------------------------------------------------------------- |
| 155 | Ran 3 tests in 0.110s |
| 156 | |
| 157 | OK |
| 158 | </pre></div> |
| 159 | |
| 160 | <P> |
| 161 | The above examples show the most commonly used <tt class="module">unittest</tt> features |
| 162 | which are sufficient to meet many everyday testing needs. The remainder |
| 163 | of the documentation explores the full feature set from first principles. |
| 164 | |
| 165 | <P> |
| 166 | |
| 167 | <DIV CLASS="navigation"> |
| 168 | <div class='online-navigation'> |
| 169 | <p></p><hr /> |
| 170 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 171 | <tr> |
| 172 | <td class='online-navigation'><a rel="prev" title="5.3 unittest " |
| 173 | href="module-unittest.html"><img src='../icons/previous.png' |
| 174 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 175 | <td class='online-navigation'><a rel="parent" title="5.3 unittest " |
| 176 | href="module-unittest.html"><img src='../icons/up.png' |
| 177 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 178 | <td class='online-navigation'><a rel="next" title="5.3.2 Organizing test code" |
| 179 | href="organizing-tests.html"><img src='../icons/next.png' |
| 180 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 181 | <td align="center" width="100%">Python Library Reference</td> |
| 182 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 183 | href="contents.html"><img src='../icons/contents.png' |
| 184 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 185 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 186 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 187 | <td class='online-navigation'><a rel="index" title="Index" |
| 188 | href="genindex.html"><img src='../icons/index.png' |
| 189 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 190 | </tr></table> |
| 191 | <div class='online-navigation'> |
| 192 | <b class="navlabel">Previous:</b> |
| 193 | <a class="sectref" rel="prev" href="module-unittest.html">5.3 unittest </A> |
| 194 | <b class="navlabel">Up:</b> |
| 195 | <a class="sectref" rel="parent" href="module-unittest.html">5.3 unittest </A> |
| 196 | <b class="navlabel">Next:</b> |
| 197 | <a class="sectref" rel="next" href="organizing-tests.html">5.3.2 Organizing test code</A> |
| 198 | </div> |
| 199 | </div> |
| 200 | <hr /> |
| 201 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 202 | </DIV> |
| 203 | <!--End of Navigation Panel--> |
| 204 | <ADDRESS> |
| 205 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 206 | </ADDRESS> |
| 207 | </BODY> |
| 208 | </HTML> |