升级到最新的 moto 版本(1.3.15 和 1.3.16)后测试失败
Test broken after upgrade to latest moto version (1.3.15 and 1.3.16)
将 moto 从版本 1.3.14 更新到 1.3.15/1.3.16 中断测试并抛出异常。
即使单独使用注释也会引发错误。
我正在使用这个 requiriments.txt
文件:
moto==1.3.16
此测试示例适用于 moto 1.3.14,但不适用于较新的版本:
from moto import mock_s3
import unittest
@mock_s3
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
if __name__ == '__main__':
unittest.main()
这是执行时的错误 python3 -m unittest mytest.py
:
(venv) ~/p/mocotest $ python3 -m unittest mytest.py
ETraceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.8/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/usr/lib/python3.8/unittest/main.py", line 101, in __init__
self.runTests()
File "/usr/lib/python3.8/unittest/main.py", line 271, in runTests
self.result = testRunner.run(self.test)
File "/usr/lib/python3.8/unittest/runner.py", line 176, in run
test(result)
File "/usr/lib/python3.8/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.8/unittest/suite.py", line 122, in run
test(result)
File "/usr/lib/python3.8/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.8/unittest/suite.py", line 122, in run
test(result)
File "/usr/lib/python3.8/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.8/unittest/suite.py", line 122, in run
test(result)
File "/usr/lib/python3.8/unittest/case.py", line 736, in __call__
return self.run(*args, **kwds)
File "/home/cerveraa/projects/mocotest/venv/lib/python3.8/site-packages/moto/core/models.py", line 102, in wrapper
self.stop()
File "/home/cerveraa/projects/mocotest/venv/lib/python3.8/site-packages/moto/core/models.py", line 86, in stop
self.default_session_mock.stop()
File "/home/cerveraa/projects/mocotest/venv/lib/python3.8/site-packages/mock/mock.py", line 1563, in stop
return self.__exit__(None, None, None)
File "/home/cerveraa/projects/mocotest/venv/lib/python3.8/site-packages/mock/mock.py", line 1529, in __exit__
if self.is_local and self.temp_original is not DEFAULT:
AttributeError: '_patch' object has no attribute 'is_local'
版本:
- Python 3.8.5
- 点 20.0.2
有什么建议吗?
注1:
为了进行测试,我使用了全新的 venv
环境,并且唯一安装的库是 moto
.
注2:
使用 1.3.14 可以正常工作,但在安装依赖项时会出现警告:
ERROR: python-jose 3.2.0 has requirement ecdsa<0.15, but you'll have ecdsa 0.16.1 which is incompatible
同样的问题。 moto 模型的补丁功能会影响模拟功能。
(解决方法):受影响的注释行
def __exit__(self, *exc_info):
"""Undo the patch."""
#if self.is_local and self.temp_original is not DEFAULT:
# setattr(self.target, self.attribute, self.temp_original)
#else:
# delattr(self.target, self.attribute)
# if not self.create and (not hasattr(self.target, self.attribute) or
# self.attribute in ('__doc__', '__module__',
# '__defaults__', '__annotations__',
# '__kwdefaults__')):
# # needed for proxy objects like django settings
# setattr(self.target, self.attribute, self.temp_original)
#del self.temp_original
#del self.is_local
#del self.target
exit_stack = self._exit_stack
#del self._exit_stack
return exit_stack.__exit__(*exc_info)
- 这是
moto
和mock
库之间的兼容性问题
- 使用 moto 版本
moto==1.3.16
和 mock 版本 mock==4.0.2
解决了我的问题。
将 moto 从版本 1.3.14 更新到 1.3.15/1.3.16 中断测试并抛出异常。
即使单独使用注释也会引发错误。
我正在使用这个 requiriments.txt
文件:
moto==1.3.16
此测试示例适用于 moto 1.3.14,但不适用于较新的版本:
from moto import mock_s3
import unittest
@mock_s3
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
if __name__ == '__main__':
unittest.main()
这是执行时的错误 python3 -m unittest mytest.py
:
(venv) ~/p/mocotest $ python3 -m unittest mytest.py
ETraceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.8/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/usr/lib/python3.8/unittest/main.py", line 101, in __init__
self.runTests()
File "/usr/lib/python3.8/unittest/main.py", line 271, in runTests
self.result = testRunner.run(self.test)
File "/usr/lib/python3.8/unittest/runner.py", line 176, in run
test(result)
File "/usr/lib/python3.8/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.8/unittest/suite.py", line 122, in run
test(result)
File "/usr/lib/python3.8/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.8/unittest/suite.py", line 122, in run
test(result)
File "/usr/lib/python3.8/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.8/unittest/suite.py", line 122, in run
test(result)
File "/usr/lib/python3.8/unittest/case.py", line 736, in __call__
return self.run(*args, **kwds)
File "/home/cerveraa/projects/mocotest/venv/lib/python3.8/site-packages/moto/core/models.py", line 102, in wrapper
self.stop()
File "/home/cerveraa/projects/mocotest/venv/lib/python3.8/site-packages/moto/core/models.py", line 86, in stop
self.default_session_mock.stop()
File "/home/cerveraa/projects/mocotest/venv/lib/python3.8/site-packages/mock/mock.py", line 1563, in stop
return self.__exit__(None, None, None)
File "/home/cerveraa/projects/mocotest/venv/lib/python3.8/site-packages/mock/mock.py", line 1529, in __exit__
if self.is_local and self.temp_original is not DEFAULT:
AttributeError: '_patch' object has no attribute 'is_local'
版本:
- Python 3.8.5
- 点 20.0.2
有什么建议吗?
注1:
为了进行测试,我使用了全新的 venv
环境,并且唯一安装的库是 moto
.
注2: 使用 1.3.14 可以正常工作,但在安装依赖项时会出现警告:
ERROR: python-jose 3.2.0 has requirement ecdsa<0.15, but you'll have ecdsa 0.16.1 which is incompatible
同样的问题。 moto 模型的补丁功能会影响模拟功能。
(解决方法):受影响的注释行
def __exit__(self, *exc_info):
"""Undo the patch."""
#if self.is_local and self.temp_original is not DEFAULT:
# setattr(self.target, self.attribute, self.temp_original)
#else:
# delattr(self.target, self.attribute)
# if not self.create and (not hasattr(self.target, self.attribute) or
# self.attribute in ('__doc__', '__module__',
# '__defaults__', '__annotations__',
# '__kwdefaults__')):
# # needed for proxy objects like django settings
# setattr(self.target, self.attribute, self.temp_original)
#del self.temp_original
#del self.is_local
#del self.target
exit_stack = self._exit_stack
#del self._exit_stack
return exit_stack.__exit__(*exc_info)
- 这是
moto
和mock
库之间的兼容性问题 - 使用 moto 版本
moto==1.3.16
和 mock 版本mock==4.0.2
解决了我的问题。