blob: 2e3bb61c648d1d9c769cbbce20dc06ebf338c7de [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.
Brett Cannon27ef61c2007-03-25 01:32:36 +000017All new tests should be written using the \refmodule{unittest} or
18\refmodule{doctest} module. Some older tests are
19written using a ``traditional'' testing style that compares output
20printed to \code{sys.stdout}; this style of test is considered
21deprecated.
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
Brett Cannon27ef61c2007-03-25 01:32:36 +000032It is preferred that tests that use the \refmodule{unittest} module
33follow 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}
Collin Winterc2898c52007-04-25 17:29:52 +0000199Exception to be raised when a test fails. This is deprecated in favor
200of \module{unittest}-based tests and \class{unittest.TestCase}'s
201assertion methods.
Fred Drake60e868a2003-09-06 17:51:16 +0000202\end{excdesc}
203
204\begin{excdesc}{TestSkipped}
205Subclass of \exception{TestFailed}.
206Raised when a test is skipped.
207This occurs when a needed resource (such as a network connection) is not
208available at the time of testing.
209\end{excdesc}
210
211\begin{excdesc}{ResourceDenied}
212Subclass of \exception{TestSkipped}.
213Raised when a resource (such as a network connection) is not available.
214Raised by the \function{requires()} function.
215\end{excdesc}
216
217
218The \module{test.test_support} module defines the following constants:
219
220\begin{datadesc}{verbose}
221\constant{True} when verbose output is enabled.
222Should be checked when more detailed information is desired about a running
223test.
224\var{verbose} is set by \module{test.regrtest}.
225\end{datadesc}
226
227\begin{datadesc}{have_unicode}
228\constant{True} when Unicode support is available.
229\end{datadesc}
230
231\begin{datadesc}{is_jython}
232\constant{True} if the running interpreter is Jython.
233\end{datadesc}
234
235\begin{datadesc}{TESTFN}
236Set to the path that a temporary file may be created at.
237Any temporary that is created should be closed and unlinked (removed).
238\end{datadesc}
239
240
241The \module{test.test_support} module defines the following functions:
242
243\begin{funcdesc}{forget}{module_name}
244Removes the module named \var{module_name} from \code{sys.modules} and deletes
245any byte-compiled files of the module.
246\end{funcdesc}
247
248\begin{funcdesc}{is_resource_enabled}{resource}
249Returns \constant{True} if \var{resource} is enabled and available.
250The list of available resources is only set when \module{test.regrtest}
251is executing the tests.
252\end{funcdesc}
253
254\begin{funcdesc}{requires}{resource\optional{, msg}}
255Raises \exception{ResourceDenied} if \var{resource} is not available.
256\var{msg} is the argument to \exception{ResourceDenied} if it is raised.
257Always returns true if called by a function whose \code{__name__} is
258\code{'__main__'}.
259Used when tests are executed by \module{test.regrtest}.
260\end{funcdesc}
261
262\begin{funcdesc}{findfile}{filename}
263Return the path to the file named \var{filename}.
264If no match is found \var{filename} is returned.
265This does not equal a failure since it could be the path to the file.
266\end{funcdesc}
267
268\begin{funcdesc}{run_unittest}{*classes}
269Execute \class{unittest.TestCase} subclasses passed to the function.
270The function scans the classes for methods starting with the prefix
271\samp{test_} and executes the tests individually.
Fred Drake60e868a2003-09-06 17:51:16 +0000272
Collin Winterc2898c52007-04-25 17:29:52 +0000273It is also legal to pass strings as parameters; these should be keys in
274\code{sys.modules}. Each associated module will be scanned by
275\code{unittest.TestLoader.loadTestsFromModule()}. This is usually seen in
276the following \function{test_main()} function:
277
278\begin{verbatim}
279def test_main():
280 test_support.run_unittest(__name__)
281\end{verbatim}
282
283This will run all tests defined in the named module.
Brett Cannon78a132b2007-01-12 07:27:52 +0000284\end{funcdesc}
Brett Cannon92d54d52007-01-04 00:23:49 +0000285
286The \module{test.test_support} module defines the following classes:
287
Brett Cannona30fcb42007-03-08 23:58:11 +0000288\begin{classdesc}{TransientResource}{exc\optional{, **kwargs}}
Brett Cannon51532662007-08-14 03:42:13 +0000289Instances are a context manager that raises \class{ResourceDenied} if the
290specified exception type is raised. Any keyword arguments are treated as
291attribute/value pairs to be compared against any exception raised within the
292\code{with} statement. Only if all pairs match properly against attributes on
293the exception is \class{ResourceDenied} raised.
Brett Cannona30fcb42007-03-08 23:58:11 +0000294\versionadded{2.6}
295\end{classdesc}
296
Brett Cannon92d54d52007-01-04 00:23:49 +0000297\begin{classdesc}{EnvironmentVarGuard}{}
298Class used to temporarily set or unset environment variables. Instances can be
299used as a context manager.
300\versionadded{2.6}
301\end{classdesc}
302
303\begin{methoddesc}{set}{envvar, value}
304Temporarily set the environment variable \code{envvar} to the value of
305\code{value}.
306\end{methoddesc}
307
308\begin{methoddesc}{unset}{envvar}
309Temporarily unset the environment variable \code{envvar}.
310\end{methoddesc}
311