#56 Tests: Don't mock twice
Closed 2 years ago by churchyard. Opened 2 years ago by churchyard.
churchyard/python-daemon nomock2  into  main

file modified
-6
@@ -391,12 +391,6 @@ 

          pidlockfile_scenarios = make_pidlockfile_scenarios()

          self.pidlockfile_scenario = pidlockfile_scenarios['simple']

  

-         for func_name in ['__init__', 'acquire']:

-             func_patcher = unittest.mock.patch.object(

-                     lockfile.pidlockfile.PIDLockFile, func_name)

The lockfile.pidlockfile.PIDLockFile class has these attributes (in the package lockfile == 0.12.2). Is that the case for you?

When using an instance of that class in these test cases, we want those attributes mocked for this collection of test cases. Are you saying these are already mocked somewhere else?

-             func_patcher.start()

-             self.addCleanup(func_patcher.stop)

- 

          self.scenario = {

                  'pidfile_path': self.pidlockfile_scenario['pidfile_path'],

                  'acquire_timeout': self.getUniqueInteger(),

With Python 3.10, we see the following errors:

======================================================================
ERROR: test_acquire_uses_specified_timeout (test.test_pidfile.TimeoutPIDLockFile_TestCase)
test.test_pidfile.TimeoutPIDLockFile_TestCase.test_acquire_uses_specified_timeout
----------------------------------------------------------------------
testtools.testresult.real._StringException: Traceback (most recent call last):
  File "/usr/lib64/python3.10/unittest/mock.py", line 1367, in patched
    with self.decoration_helper(patched,
  File "/usr/lib64/python3.10/contextlib.py", line 133, in __enter__
    return next(self.gen)
  File "/usr/lib64/python3.10/unittest/mock.py", line 1349, in decoration_helper
    arg = exit_stack.enter_context(patching)
  File "/usr/lib64/python3.10/contextlib.py", line 478, in enter_context
    result = _cm_type.__enter__(cm)
  File "/usr/lib64/python3.10/unittest/mock.py", line 1529, in __enter__
    raise InvalidSpecError(
unittest.mock.InvalidSpecError: Cannot autospec attr 'acquire' from target 'PIDLockFile' as it has already been mocked out. [target=<class 'lockfile.pidlockfile.PIDLockFile'>, attr=<MagicMock name='acquire' id='139634358950000'>]

======================================================================
ERROR: test_acquire_uses_stored_timeout_by_default (test.test_pidfile.TimeoutPIDLockFile_TestCase)
test.test_pidfile.TimeoutPIDLockFile_TestCase.test_acquire_uses_stored_timeout_by_default
----------------------------------------------------------------------
testtools.testresult.real._StringException: Traceback (most recent call last):
  File "/usr/lib64/python3.10/unittest/mock.py", line 1367, in patched
    with self.decoration_helper(patched,
  File "/usr/lib64/python3.10/contextlib.py", line 133, in __enter__
    return next(self.gen)
  File "/usr/lib64/python3.10/unittest/mock.py", line 1349, in decoration_helper
    arg = exit_stack.enter_context(patching)
  File "/usr/lib64/python3.10/contextlib.py", line 478, in enter_context
    result = _cm_type.__enter__(cm)
  File "/usr/lib64/python3.10/unittest/mock.py", line 1529, in __enter__
    raise InvalidSpecError(
unittest.mock.InvalidSpecError: Cannot autospec attr 'acquire' from target 'PIDLockFile' as it has already been mocked out. [target=<class 'lockfile.pidlockfile.PIDLockFile'>, attr=<MagicMock name='acquire' id='139634358932176'>]

======================================================================
ERROR: test_calls_superclass_init (test.test_pidfile.TimeoutPIDLockFile_TestCase)
test.test_pidfile.TimeoutPIDLockFile_TestCase.test_calls_superclass_init
----------------------------------------------------------------------
testtools.testresult.real._StringException: Traceback (most recent call last):
  File "/usr/lib64/python3.10/unittest/mock.py", line 1367, in patched
    with self.decoration_helper(patched,
  File "/usr/lib64/python3.10/contextlib.py", line 133, in __enter__
    return next(self.gen)
  File "/usr/lib64/python3.10/unittest/mock.py", line 1349, in decoration_helper
    arg = exit_stack.enter_context(patching)
  File "/usr/lib64/python3.10/contextlib.py", line 478, in enter_context
    result = _cm_type.__enter__(cm)
  File "/usr/lib64/python3.10/unittest/mock.py", line 1529, in __enter__
    raise InvalidSpecError(
unittest.mock.InvalidSpecError: Cannot autospec attr '__init__' from target 'PIDLockFile' as it has already been mocked out. [target=<class 'lockfile.pidlockfile.PIDLockFile'>, attr=<MagicMock name='__init__' id='139634359084912'>]

This avoids them.

I am still about to test this in Fedora first.

I am still about to test this in Fedora first.

Works with Python 3.10 and 3.9 as well.

I'm not seeing this behaviour. Perhaps we have different lockfile library versions?

DEBUG util.py:446:  Installing:
DEBUG util.py:446:   python3-devel             ppc64le   3.10.0~rc1-1.fc35            build   206 k
DEBUG util.py:446:   python3-docutils          noarch    0.16-7.fc35                  build   811 k
DEBUG util.py:446:   python3-extras            noarch    1.0.0-18.fc35                build    19 k
DEBUG util.py:446:   python3-lockfile          noarch    1:0.12.2-4.fc35              build    30 k
DEBUG util.py:446:   python3-mock              noarch    3.0.5-17.fc35                build    52 k
DEBUG util.py:446:   python3-setuptools        noarch    57.4.0-1.fc35                build   928 k
DEBUG util.py:446:   python3-testscenarios     noarch    0.5.0-23.fc35                build    37 k
DEBUG util.py:446:   python3-testtools         noarch    2.5.0-2.fc35                 build   332 k

The lockfile.pidlockfile.PIDLockFile class has these attributes (in the package lockfile == 0.12.2). Is that the case for you?

When using an instance of that class in these test cases, we want those attributes mocked for this collection of test cases. Are you saying these are already mocked somewhere else?

Metadata Update from @bignose:
- Pull-request tagged with: more-info

2 years ago

The lockfile.pidlockfile.PIDLockFile class has these attributes (in the package lockfile == 0.12.2). Is that the case for you?

Not sure. I was having this problem 3 months ago and I fixed it with the provided patch.

When using an instance of that class in these test cases, we want those attributes mocked for this collection of test cases. Are you saying these are already mocked somewhere else?

Yes, I think they are. Let me try to figure out where.

In the decorators of the relevant tests:

  • test_acquire_uses_specified_timeout
  • test_acquire_uses_stored_timeout_by_default
  • test_calls_superclass_init

I have merged an equivalent set of changes (attributed to the author of this merge request; thanks!) in commit 0c67a3c.

Pull-Request has been closed by bignose

2 years ago

Pull-Request has been closed by churchyard

2 years ago
Metadata