Python 3.4 init.py (C:\Python34\Lib\unittest) code as below,
__all__ = ['TestResult', 'TestCase', 'TestSuite',
'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
'expectedFailure', 'TextTestResult', 'installHandler',
'registerResult', 'removeResult', 'removeHandler']
# Expose obsolete functions for backwards compatibility
__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
__unittest = True
from .result import TestResult
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
skipUnless, expectedFailure)
from .suite import BaseTestSuite, TestSuite
from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames,
findTestCases)
from .main import TestProgram, main
from .runner import TextTestRunner, TextTestResult
from .signals import installHandler, registerResult, removeResult, removeHandler
# deprecated
_TextTestResult = TextTestResult
Python 3.12 init.py (C:\Python312\Lib\unittest) code as below,
__all__ = ['TestResult', 'TestCase', 'IsolatedAsyncioTestCase', 'TestSuite',
'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
'expectedFailure', 'TextTestResult', 'installHandler',
'registerResult', 'removeResult', 'removeHandler',
'addModuleCleanup', 'doModuleCleanups', 'enterModuleContext']
# Expose obsolete functions for backwards compatibility
# bpo-5846: Deprecated in Python 3.11, scheduled for removal in Python 3.13.
__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
__unittest = True
from .result import TestResult
from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
skipIf, skipUnless, expectedFailure, doModuleCleanups,
enterModuleContext)
from .suite import BaseTestSuite, TestSuite
from .loader import TestLoader, defaultTestLoader
from .main import TestProgram, main
from .runner import TextTestRunner, TextTestResult
from .signals import installHandler, registerResult, removeResult, removeHandler
# IsolatedAsyncioTestCase will be imported lazily.
from .loader import makeSuite, getTestCaseNames, findTestCases
# Lazy import of IsolatedAsyncioTestCase from .async_case
# It imports asyncio, which is relatively heavy, but most tests
# do not need it.
def __dir__():
return globals().keys() | {'IsolatedAsyncioTestCase'}
def __getattr__(name):
if name == 'IsolatedAsyncioTestCase':
global IsolatedAsyncioTestCase
from .async_case import IsolatedAsyncioTestCase
return IsolatedAsyncioTestCase
raise AttributeError("module {__name__!r} has no attribute {name!r}")
While finding the difference between above version code,
Lazy Imports in Python 3.12 vs. Direct Imports in Python 3.4
In the unittest
module’s __init__.py
file, Python 3.4 directly imports makeSuite
, getTestCaseNames
, and findTestCases
from the loader
submodule. Python 3.12, however, implements lazy imports for these functions, meaning they’re only imported when explicitly requested.
Here’s a breakdown of the difference:
Python 3.4:
- When the
__init__.py
file is imported, all functions, including the ones from loader
, are loaded upfront .
- Even if these functions are not immediately used, they still occupy memory and contribute to loading time.
Python 3.12:
- Instead of eagerly importing everything,
__init__.py
only imports the necessary parts (TestResult
, TestCase
, etc.) at the beginning.
- If needed,
makeSuite
, getTestCaseNames
, and findTestCases
are imported on demand using the from .loader import
syntax within the relevant parts of the unittest
module.
- This avoids unnecessary imports for users who might not use these specific functions, leading to improved efficiency and reduced memory usage .
Is this causing issue?