This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make use of the "load_tests" protocol in test_importlib packages
Type: enhancement Stage: resolved
Components: Tests Versions: Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Make full use of test discovery in test subpackages
View: 22002
Assigned To: Nosy List: brett.cannon, eric.snow, r.david.murray, zach.ware
Priority: low Keywords:

Created on 2014-05-13 18:55 by eric.snow, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg218476 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2014-05-13 18:55
Right now to run importlib tests you can do either of the following:

./python -m tests test_importlib
./python -m tests.test_importlib

Both make use of the regrtest infrastructure.  For test submodules the commands are similar:

./python -m tests test_importlib.test_api
./python -m tests.test_importlib.test_api

You can also use unittest directly when testing specific test modules:

./python -m unittest tests.test_importlib.test_api
./python -m unittest tests.test_importlib.test_api.Source_ReloadTests.test_reload_location_changed

However, currently you cannot use unittest directly with a test package:

./python -m unittest tests.test_importlib
./python -m unittest tests.test_importlib.source

It would be nice to be able to do so, rather than switching back and forth between the unittest CLI and the regrtest CLI.

The change to do so is relatively straight-forward using the "load_tests" protocol*.  Just add the following to the __init__.py of the test packages:

 def load_tests(loader, tests, pattern):
     from test import TEST_HOME_DIR as topdir
     startdir = os.path.dirname(__name__)
     pkgtests = loader.discover(startdir, pattern or 'test*.py', topdir)
     tests.addTests(pkgtests)
     return tests

The boilerplate could even be moved to tests.support as a factory function:

 def make_load_tests(modfilename):
     from test import TEST_HOME_DIR as topdir
     startdir = os.path.dirname(modfilename)
     def load_tests(loader, tests, pattern):
         pkgtests = loader.discover(startdir, pattern or 'test*.py', topdir)
         tests.addTests(pkgtests)
         return tests
     return load_tests

In the test package __init__.py:

  load_tests = support.make_load_tests(__name__)

Then using unittest directly with an importlib test package will work as expected.  This is also something that could readily apply to any other test packages in the suite, which is why the factory function in test.support makes sense.

* https://docs.python.org/2/library/unittest.html#load-tests-protocol
msg219414 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-05-30 19:03
Personally I think the fact that this doesn't work by default is a bug in unittest.  See issue 15007.  Issue 16662 may also be related.
msg223751 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2014-07-23 17:26
This has been covered by #22002.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65699
2014-07-23 17:26:18zach.waresetstatus: open -> closed

superseder: Make full use of test discovery in test subpackages

nosy: + zach.ware
messages: + msg223751
resolution: duplicate
stage: needs patch -> resolved
2014-05-30 19:03:11r.david.murraysetnosy: + r.david.murray
messages: + msg219414
2014-05-13 18:55:58eric.snowcreate