[Python-checkins] (2.7): Backport from 3.x several improvements and fixes for unittest.rst. (original) (raw)

ezio.melotti python-checkins at python.org
Thu Mar 10 22:01:25 CET 2011


http://hg.python.org/cpython/rev/6e5b5d1b6714 changeset: 68355:6e5b5d1b6714 branch: 2.7 user: Ezio Melotti <ezio.melotti at gmail.com> date: Thu Mar 10 23:00:48 2011 +0200 summary: Backport from 3.x several improvements and fixes for unittest.rst.

files: Doc/library/unittest.rst

diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -8,9 +8,11 @@ .. sectionauthor:: Fred L. Drake, Jr. <fdrake at acm.org> .. sectionauthor:: Raymond Hettinger <python at rcn.com>

.. versionadded:: 2.1 +(If you are already familiar with the basic concepts of testing, you might want +to skip to :ref:the list of assert methods <assert-methods>.) + The Python unit testing framework, sometimes referred to as "PyUnit," is a Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in turn, a Java version of Kent's Smalltalk testing framework. Each is the de @@ -128,13 +130,13 @@ def test_choice(self): element = random.choice(self.seq) - self.assertTrue(element in self.seq) + self.assertIn(element, self.seq) def test_sample(self): with self.assertRaises(ValueError): random.sample(self.seq, 20) for element in random.sample(self.seq, 5): - self.assertTrue(element in self.seq) + self.assertIn(element, self.seq) if name == 'main': unittest.main() @@ -192,7 +194,7 @@ .. _unittest-command-line-interface: -Command-Line Interface +Command Line Interface

The unittest module can be used from the command line to run tests from @@ -482,8 +484,9 @@ WidgetTestCase.test_resize. :class:TestLoader uses the 'test' method name prefix to identify test methods automatically. -Note that the order in which the various test cases will be run is determined by -sorting the test function names with the built-in :func:cmp function. +Note that the order in which the various test cases will be run is +determined by sorting the test function names with respect to the +built-in ordering for strings. Often it is desirable to group suites of test cases together, so as to run tests for the whole system at once. This is easy, since :class:TestSuite instances @@ -674,7 +677,7 @@ Test cases ~~~~~~~~~~ -.. class:: TestCase([methodName]) +.. class:: TestCase(methodName='runTest') Instances of the :class:TestCase class represent the smallest testable units in the :mod:unittest universe. This class is intended to be used as a base @@ -755,10 +758,10 @@ .. versionadded:: 2.7 - .. method:: run([result]) + .. method:: run(result=None) Run the test, collecting the result into the test result object passed as - result. If result is omitted or :const:None, a temporary result + result. If result is omitted or None, a temporary result object is created (by calling the :meth:defaultTestResult method) and used. The result object is not returned to :meth:run's caller. @@ -780,151 +783,101 @@ by the test to be propagated to the caller, and can be used to support running tests under a debugger. - The test code can use any of the following methods to check for and report - failures.

@@ -932,99 +885,35 @@ .. method:: assertIn(first, second, msg=None) assertNotIn(first, second, msg=None) - Tests that first is or is not in second with an explanatory error - message as appropriate.

@@ -1033,8 +922,8 @@ To catch any of a group of exceptions, a tuple containing the exception classes may be passed as exception. - If callable is omitted or None, returns a context manager so that the - code under test can be written inline rather than as a function:: + If only the exception argument is given, returns a context manager so + that the code under test can be written inline rather than as a function:: with self.assertRaises(SomeException): do_something() @@ -1052,11 +941,9 @@ .. versionchanged:: 2.7 Added the ability to use :meth:assertRaises as a context manager. - .. deprecated:: 2.7 - :meth:failUnlessRaises; use :meth:assertRaises.

@@ -1074,68 +961,241 @@ .. versionadded:: 2.7 - .. method:: assertIsNone(expr[, msg])

@@ -1150,18 +1210,19 @@ .. attribute:: longMessage - If set to True then any explicit failure message you pass in to the - assert methods will be appended to the end of the normal failure message. - The normal messages contain useful information about the objects involved, - for example the message from assertEqual shows you the repr of the two - unequal objects. Setting this attribute to True allows you to have a - custom error message in addition to the normal one.

@@ -1210,30 +1271,14 @@ .. method:: shortDescription() - Returns a description of the test, or :const:None if no description + Returns a description of the test, or None if no description has been provided. The default implementation of this method returns the first line of the test method's docstring, if available, or :const:None. - .. method:: addTypeEqualityFunc(typeobj, function)

@@ -1263,7 +1308,7 @@ .. versionadded:: 2.7

-.. class:: FunctionTestCase(testFunc[, setUp[, tearDown[, description]]]) +.. class:: FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)

This class implements the portion of the :class:`TestCase` interface which
allows the test runner to drive the test, but does not provide the methods

@@ -1272,12 +1317,36 @@ :mod:unittest-based test framework.

+Deprecated aliases +################## + +For historical reasons, some of the :class:TestCase methods had one or more +aliases that are now deprecated. The following table lists the correct names +along with their deprecated aliases: +

-.. class:: TestSuite([tests]) +.. class:: TestSuite(tests=())

This class represents an aggregation of individual tests cases and test suites.
The class presents the interface needed by the test runner to allow it to be run

@@ -1391,7 +1460,7 @@ Support for load_tests added.

@@ -1416,7 +1485,7 @@ The method optionally resolves name relative to the given module.

@@ -1578,14 +1647,14 @@

.. method:: wasSuccessful()

@@ -1694,7 +1763,7 @@ instead of repeatedly creating new instances.

-.. class:: TextTestRunner([stream[, descriptions[, verbosity], [resultclass]]]) +.. class:: TextTestRunner(stream=sys.stderr, descriptions=True, verbosity=1)

A basic test runner implementation which prints results on standard error.  It
has a few configurable parameters, but is essentially very simple.  Graphical

-- Repository URL: http://hg.python.org/cpython



More information about the Python-checkins mailing list