Skip to content

test_datetime does not respect custom setUpClass and tearDownClass #120242

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

Closed
sobolevn opened this issue Jun 7, 2024 · 0 comments
Closed

test_datetime does not respect custom setUpClass and tearDownClass #120242

sobolevn opened this issue Jun 7, 2024 · 0 comments
Assignees
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Jun 7, 2024

Bug report

Example test case that can be possibly added to test_datetime.py:

class TestExample(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.value_to_test = 1
        return super().setUpClass()

    def test_example_bug(self):
        self.assertEqual(self.value_to_test, 1)

./python.exe -m test test_datetime -v -m test_example_bug outputs:

test_example_bug (test.datetimetester.TestExample_Pure.test_example_bug) ... ERROR
test_example_bug (test.datetimetester.TestExample_Fast.test_example_bug) ... ERROR

======================================================================
ERROR: test_example_bug (test.datetimetester.TestExample_Pure.test_example_bug)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython2/Lib/test/datetimetester.py", line 77, in test_example_bug
    self.assertEqual(self.value_to_test, 1)
                     ^^^^^^^^^^^^^^^^^^
AttributeError: 'TestExample_Pure' object has no attribute 'value_to_test'

======================================================================
ERROR: test_example_bug (test.datetimetester.TestExample_Fast.test_example_bug)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython2/Lib/test/datetimetester.py", line 77, in test_example_bug
    self.assertEqual(self.value_to_test, 1)
                     ^^^^^^^^^^^^^^^^^^
AttributeError: 'TestExample_Fast' object has no attribute 'value_to_test'

----------------------------------------------------------------------
Ran 2 tests in 0.003s

FAILED (errors=2)
test test_datetime failed
test_datetime failed (2 errors)

== Tests result: FAILURE ==

1 test failed:
    test_datetime

Total duration: 208 ms
Total tests: run=2 (filtered)
Total test files: run=1/1 (filtered) failed=1
Result: FAILURE

This happens because of these lines:

for cls in test_classes:
cls.__name__ += suffix
cls.__qualname__ += suffix
@classmethod
def setUpClass(cls_, module=module):
cls_._save_sys_modules = sys.modules.copy()
sys.modules[TESTS] = module
sys.modules['datetime'] = module.datetime_module
if hasattr(module, '_pydatetime'):
sys.modules['_pydatetime'] = module._pydatetime
sys.modules['_strptime'] = module._strptime
@classmethod
def tearDownClass(cls_):
sys.modules.clear()
sys.modules.update(cls_._save_sys_modules)
cls.setUpClass = setUpClass
cls.tearDownClass = tearDownClass

I proposed this fix: https://github.com/python/cpython/pull/119675/files#diff-d4ea73ff6f3d1428ce4536793404e64ce7dbc1f6d0afe22f707ee0a24777afea but it was not merged.

Refs #119675
Refs #119659

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error tests Tests in the Lib/test dir labels Jun 7, 2024
@sobolevn sobolevn self-assigned this Jun 7, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Jun 7, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 8, 2024
…etime` (pythonGH-120243)

(cherry picked from commit 95f4db8)

Co-authored-by: Nikita Sobolev <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 8, 2024
…etime` (pythonGH-120243)

(cherry picked from commit 95f4db8)

Co-authored-by: Nikita Sobolev <[email protected]>
sobolevn added a commit that referenced this issue Jun 8, 2024
…tetime` (GH-120243) (#120260)

gh-120242: Fix handling of `[setUp,tearDown]Class` in `test_datetime` (GH-120243)
(cherry picked from commit 95f4db8)

Co-authored-by: Nikita Sobolev <[email protected]>
sobolevn added a commit that referenced this issue Jun 8, 2024
…tetime` (GH-120243) (#120259)

gh-120242: Fix handling of `[setUp,tearDown]Class` in `test_datetime` (GH-120243)
(cherry picked from commit 95f4db8)

Co-authored-by: Nikita Sobolev <[email protected]>
@sobolevn sobolevn closed this as completed Jun 8, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant