[issue21500] Make use of the "load_tests" protocol in test_importlib packages

Eric Snow report at bugs.python.org
Tue May 13 20:55:58 CEST 2014


New submission from Eric Snow:

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

----------
components: Tests
messages: 218476
nosy: brett.cannon, eric.snow
priority: low
severity: normal
stage: needs patch
status: open
title: Make use of the "load_tests" protocol in test_importlib packages
type: enhancement
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21500>
_______________________________________


More information about the Python-bugs-list mailing list