mirror of
https://github.com/python/cpython.git
synced 2025-09-14 04:37:29 +00:00
Add a few labels and links to unittest doc
This commit is contained in:
parent
d8f6597318
commit
22170edaf5
1 changed files with 41 additions and 28 deletions
|
@ -8,6 +8,8 @@
|
||||||
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
|
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
|
||||||
.. sectionauthor:: Raymond Hettinger <python@rcn.com>
|
.. sectionauthor:: Raymond Hettinger <python@rcn.com>
|
||||||
|
|
||||||
|
(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
|
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
|
Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in
|
||||||
|
@ -776,7 +778,7 @@ Test cases
|
||||||
by the test to be propagated to the caller, and can be used to support
|
by the test to be propagated to the caller, and can be used to support
|
||||||
running tests under a debugger.
|
running tests under a debugger.
|
||||||
|
|
||||||
|
.. _assert-methods:
|
||||||
|
|
||||||
The :class:`TestCase` class provides a number of methods to check for and
|
The :class:`TestCase` class provides a number of methods to check for and
|
||||||
report failures, such as:
|
report failures, such as:
|
||||||
|
@ -821,6 +823,10 @@ Test cases
|
||||||
| <TestCase.assertNotIsInstance>` | | |
|
| <TestCase.assertNotIsInstance>` | | |
|
||||||
+-----------------------------------------+-----------------------------+---------------+
|
+-----------------------------------------+-----------------------------+---------------+
|
||||||
|
|
||||||
|
All the assert methods (except :meth:`assertRaises`,
|
||||||
|
:meth:`assertRaisesRegexp`, :meth:`assertWarns`, :meth:`assertWarnsRegexp`)
|
||||||
|
accept a *msg* argument that, if specified, is used as the error message on
|
||||||
|
failure (see also :data:`longMessage`).
|
||||||
|
|
||||||
.. method:: assertEqual(first, second, msg=None)
|
.. method:: assertEqual(first, second, msg=None)
|
||||||
|
|
||||||
|
@ -831,9 +837,8 @@ Test cases
|
||||||
list, tuple, dict, set, frozenset or str or any type that a subclass
|
list, tuple, dict, set, frozenset or str or any type that a subclass
|
||||||
registers with :meth:`addTypeEqualityFunc` the type specific equality
|
registers with :meth:`addTypeEqualityFunc` the type specific equality
|
||||||
function will be called in order to generate a more useful default
|
function will be called in order to generate a more useful default
|
||||||
error message.
|
error message (see also the :ref:`list of type-specific methods
|
||||||
|
<type-specific-methods>`).
|
||||||
If specified, *msg* will be used as the error message on failure.
|
|
||||||
|
|
||||||
.. versionchanged:: 3.1
|
.. versionchanged:: 3.1
|
||||||
Added the automatic calling of type specific equality function.
|
Added the automatic calling of type specific equality function.
|
||||||
|
@ -1145,9 +1150,29 @@ Test cases
|
||||||
.. deprecated:: 3.2
|
.. deprecated:: 3.2
|
||||||
|
|
||||||
|
|
||||||
|
.. _type-specific-methods:
|
||||||
|
|
||||||
The following methods are used automatically by :meth:`~TestCase.assertEqual`
|
The :meth:`assertEqual` method dispatches the equality check for objects of
|
||||||
and usually is not necessary to invoke them directly:
|
the same type to different type-specific methods. These methods are already
|
||||||
|
implemented for most of the built-in types, but it's also possible to
|
||||||
|
register new methods using :meth:`addTypeEqualityFunc`:
|
||||||
|
|
||||||
|
.. method:: addTypeEqualityFunc(typeobj, function)
|
||||||
|
|
||||||
|
Registers a type-specific method called by :meth:`assertEqual` to check
|
||||||
|
if two objects of exactly the same *typeobj* (not subclasses) compare
|
||||||
|
equal. *function* must take two positional arguments and a third msg=None
|
||||||
|
keyword argument just as :meth:`assertEqual` does. It must raise
|
||||||
|
:data:`self.failureException(msg) <failureException>` when inequality
|
||||||
|
between the first two parameters is detected -- possibly providing useful
|
||||||
|
information and explaining the inequalities in details in the error
|
||||||
|
message.
|
||||||
|
|
||||||
|
.. versionadded:: 3.1
|
||||||
|
|
||||||
|
The list of type-specific methods automatically used by
|
||||||
|
:meth:`~TestCase.assertEqual` are summarized in the following table. Note
|
||||||
|
that it's usually not necessary to invoke these methods directly.
|
||||||
|
|
||||||
+-----------------------------------------+-----------------------------+--------------+
|
+-----------------------------------------+-----------------------------+--------------+
|
||||||
| Method | Used to compare | New in |
|
| Method | Used to compare | New in |
|
||||||
|
@ -1190,7 +1215,8 @@ Test cases
|
||||||
be raised. If the sequences are different an error message is
|
be raised. If the sequences are different an error message is
|
||||||
constructed that shows the difference between the two.
|
constructed that shows the difference between the two.
|
||||||
|
|
||||||
This method is used to implement :meth:`assertListEqual` and
|
This method is not called directly by :meth:`assertEqual`, but
|
||||||
|
it's used to implement :meth:`assertListEqual` and
|
||||||
:meth:`assertTupleEqual`.
|
:meth:`assertTupleEqual`.
|
||||||
|
|
||||||
.. versionadded:: 3.1
|
.. versionadded:: 3.1
|
||||||
|
@ -1231,6 +1257,8 @@ Test cases
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
.. _other-methods-and-attrs:
|
||||||
|
|
||||||
Finally the :class:`TestCase` provides the following methods and attributes:
|
Finally the :class:`TestCase` provides the following methods and attributes:
|
||||||
|
|
||||||
|
|
||||||
|
@ -1252,11 +1280,12 @@ Test cases
|
||||||
.. attribute:: longMessage
|
.. attribute:: longMessage
|
||||||
|
|
||||||
If set to True then any explicit failure message you pass in to the
|
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.
|
:ref:`assert methods <assert-methods>` will be appended to the end of the
|
||||||
The normal messages contain useful information about the objects involved,
|
normal failure message. The normal messages contain useful information
|
||||||
for example the message from assertEqual shows you the repr of the two
|
about the objects involved, for example the message from assertEqual
|
||||||
unequal objects. Setting this attribute to True allows you to have a
|
shows you the repr of the two unequal objects. Setting this attribute
|
||||||
custom error message in addition to the normal one.
|
to True allows you to have a custom error message in addition to the
|
||||||
|
normal one.
|
||||||
|
|
||||||
This attribute defaults to False, meaning that a custom message passed
|
This attribute defaults to False, meaning that a custom message passed
|
||||||
to an assert method will silence the normal message.
|
to an assert method will silence the normal message.
|
||||||
|
@ -1322,22 +1351,6 @@ Test cases
|
||||||
with unittest extensions and adding the test name was moved to the
|
with unittest extensions and adding the test name was moved to the
|
||||||
:class:`TextTestResult`.
|
:class:`TextTestResult`.
|
||||||
|
|
||||||
.. method:: addTypeEqualityFunc(typeobj, function)
|
|
||||||
|
|
||||||
Registers a type specific :meth:`assertEqual` equality checking
|
|
||||||
function to be called by :meth:`assertEqual` when both objects it has
|
|
||||||
been asked to compare are exactly *typeobj* (not subclasses).
|
|
||||||
*function* must take two positional arguments and a third msg=None
|
|
||||||
keyword argument just as :meth:`assertEqual` does. It must raise
|
|
||||||
``self.failureException`` when inequality between the first two
|
|
||||||
parameters is detected.
|
|
||||||
|
|
||||||
One good use of custom equality checking functions for a type
|
|
||||||
is to raise ``self.failureException`` with an error message useful
|
|
||||||
for debugging the problem by explaining the inequalities in detail.
|
|
||||||
|
|
||||||
.. versionadded:: 3.1
|
|
||||||
|
|
||||||
|
|
||||||
.. method:: addCleanup(function, *args, **kwargs)
|
.. method:: addCleanup(function, *args, **kwargs)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue