Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does unittest.TestLoader.discover still rely on existence of __init__.py files? #73828

Closed
andreif mannequin opened this issue Feb 24, 2017 · 10 comments
Closed

Why does unittest.TestLoader.discover still rely on existence of __init__.py files? #73828

andreif mannequin opened this issue Feb 24, 2017 · 10 comments
Labels
3.7 (EOL) end of life tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@andreif
Copy link
Mannequin

andreif mannequin commented Feb 24, 2017

BPO 29642
Nosy @warsaw, @nedbat, @rbtcollins, @ezio-melotti, @voidspace, @methane, @andreif
PRs
  • bpo-29642: Load tests from implicit packages. #282
  • Superseder
  • bpo-23882: unittest discovery doesn't detect namespace packages when given no parameters
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-04-12.04:02:31.660>
    created_at = <Date 2017-02-24.16:48:45.814>
    labels = ['3.7', 'invalid', 'type-bug', 'tests']
    title = 'Why does unittest.TestLoader.discover still rely on existence of __init__.py files?'
    updated_at = <Date 2020-07-19.03:35:55.175>
    user = 'https://github.com/andreif'

    bugs.python.org fields:

    activity = <Date 2020-07-19.03:35:55.175>
    actor = 'methane'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-04-12.04:02:31.660>
    closer = 'berker.peksag'
    components = ['Tests']
    creation = <Date 2017-02-24.16:48:45.814>
    creator = 'Andrei Fokau'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29642
    keywords = []
    message_count = 10.0
    messages = ['288529', '288531', '288534', '288545', '288558', '288567', '288576', '288697', '288698', '290064']
    nosy_count = 8.0
    nosy_names = ['barry', 'nedbat', 'rbcollins', 'ezio.melotti', 'michael.foord', 'methane', 'Andrei Fokau', 'Andrei Fokau2']
    pr_nums = ['282']
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = '23882'
    type = 'behavior'
    url = 'https://bugs.python.org/issue29642'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']

    @andreif
    Copy link
    Mannequin Author

    andreif mannequin commented Feb 24, 2017

    Hi, As far as I see, unittest.TestLoader doesn't search in PEP-420 packages, i.e. packages without __init__.py files.

    Is there some motivation behind this, or the loader was just not yet adapted for Implicit Namespace Packages?

    @andreif andreif mannequin added 3.7 (EOL) end of life tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error labels Feb 24, 2017
    @andreif
    Copy link
    Mannequin Author

    andreif mannequin commented Feb 24, 2017

    Ok, it's actually not so hard to work around (for Python 3.6, at least):

    import os
    from unittest import TestLoader
    
    class CustomTestLoader(TestLoader):
        def _find_test_path(self, full_path, pattern, namespace=False):
            original_isfile = os.path.isfile
    
            def patched_isfile(path):
                return str(path).endswith('__init__.py') or original_isfile(path)
    
            os.path.isfile = patched_isfile
            result = super()._find_test_path(full_path=full_path, pattern=pattern,
                                             namespace=namespace)
            os.path.isfile = original_isfile
            return result

    I'll try to submit a pull request if it can be resolved properly.

    @andreif
    Copy link
    Mannequin Author

    andreif mannequin commented Feb 24, 2017

    Alright, I made an initial fix in #282. I believe that I still need
    to update the docs and run it with something big, e.g. Django.

    @andreif
    Copy link
    Mannequin Author

    andreif mannequin commented Feb 25, 2017

    Docs promise already support for namespace packages, so just a minor clarification was done.

    @andreif
    Copy link
    Mannequin Author

    andreif mannequin commented Feb 25, 2017

    Ok, testing with Django was a bad idea due to compatibility with 3.7.
    I could apply it to 3.6.x and test Django with it.

    Is there a better idea how to trial the test discovery?

    @andreif
    Copy link
    Mannequin Author

    andreif mannequin commented Feb 25, 2017

    Testing with Django seems indicated an issue. I did the following with 3.6 patch (cherry-pick to bea9d2f) on macOS with OpenSSL installed via Homebrew:

    $ cd /Users/andrei/Python/cpython/
    $ export CFLAGS="-I/usr/local/opt/openssl/include"
    $ export LDFLAGS="-L/usr/local/opt/openssl/lib"
    $ ./configure --with-pydebug --prefix=/Users/andrei/Python/installed/
    $ make -j
    $ make install

    Then in Django (master, b427f0d674):

    $ cd /Users/andrei/Python/django/
    $ ../../installed/bin/pip3.6 install -r ./requirements/py3.txt
    $ PYTHONPATH=.. DJANGO_SETTINGS_MODULE=test_sqlite ../../installed/bin/python3.6 ./runtests.py

    That produced one error:

    ======================================================================
    ERROR: auth_tests.test_hashers (unittest.loader._FailedTest)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "/Users/andrei/Python/installed/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
        yield
      File "/Users/andrei/Python/installed/lib/python3.6/unittest/case.py", line 601, in run
        testMethod()
      File "/Users/andrei/Python/installed/lib/python3.6/unittest/loader.py", line 34, in testFailure
        raise self._exception
    ImportError: Failed to import test module: auth_tests.test_hashers
    Traceback (most recent call last):
      File "/Users/andrei/Python/installed/lib/python3.6/unittest/loader.py", line 426, in _find_test_path
        module = self._get_module_from_name(name)
      File "/Users/andrei/Python/installed/lib/python3.6/unittest/loader.py", line 367, in _get_module_from_name
        __import__(name)
      File "/Users/andrei/Python/django/tests/auth_tests/test_hashers.py", line 20, in <module>
        if crypt.crypt('', '') is None:
      File "/Users/andrei/Python/installed/lib/python3.6/crypt.py", line 47, in crypt
        return _crypt.crypt(word, salt)
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfb in position 1: invalid start byte

    Ran 11695 tests in 259.390s
    FAILED (errors=1, skipped=1149, expected failures=4)

    Then I ran the same tests in 3.6.0 virtualenv installed via pyenv:

    $ pyenv virtualenv 3.6.0 djtest
    $ pyenv shell djtest
    $ pip install -r ./requirements/py3.txt
    $ PYTHONPATH=.. DJANGO_SETTINGS_MODULE=test_sqlite python ./runtests.py

    and they went fine:

    Ran 11723 tests in 87.369s
    OK (skipped=1149, expected failures=4)

    So the patch causes 1 error and misses 28 tests. I'll try to figure out the problem with failing test and what tests are missing.

    @andreif
    Copy link
    Mannequin Author

    andreif mannequin commented Feb 25, 2017

    Removing --with-pydebug parameter helped to avoid issue with _crypto extension. Testing Django with that build produced result identical to 3.6.0:

    Ran 11723 tests in 83.897s
    OK (skipped=1149, expected failures=4)

    The patch is ready for review.

    @methane
    Copy link
    Member

    methane commented Feb 28, 2017

    I'm afraid this change makes testloader searches unrelated directory contains massive files (like node_modules).

    I don't think loading all tests from whole namespace package is not usual use case.

    @methane
    Copy link
    Member

    methane commented Feb 28, 2017

    When using import, (namespace) package name is explicitly specified.
    Only specified name is searched.

    In test loader's case, there are no such limit.
    Loader may search millions of completely unrelated directories.
    It may include directories in NFS or samba over slow network.

    @AndreiFokau2
    Copy link
    Mannequin

    AndreiFokau2 mannequin commented Mar 23, 2017

    I was wrong. The ticket can be closed now.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants