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

Multiple test modules fail to run if invoked directly #90583

Closed
sobolevn opened this issue Jan 18, 2022 · 19 comments
Closed

Multiple test modules fail to run if invoked directly #90583

sobolevn opened this issue Jan 18, 2022 · 19 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

BPO 46425
Nosy @terryjreedy, @jaraco, @asvetlov, @ethanfurman, @zware, @serhiy-storchaka, @corona10, @miss-islington, @sobolevn
PRs
  • bpo-46425: fix direct invokation of multiple test modules #30666
  • bpo-46425: fix direct invocation of test_sqlite3 #30676
  • bpo-46425: fix direct invocation of test_contextlib #30681
  • bpo-46425: fix direct invocation of test_importlib #30682
  • [3.10] bpo-46425: Fix direct invocation of multiple test modules (GH-30666) #30699
  • [3.9] bpo-46425: Fix direct invocation of multiple test modules (GH-30666) #30700
  • bpo-46425: fix direct invocation of asyncio tests #30725
  • bpo-46425: fix direct invocation of test_traceback #30746
  • [3.10] bpo-46425: fix direct invocation of asyncio tests (GH-30725) #30782
  • [3.9] bpo-46425: fix direct invocation of asyncio tests (GH-30725) #30783
  • bpo-46425: fix direct invocation of test_fileutils and test_zoneinfo #30792
  • bpo-46425: Partially revert GH-30682 #30799
  • bpo-46425: revert changes to importlib.metadata and importlib.resources tests #30800
  • bpo-46474: sync with importlib_metadata 4.10.0 #30802
  • bpo-46474: Avoid REDoS in EntryPoint.pattern (sync with importlib_metadata 4.10.1) #30803
  • 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 = None
    created_at = <Date 2022-01-18.18:41:39.303>
    labels = ['type-bug', 'tests', '3.9', '3.10', '3.11']
    title = 'Multiple test modules fail to run if invoked directly'
    updated_at = <Date 2022-01-23.02:38:46.011>
    user = '/~https://github.com/sobolevn'

    bugs.python.org fields:

    activity = <Date 2022-01-23.02:38:46.011>
    actor = 'jaraco'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Tests']
    creation = <Date 2022-01-18.18:41:39.303>
    creator = 'sobolevn'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46425
    keywords = ['patch']
    message_count = 18.0
    messages = ['410879', '410880', '410881', '410886', '410887', '410911', '410924', '410999', '411000', '411088', '411247', '411250', '411252', '411264', '411265', '411266', '411267', '411334']
    nosy_count = 9.0
    nosy_names = ['terry.reedy', 'jaraco', 'asvetlov', 'ethan.furman', 'zach.ware', 'serhiy.storchaka', 'corona10', 'miss-islington', 'sobolevn']
    pr_nums = ['30666', '30676', '30681', '30682', '30699', '30700', '30725', '30746', '30782', '30783', '30792', '30799', '30800', '30802', '30803']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46425'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @sobolevn
    Copy link
    Member Author

    While working on a similar issue for test_typing.py (https://bugs.python.org/issue46416) I wondered: how many other modules also have this problem?

    So, after several hours of local testing, I got several problems:

    Probably there are other problems, because I haven't checked:

    • Windows tests, because I don't have a Win machine
    • test_importlib, because it has a lot of failure (I don't know how to fix them yet)
    • Multiple other modules with slow tests
    • Modules with doctests which assert full __qualname__ with module names

    To keep PRs reviews sane, I will include changes for several modules and split this big task of checking all test modules into several consecutive and rather simple pull requests.

    The first one with the problems described above is on its way!

    @sobolevn sobolevn added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error labels Jan 18, 2022
    @ethanfurman
    Copy link
    Member

    I suggest your grouping of PRs be by error type -- so have batches of "relative import" fixes and batches of "TESTFN" fixes, etc.

    @serhiy-storchaka
    Copy link
    Member

    New changeset 1292aa6 by Nikita Sobolev in branch 'main':
    bpo-46425: Fix direct invocation of multiple test modules (GH-30666)
    1292aa6

    @sobolevn
    Copy link
    Member Author

    Thank you, Ethan. Good idea! Will do from the second PR.
    Thanks for merging, Serhiy.

    @terryjreedy
    Copy link
    Member

    Nikita, thanks for doing this.

    Ethan's suggestion has two advantages. 1. It would fix most backport issues. By containing a mix of changes, PR-30666 cannot be backported as is. 'unittest.main' should always backport unless there is a context conflict. But 3.9 does not have os.helper and TESTFN is still in test.support in 3.9 and that change cannot backport. (I did not check 3.10.)

    1. It restricts the knowledge needed to review (and be willing to merge). I could and would review and merge a patch with only unittest.main changes, but not, without checking the docs to learn about new changes, the mixture you posted.

    I suggest you write a script to directly run each test.test_xyz file via subprocess and report to stdout. The test for this issue is a clean report. If you do so, I will run it on Windows in a fresh build of each of 3.9, 3.10, and 3.11.

    Note that there are two possible commands: 'python -m test test_xyz' and 'python -m test.test_xyz'. They are subtly different. So I would add an option to do it each way.

    With current 3.11, test_importlib runs fine on my Windows, with 1436 tests.

    @sobolevn
    Copy link
    Member Author

    Terry, yes I am using this script to run tests locally:

    #!/usr/bin/env bash
    
    set -e
    
    while read line; do 
      ./python.exe "$line"
    done < files.txt

    And this one to populate files.txt:

    find ./Lib -iname 'test*.py' -o -iname '*tests.py' -exec echo {} >> files.txt \;

    ---

    There are several other problems that I have to fix manually. For example, module-level raise unittest.case.SkipTest():

    » ./python.exe Lib/test/test_winreg.py 
    Traceback (most recent call last):
      File "/Users/sobolev/Desktop/cpython/Lib/test/support/import_helper.py", line 77, in import_module
        return importlib.import_module(name)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/sobolev/Desktop/cpython/Lib/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
    ModuleNotFoundError: No module named 'winreg'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/sobolev/Desktop/cpython/Lib/test/test_winreg.py", line 11, in <module>
        import_helper.import_module('winreg', required_on=['win'])
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/sobolev/Desktop/cpython/Lib/contextlib.py", line 155, in __exit__
        self.gen.throw(typ, value, traceback)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/sobolev/Desktop/cpython/Lib/test/support/import_helper.py", line 26, in _ignore_deprecated_imports
        yield
        ^^^^^
      File "/Users/sobolev/Desktop/cpython/Lib/test/support/import_helper.py", line 81, in import_module
        raise unittest.SkipTest(str(msg))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    unittest.case.SkipTest: No module named 'winreg'
    

    I am not sure what is the proper solution to this one 🤔

    Probably, I will update my script for more advanced Python-based one and ignore this exception.

    In the end, I expect to run this script without any failures. This is going to be a great result!

    ---

    I will also check that all possible modes python -m test test_xyz / python -m test.test_xyz / python [Lib/test/test_xyz.py](/~https://github.com/python/cpython/blob/main/Lib/test/test_xyz.py) are supported.

    ---

    Also, thank you for listing concrete problems with my first submission. The second one is bound to a single problem and a single submodule.

    @sobolevn
    Copy link
    Member Author

    Important note, in practice there's also a difference in how one calls some test module:

    Example of a test failure because of that: #30681

    @corona10
    Copy link
    Member

    New changeset 8105dd2 by Nikita Sobolev in branch '3.9':
    [3.9] bpo-46425: Fix direct invocation of multiple test modules (GH-30666) (GH-30700)
    8105dd2

    @corona10
    Copy link
    Member

    New changeset a6a0885 by Nikita Sobolev in branch '3.10':
    [3.10] bpo-46425: Fix direct invocation of multiple test modules (GH-30666) (GH-30699)
    a6a0885

    @serhiy-storchaka
    Copy link
    Member

    New changeset 22f73bd by Nikita Sobolev in branch 'main':
    bpo-46425: Fix direct invocation of test_contextlib (GH-30681)
    22f73bd

    @asvetlov
    Copy link
    Contributor

    New changeset 5a53400 by Nikita Sobolev in branch 'main':
    bpo-46425: fix direct invocation of asyncio tests (bpo-30725)
    5a53400

    @miss-islington
    Copy link
    Contributor

    New changeset 3c4a374 by Miss Islington (bot) in branch '3.9':
    bpo-46425: fix direct invocation of asyncio tests (GH-30725)
    3c4a374

    @asvetlov
    Copy link
    Contributor

    New changeset 6111d5d by Miss Islington (bot) in branch '3.10':
    bpo-46425: fix direct invocation of asyncio tests (GH-30725) (bpo-30782)
    6111d5d

    @asvetlov
    Copy link
    Contributor

    New changeset 101a184 by Nikita Sobolev in branch 'main':
    bpo-46425: fix direct invocation of test_traceback (GH-30746)
    101a184

    @asvetlov
    Copy link
    Contributor

    New changeset 55f4ec4 by Nikita Sobolev in branch 'main':
    bpo-46425: use absolute imports in test_sqlite3 (GH-30676)
    55f4ec4

    @asvetlov
    Copy link
    Contributor

    New changeset 57316c5 by Nikita Sobolev in branch 'main':
    bpo-46425: fix direct invocation of test_importlib (GH-30682)
    57316c5

    @asvetlov
    Copy link
    Contributor

    New changeset 1f8014c by Nikita Sobolev in branch 'main':
    bpo-46425: fix direct invocation of test_fileutils and test_zoneinfo (GH-30792)
    1f8014c

    @jaraco
    Copy link
    Member

    jaraco commented Jan 23, 2022

    New changeset d888ff5 by Jason R. Coombs in branch 'main':
    bpo-46425: Partially revert "bpo-46425: fix direct invocation of test_importlib (GH-30682)" (GH-30799)
    d888ff5

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @kumaraditya303
    Copy link
    Contributor

    @sobolevn Is this fixed now or are there more modules left?

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    9 participants