Skip to content

Fix assertions regarding magic methods function body that was not executed#14154

Merged
lisroach merged 1 commit into
python:masterfrom
tirkarthi:fix-testasync-cm-asserts
Sep 9, 2019
Merged

Fix assertions regarding magic methods function body that was not executed#14154
lisroach merged 1 commit into
python:masterfrom
tirkarthi:fix-testasync-cm-asserts

Conversation

@tirkarthi
Copy link
Copy Markdown
Member

The context manager object supplied to MagicMock has the magic methods __aenter__ and __aexit__ called with mock call registered but the actual function body was not executed. Since MagicMock is always True the attributes entered and exited returned a MagicMock that asserted to True in the test.

@tirkarthi
Copy link
Copy Markdown
Member Author

@lisroach @mariocj89 Would be helpful to have a review of this change . This is a test only change and doesn't require a NEWS entry or an issue. I have removed the assertions as per PR description and also removed the constructor since it's not needed.

@mariocj89
Copy link
Copy Markdown
Contributor

mariocj89 commented Jun 18, 2019

Sorry, I am not sure why the test is wrong, are you saying that the original code that is being passed to the mock is not being called? 😕

@tirkarthi
Copy link
Copy Markdown
Member Author

Sorry, I am not sure why the test is wrong, are you saying that the original code that is being passed to the mock is not being called?

Yes, when an object is presented as spec to MagicMock then the calls to magic methods are set to MagicProxy at

def _mock_set_magics(self):

So in these tests __enter__ and __exit__ implementations are not called and accessing entered and exited returns MagicMock which is always True. The tests are trying to assert that entered attribute is True in boolean context with assertTrue. If assertIs is used then the below error is displayed :

./python.exe -m unittest Lib.unittest.test.testmock.testasync.AsyncContextManagerTest.test_mock_supports_async_context_manager
F
======================================================================
FAIL: test_mock_supports_async_context_manager (Lib.unittest.test.testmock.testasync.AsyncContextManagerTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/test/testmock/testasync.py", line 396, in test_mock_supports_async_context_manager
    self.assertIs(mock_instance.entered, True)
AssertionError: <MagicMock name='mock.entered' id='4319777968'> is not True

----------------------------------------------------------------------
Ran 1 test in 0.064s

FAILED (failures=1)

Copy link
Copy Markdown
Contributor

@lisroach lisroach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, thanks for catching this!

@lisroach
Copy link
Copy Markdown
Contributor

Closed and reopened to try to re-trigger Azure

@ned-deily
Copy link
Copy Markdown
Member

@lisroach This PR seems to have stalled. Are you planning to merge it?

@lisroach
Copy link
Copy Markdown
Contributor

lisroach commented Aug 29, 2019 via email

@lisroach lisroach merged commit aa51508 into python:master Sep 9, 2019
@tirkarthi
Copy link
Copy Markdown
Member Author

Thanks Lisa :)

@bedevere-bot
Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Windows7 SP1 3.x has failed when building commit aa51508.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/all/#builders/40/builds/2996) and take a look at the build logs.
  4. Check if the failure is related to this commit (aa51508) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/all/#builders/40/builds/2996

Failed tests:

  • test_regrtest

Summary of the results of the build (if available):

== Tests result: FAILURE then SUCCESS ==

386 tests OK.

10 slowest tests:

  • test_multiprocessing_spawn: 3 min 2 sec
  • test_mmap: 2 min 42 sec
  • test_io: 2 min 23 sec
  • test_venv: 2 min 21 sec
  • test_tools: 2 min 21 sec
  • test_tokenize: 1 min 55 sec
  • test_concurrent_futures: 1 min 52 sec
  • test_lib2to3: 1 min 33 sec
  • test_largefile: 1 min 27 sec
  • test_socket: 1 min 2 sec

33 tests skipped:
test_curses test_dbm_gnu test_dbm_ndbm test_devpoll test_epoll
test_fcntl test_fork1 test_gdb test_grp test_ioctl test_kqueue
test_multiprocessing_fork test_multiprocessing_forkserver test_nis
test_openpty test_ossaudiodev test_pipes test_poll test_posix
test_pty test_pwd test_readline test_resource test_spwd
test_syslog test_threadsignals test_tix test_tk test_ttk_guionly
test_wait3 test_wait4 test_xxtestfuzz test_zipfile64

1 re-run test:
test_regrtest

Total duration: 25 min 20 sec

Click to see traceback logs
Traceback (most recent call last):
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 1013, in temp_dir
    yield path
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 1065, in temp_cwd
    yield cwd_dir
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\libregrtest\main.py", line 628, in main
    self._main(tests, kwargs)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\libregrtest\main.py", line 690, in _main
    sys.exit(0)
SystemExit: 0


Traceback (most recent call last):
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\__main__.py", line 2, in <module>
    main()
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\libregrtest\main.py", line 695, in main
    Regrtest().main(tests=tests, **kwargs)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\libregrtest\main.py", line 628, in main
    self._main(tests, kwargs)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 1065, in temp_cwd
    yield cwd_dir
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 1018, in temp_dir
    rmtree(path)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 452, in rmtree
    _rmtree(path)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 393, in _rmtree
    _waitfor(_rmtree_inner, path, waitall=True)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 341, in _waitfor
    func(pathname)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 390, in _rmtree_inner
    _force_run(fullname, os.rmdir, fullname)
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 336, in _force_run
    return func(*args)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\buildbot.python.org\\3.x.kloth-win64\\build\\build\\test_python_4376\\test_python_worker_5136'


Traceback (most recent call last):
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\support\__init__.py", line 330, in _force_run
    return func(*args)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\buildbot.python.org\\3.x.kloth-win64\\build\\build\\test_python_4376\\test_python_worker_5136'

@lisroach
Copy link
Copy Markdown
Contributor

lisroach commented Sep 9, 2019

Buildbot looks like a False positive.

lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants