summaryrefslogtreecommitdiff
path: root/Doc/library/unittest.rst
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2014-09-08 14:21:37 -0400
committerBarry Warsaw <barry@python.org>2014-09-08 14:21:37 -0400
commit3308a503414b99eaa4c512ee4c232776bee13352 (patch)
tree4f120a49c40d8575c6d7960190f6f04dc5b78b12 /Doc/library/unittest.rst
parente75be22ed5cb3f363d3554b1236841001ef0171b (diff)
downloadcpython-3308a503414b99eaa4c512ee4c232776bee13352.tar.gz
- Issue #16662: load_tests() is now unconditionally run when it is present in
a package's __init__.py. TestLoader.loadTestsFromModule() still accepts use_load_tests, but it is deprecated and ignored. A new keyword-only attribute `pattern` is added and documented. Patch given by Robert Collins, tweaked by Barry Warsaw.
Diffstat (limited to 'Doc/library/unittest.rst')
-rw-r--r--Doc/library/unittest.rst67
1 files changed, 39 insertions, 28 deletions
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
index 52f6e3f41e..1c2d8f6c4f 100644
--- a/Doc/library/unittest.rst
+++ b/Doc/library/unittest.rst
@@ -1561,7 +1561,7 @@ Loading and running tests
:class:`testCaseClass`.
- .. method:: loadTestsFromModule(module)
+ .. method:: loadTestsFromModule(module, pattern=None)
Return a suite of all tests cases contained in the given module. This
method searches *module* for classes derived from :class:`TestCase` and
@@ -1578,11 +1578,18 @@ Loading and running tests
If a module provides a ``load_tests`` function it will be called to
load the tests. This allows modules to customize test loading.
- This is the `load_tests protocol`_.
+ This is the `load_tests protocol`_. The *pattern* argument is passed as
+ the third argument to ``load_tests``.
.. versionchanged:: 3.2
Support for ``load_tests`` added.
+ .. versionchanged:: 3.5
+ The undocumented and unofficial *use_load_tests* default argument is
+ deprecated and ignored, although it is still accepted for backward
+ compatibility. The method also now accepts a keyword-only argument
+ *pattern* which is passed to ``load_tests`` as the third argument.
+
.. method:: loadTestsFromName(name, module=None)
@@ -1634,18 +1641,18 @@ Loading and running tests
the start directory is not the top level directory then the top level
directory must be specified separately.
- If importing a module fails, for example due to a syntax error, then this
- will be recorded as a single error and discovery will continue. If the
- import failure is due to :exc:`SkipTest` being raised, it will be recorded
- as a skip instead of an error.
+ If importing a module fails, for example due to a syntax error, then
+ this will be recorded as a single error and discovery will continue. If
+ the import failure is due to :exc:`SkipTest` being raised, it will be
+ recorded as a skip instead of an error.
- If a test package name (directory with :file:`__init__.py`) matches the
- pattern then the package will be checked for a ``load_tests``
- function. If this exists then it will be called with *loader*, *tests*,
- *pattern*.
+ If a package (a directory containing a file named :file:`__init__.py`) is
+ found, the package will be checked for a ``load_tests`` function. If this
+ exists then it will be called with *loader*, *tests*, *pattern*.
- If load_tests exists then discovery does *not* recurse into the package,
- ``load_tests`` is responsible for loading all tests in the package.
+ If ``load_tests`` exists then discovery does *not* recurse into the
+ package, ``load_tests`` is responsible for loading all tests in the
+ package.
The pattern is deliberately not stored as a loader attribute so that
packages can continue discovery themselves. *top_level_dir* is stored so
@@ -1664,6 +1671,11 @@ Loading and running tests
the same even if the underlying file system's ordering is not
dependent on file name.
+ .. versionchanged:: 3.5
+ Found packages are now checked for ``load_tests`` regardless of
+ whether their path matches *pattern*, because it is impossible for
+ a package name to match the default pattern.
+
The following attributes of a :class:`TestLoader` can be configured either by
subclassing or assignment on an instance:
@@ -2032,7 +2044,10 @@ test runs or test discovery by implementing a function called ``load_tests``.
If a test module defines ``load_tests`` it will be called by
:meth:`TestLoader.loadTestsFromModule` with the following arguments::
- load_tests(loader, standard_tests, None)
+ load_tests(loader, standard_tests, pattern)
+
+where *pattern* is passed straight through from ``loadTestsFromModule``. It
+defaults to ``None``.
It should return a :class:`TestSuite`.
@@ -2054,21 +2069,12 @@ A typical ``load_tests`` function that loads tests from a specific set of
suite.addTests(tests)
return suite
-If discovery is started, either from the command line or by calling
-:meth:`TestLoader.discover`, with a pattern that matches a package
-name then the package :file:`__init__.py` will be checked for ``load_tests``.
-
-.. note::
-
- The default pattern is ``'test*.py'``. This matches all Python files
- that start with ``'test'`` but *won't* match any test directories.
-
- A pattern like ``'test*'`` will match test packages as well as
- modules.
-
-If the package :file:`__init__.py` defines ``load_tests`` then it will be
-called and discovery not continued into the package. ``load_tests``
-is called with the following arguments::
+If discovery is started in a directory containing a package, either from the
+command line or by calling :meth:`TestLoader.discover`, then the package
+:file:`__init__.py` will be checked for ``load_tests``. If that function does
+not exist, discovery will recurse into the package as though it were just
+another directory. Otherwise, discovery of the package's tests will be left up
+to ``load_tests`` which is called with the following arguments::
load_tests(loader, standard_tests, pattern)
@@ -2087,6 +2093,11 @@ continue (and potentially modify) test discovery. A 'do nothing'
standard_tests.addTests(package_tests)
return standard_tests
+.. versionchanged:: 3.5
+ Discovery no longer checks package names for matching *pattern* due to the
+ impossibility of package names matching the default pattern.
+
+
Class and Module Fixtures
-------------------------