gh-80958: unittest: discovery support for namespace packages as start directory (#123820)

This commit is contained in:
Jacob Walls 2024-10-23 00:41:33 -04:00 committed by GitHub
parent 34653bba64
commit c75ff2ef8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 145 additions and 37 deletions

View file

@ -340,28 +340,21 @@ Test modules and packages can customize test loading and discovery by through
the `load_tests protocol`_.
.. versionchanged:: 3.4
Test discovery supports :term:`namespace packages <namespace package>`
for the start directory. Note that you need to specify the top level
directory too (e.g.
``python -m unittest discover -s root/namespace -t root``).
Test discovery supports :term:`namespace packages <namespace package>`.
.. versionchanged:: 3.11
:mod:`unittest` dropped the :term:`namespace packages <namespace package>`
support in Python 3.11. It has been broken since Python 3.7. Start directory and
subdirectories containing tests must be regular package that have
``__init__.py`` file.
Test discovery dropped the :term:`namespace packages <namespace package>`
support. It has been broken since Python 3.7.
Start directory and its subdirectories containing tests must be regular
package that have ``__init__.py`` file.
Directories containing start directory still can be a namespace package.
In this case, you need to specify start directory as dotted package name,
and target directory explicitly. For example::
If the start directory is the dotted name of the package, the ancestor packages
can be namespace packages.
# proj/ <-- current directory
# namespace/
# mypkg/
# __init__.py
# test_mypkg.py
python -m unittest discover -s namespace.mypkg -t .
.. versionchanged:: 3.14
Test discovery supports :term:`namespace package` as start directory again.
To avoid scanning directories unrelated to Python,
tests are not searched in subdirectories that do not contain ``__init__.py``.
.. _organizing-tests:
@ -1915,10 +1908,8 @@ Loading and running tests
Modules that raise :exc:`SkipTest` on import are recorded as skips,
not errors.
.. versionchanged:: 3.4
*start_dir* can be a :term:`namespace packages <namespace package>`.
.. versionchanged:: 3.4
Paths are sorted before being imported so that execution order is the
same even if the underlying file system's ordering is not dependent
on file name.
@ -1930,11 +1921,13 @@ Loading and running tests
.. versionchanged:: 3.11
*start_dir* can not be a :term:`namespace packages <namespace package>`.
It has been broken since Python 3.7 and Python 3.11 officially remove it.
It has been broken since Python 3.7, and Python 3.11 officially removes it.
.. versionchanged:: 3.13
*top_level_dir* is only stored for the duration of *discover* call.
.. versionchanged:: 3.14
*start_dir* can once again be a :term:`namespace package`.
The following attributes of a :class:`TestLoader` can be configured either by
subclassing or assignment on an instance:

View file

@ -421,6 +421,15 @@ unicodedata
* The Unicode database has been updated to Unicode 16.0.0.
unittest
--------
* unittest discovery supports :term:`namespace package` as start
directory again. It was removed in Python 3.11.
(Contributed by Jacob Walls in :gh:`80958`.)
.. Add improved modules above alphabetically, not here at the end.
Optimizations