blob: 54a24b1c98ed37eb692825b9551d47a2d2d0a60c [file] [log] [blame]
Brett Cannon066f3922003-05-07 22:02:17 +00001\section{\module{test} ---
2 Regression tests package for Python}
3
4\declaremodule{standard}{test}
Brett Cannon066f3922003-05-07 22:02:17 +00005\sectionauthor{Brett Cannon}{brett@python.org}
Fred Drake60e868a2003-09-06 17:51:16 +00006\modulesynopsis{Regression tests package containing the testing suite
7 for Python.}
Brett Cannon066f3922003-05-07 22:02:17 +00008
9
Fred Drake60e868a2003-09-06 17:51:16 +000010The \module{test} package contains all regression tests for Python as
11well as the modules \module{test.test_support} and
12\module{test.regrtest}. \module{test.test_support} is used to enhance
13your tests while \module{test.regrtest} drives the testing suite.
Brett Cannon066f3922003-05-07 22:02:17 +000014
15Each module in the \module{test} package whose name starts with
Fred Drake60e868a2003-09-06 17:51:16 +000016\samp{test_} is a testing suite for a specific module or feature.
17All new tests should be written using the \refmodule{unittest} module;
18using \refmodule{unittest} is not required but makes the tests more
19flexible and maintenance of the tests easier. Some older tests are
20written to use \refmodule{doctest} and a ``traditional'' testing
21style; these styles of tests will not be covered.
Brett Cannon066f3922003-05-07 22:02:17 +000022
23\begin{seealso}
24\seemodule{unittest}{Writing PyUnit regression tests.}
25\seemodule{doctest}{Tests embedded in documentation strings.}
26\end{seealso}
27
28
Fred Drake9f545c42003-05-09 19:10:12 +000029\subsection{Writing Unit Tests for the \module{test} package%
30 \label{writing-tests}}
Brett Cannon066f3922003-05-07 22:02:17 +000031
32It is preferred that tests for the \module{test} package use the
Fred Drake9f545c42003-05-09 19:10:12 +000033\refmodule{unittest} module and follow a few guidelines.
Brett Cannonc8aa8482004-12-06 06:08:59 +000034One is to name the test module by starting it with \samp{test_} and end it with
35the name of the module being tested.
36The test methods in the test module should start with \samp{test_} and end with
37a description of what the method is testing.
Brett Cannon066f3922003-05-07 22:02:17 +000038This is needed so that the methods are recognized by the test driver as
39test methods.
40Also, no documentation string for the method should be included.
41A comment (such as
Fred Drake60e868a2003-09-06 17:51:16 +000042\samp{\# Tests function returns only True or False}) should be used to provide
Brett Cannon066f3922003-05-07 22:02:17 +000043documentation for test methods.
44This is done because documentation strings get printed out if they exist and
45thus what test is being run is not stated.
46
47A basic boilerplate is often used:
48
49\begin{verbatim}
50import unittest
51from test import test_support
52
53class MyTestCase1(unittest.TestCase):
54
55 # Only use setUp() and tearDown() if necessary
56
57 def setUp(self):
58 ... code to execute in preparation for tests ...
59
60 def tearDown(self):
61 ... code to execute to clean up after tests ...
62
63 def test_feature_one(self):
64 # Test feature one.
65 ... testing code ...
66
67 def test_feature_two(self):
68 # Test feature two.
69 ... testing code ...
70
71 ... more test methods ...
72
73class MyTestCase2(unittest.TestCase):
74 ... same structure as MyTestCase1 ...
75
76... more test classes ...
77
78def test_main():
79 test_support.run_unittest(MyTestCase1,
80 MyTestCase2,
81 ... list other tests ...
82 )
83
84if __name__ == '__main__':
85 test_main()
86\end{verbatim}
87
Fred Drake60e868a2003-09-06 17:51:16 +000088This boilerplate code allows the testing suite to be run by
89\module{test.regrtest} as well as on its own as a script.
Brett Cannon066f3922003-05-07 22:02:17 +000090
91The goal for regression testing is to try to break code.
92This leads to a few guidelines to be followed:
93
94\begin{itemize}
95\item The testing suite should exercise all classes, functions, and
96 constants.
97 This includes not just the external API that is to be presented to the
98 outside world but also "private" code.
99\item Whitebox testing (examining the code being tested when the tests are
100 being written) is preferred.
101 Blackbox testing (testing only the published user interface) is not
102 complete enough to make sure all boundary and edge cases are tested.
103\item Make sure all possible values are tested including invalid ones.
104 This makes sure that not only all valid values are acceptable but also
105 that improper values are handled correctly.
106\item Exhaust as many code paths as possible.
107 Test where branching occurs and thus tailor input to make sure as many
108 different paths through the code are taken.
109\item Add an explicit test for any bugs discovered for the tested code.
110 This will make sure that the error does not crop up again if the code is
111 changed in the future.
112\item Make sure to clean up after your tests (such as close and remove all
113 temporary files).
Brett Cannonc8aa8482004-12-06 06:08:59 +0000114\item If a test is dependent on a specific condition of the operating system
115 then verify the condition already exists before attempting the test.
Brett Cannon066f3922003-05-07 22:02:17 +0000116\item Import as few modules as possible and do it as soon as possible.
117 This minimizes external dependencies of tests and also minimizes possible
118 anomalous behavior from side-effects of importing a module.
119\item Try to maximize code reuse.
Fred Drake60e868a2003-09-06 17:51:16 +0000120 On occasion, tests will vary by something as small as what type
121 of input is used.
Brett Cannon066f3922003-05-07 22:02:17 +0000122 Minimize code duplication by subclassing a basic test class with a class
123 that specifies the input:
124\begin{verbatim}
125class TestFuncAcceptsSequences(unittest.TestCase):
126
127 func = mySuperWhammyFunction
128
129 def test_func(self):
130 self.func(self.arg)
131
132class AcceptLists(TestFuncAcceptsSequences):
133 arg = [1,2,3]
134
135class AcceptStrings(TestFuncAcceptsSequences):
136 arg = 'abc'
137
138class AcceptTuples(TestFuncAcceptsSequences):
139 arg = (1,2,3)
140\end{verbatim}
141\end{itemize}
142
143\begin{seealso}
Fred Drake60e868a2003-09-06 17:51:16 +0000144\seetitle{Test Driven Development}
145 {A book by Kent Beck on writing tests before code.}
Brett Cannon066f3922003-05-07 22:02:17 +0000146\end{seealso}
147
148
Brett Cannond1de45f2004-03-18 07:37:15 +0000149\subsection{Running tests using \module{test.regrtest} \label{regrtest}}
Brett Cannon066f3922003-05-07 22:02:17 +0000150
Fred Drake60e868a2003-09-06 17:51:16 +0000151\module{test.regrtest} can be used as a script to drive Python's
152regression test suite.
Brett Cannon066f3922003-05-07 22:02:17 +0000153Running the script by itself automatically starts running all
154regression tests in the \module{test} package.
155It does this by finding all modules in the package whose name starts with
Fred Drake60e868a2003-09-06 17:51:16 +0000156\samp{test_}, importing them, and executing the function
157\function{test_main()} if present.
Brett Cannon066f3922003-05-07 22:02:17 +0000158The names of tests to execute may also be passed to the script.
Fred Drake60e868a2003-09-06 17:51:16 +0000159Specifying a single regression test (\program{python regrtest.py}
160\programopt{test_spam.py}) will minimize output and only print whether
161the test passed or failed and thus minimize output.
Brett Cannon066f3922003-05-07 22:02:17 +0000162
Fred Drake60e868a2003-09-06 17:51:16 +0000163Running \module{test.regrtest} directly allows what resources are
Brett Cannon066f3922003-05-07 22:02:17 +0000164available for tests to use to be set.
Fred Drake60e868a2003-09-06 17:51:16 +0000165You do this by using the \programopt{-u} command-line option.
166Run \program{python regrtest.py} \programopt{-uall} to turn on all
167resources; specifying \programopt{all} as an option for
168\programopt{-u} enables all possible resources.
Brett Cannon066f3922003-05-07 22:02:17 +0000169If all but one resource is desired (a more common case), a
170comma-separated list of resources that are not desired may be listed after
Fred Drake60e868a2003-09-06 17:51:16 +0000171\programopt{all}.
172The command \program{python regrtest.py}
173\programopt{-uall,-audio,-largefile} will run \module{test.regrtest}
174with all resources except the \programopt{audio} and
175\programopt{largefile} resources.
Brett Cannon066f3922003-05-07 22:02:17 +0000176For a list of all resources and more command-line options, run
Fred Drake60e868a2003-09-06 17:51:16 +0000177\program{python regrtest.py} \programopt{-h}.
Brett Cannon066f3922003-05-07 22:02:17 +0000178
179Some other ways to execute the regression tests depend on what platform the
180tests are being executed on.
Fred Drake60e868a2003-09-06 17:51:16 +0000181On \UNIX{}, you can run \program{make} \programopt{test} at the
182top-level directory where Python was built.
183On Windows, executing \program{rt.bat} from your \file{PCBuild}
184directory will run all regression tests.
185
186
187\section{\module{test.test_support} ---
188 Utility functions for tests}
189
190\declaremodule[test.testsupport]{standard}{test.test_support}
191\modulesynopsis{Support for Python regression tests.}
192
193The \module{test.test_support} module provides support for Python's
Brett Cannon066f3922003-05-07 22:02:17 +0000194regression tests.
195
Fred Drake60e868a2003-09-06 17:51:16 +0000196This module defines the following exceptions:
197
198\begin{excdesc}{TestFailed}
199Exception to be raised when a test fails.
200\end{excdesc}
201
202\begin{excdesc}{TestSkipped}
203Subclass of \exception{TestFailed}.
204Raised when a test is skipped.
205This occurs when a needed resource (such as a network connection) is not
206available at the time of testing.
207\end{excdesc}
208
209\begin{excdesc}{ResourceDenied}
210Subclass of \exception{TestSkipped}.
211Raised when a resource (such as a network connection) is not available.
212Raised by the \function{requires()} function.
213\end{excdesc}
214
215
216The \module{test.test_support} module defines the following constants:
217
218\begin{datadesc}{verbose}
219\constant{True} when verbose output is enabled.
220Should be checked when more detailed information is desired about a running
221test.
222\var{verbose} is set by \module{test.regrtest}.
223\end{datadesc}
224
225\begin{datadesc}{have_unicode}
226\constant{True} when Unicode support is available.
227\end{datadesc}
228
229\begin{datadesc}{is_jython}
230\constant{True} if the running interpreter is Jython.
231\end{datadesc}
232
233\begin{datadesc}{TESTFN}
234Set to the path that a temporary file may be created at.
235Any temporary that is created should be closed and unlinked (removed).
236\end{datadesc}
237
238
239The \module{test.test_support} module defines the following functions:
240
241\begin{funcdesc}{forget}{module_name}
242Removes the module named \var{module_name} from \code{sys.modules} and deletes
243any byte-compiled files of the module.
244\end{funcdesc}
245
246\begin{funcdesc}{is_resource_enabled}{resource}
247Returns \constant{True} if \var{resource} is enabled and available.
248The list of available resources is only set when \module{test.regrtest}
249is executing the tests.
250\end{funcdesc}
251
252\begin{funcdesc}{requires}{resource\optional{, msg}}
253Raises \exception{ResourceDenied} if \var{resource} is not available.
254\var{msg} is the argument to \exception{ResourceDenied} if it is raised.
255Always returns true if called by a function whose \code{__name__} is
256\code{'__main__'}.
257Used when tests are executed by \module{test.regrtest}.
258\end{funcdesc}
259
260\begin{funcdesc}{findfile}{filename}
261Return the path to the file named \var{filename}.
262If no match is found \var{filename} is returned.
263This does not equal a failure since it could be the path to the file.
264\end{funcdesc}
265
266\begin{funcdesc}{run_unittest}{*classes}
267Execute \class{unittest.TestCase} subclasses passed to the function.
268The function scans the classes for methods starting with the prefix
269\samp{test_} and executes the tests individually.
270This is the preferred way to execute tests.
271\end{funcdesc}
272
273\begin{funcdesc}{run_suite}{suite\optional{, testclass}}
274Execute the \class{unittest.TestSuite} instance \var{suite}.
275The optional argument \var{testclass} accepts one of the test classes in the
276suite so as to print out more detailed information on where the testing suite
277originated from.
278\end{funcdesc}