diff options
author | Larry Hastings <larry@hastings.org> | 2014-05-18 23:15:35 -0700 |
---|---|---|
committer | Larry Hastings <larry@hastings.org> | 2014-05-18 23:15:35 -0700 |
commit | cc001283f12f300f7e1c2f4c235273417ccf964f (patch) | |
tree | 5747ccc983dcf3cb8073b041e01ca016a9138b26 /Lib/unittest | |
parent | c5f310167168d11805a9d5d15cc743e7092c0b71 (diff) | |
parent | 9c7f094fee7e83e4278abb45524089ad02fa59a7 (diff) | |
download | cpython-cc001283f12f300f7e1c2f4c235273417ccf964f.tar.gz |
Null merge 3.4.1 (and 3.4.1rc1) release-engineering revisions.
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 23 | ||||
-rw-r--r-- | Lib/unittest/test/test_case.py | 2 | ||||
-rw-r--r-- | Lib/unittest/test/test_loader.py | 10 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 20 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testpatch.py | 24 |
5 files changed, 69 insertions, 10 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 5555774467..d9c2ee96b0 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -27,9 +27,13 @@ __version__ = '1.0' import inspect import pprint import sys +import builtins +from types import ModuleType from functools import wraps, partial +_builtins = {name for name in dir(builtins) if not name.startswith('_')} + BaseExceptions = (BaseException,) if 'java' in sys.platform: # jython @@ -375,7 +379,7 @@ class NonCallableMock(Base): def __init__( self, spec=None, wraps=None, name=None, spec_set=None, parent=None, _spec_state=None, _new_name='', _new_parent=None, - _spec_as_instance=False, _eat_self=None, **kwargs + _spec_as_instance=False, _eat_self=None, unsafe=False, **kwargs ): if _new_parent is None: _new_parent = parent @@ -405,6 +409,7 @@ class NonCallableMock(Base): __dict__['_mock_mock_calls'] = _CallList() __dict__['method_calls'] = _CallList() + __dict__['_mock_unsafe'] = unsafe if kwargs: self.configure_mock(**kwargs) @@ -561,13 +566,16 @@ class NonCallableMock(Base): def __getattr__(self, name): - if name == '_mock_methods': + if name in {'_mock_methods', '_mock_unsafe'}: raise AttributeError(name) elif self._mock_methods is not None: if name not in self._mock_methods or name in _all_magics: raise AttributeError("Mock object has no attribute %r" % name) elif _is_magic(name): raise AttributeError(name) + if not self._mock_unsafe: + if name.startswith(('assert', 'assret')): + raise AttributeError(name) result = self._mock_children.get(name) if result is _deleted: @@ -750,6 +758,14 @@ class NonCallableMock(Base): else: return _call + def assert_not_called(_mock_self): + """assert that the mock was never called. + """ + self = _mock_self + if self.call_count != 0: + msg = ("Expected '%s' to not have been called. Called %s times." % + (self._mock_name or 'mock', self.call_count)) + raise AssertionError(msg) def assert_called_with(_mock_self, *args, **kwargs): """assert that the mock was called with the specified arguments. @@ -1166,6 +1182,9 @@ class _patch(object): else: local = True + if name in _builtins and isinstance(target, ModuleType): + self.create = True + if not self.create and original is DEFAULT: raise AttributeError( "%s does not have the attribute %r" % (target, name) diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index 49325784d2..aae7ec3fa5 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -1080,7 +1080,7 @@ test case # so can't use assertEqual either. Just use assertTrue. self.assertTrue(sample_text_error == error) - def testAsertEqualSingleLine(self): + def testAssertEqualSingleLine(self): sample_text = "laden swallows fly slowly" revised_sample_text = "unladen swallows fly quickly" sample_text_error = """\ diff --git a/Lib/unittest/test/test_loader.py b/Lib/unittest/test/test_loader.py index b62a1b5c54..3e013af4c2 100644 --- a/Lib/unittest/test/test_loader.py +++ b/Lib/unittest/test/test_loader.py @@ -255,7 +255,7 @@ class Test_TestLoader(unittest.TestCase): try: loader.loadTestsFromName('unittest.sdasfasfasdf') except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") @@ -272,7 +272,7 @@ class Test_TestLoader(unittest.TestCase): try: loader.loadTestsFromName('sdasfasfasdf', unittest) except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") @@ -635,7 +635,7 @@ class Test_TestLoader(unittest.TestCase): try: loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest']) except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError") @@ -654,7 +654,7 @@ class Test_TestLoader(unittest.TestCase): try: loader.loadTestsFromNames(['sdasfasfasdf'], unittest) except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") @@ -673,7 +673,7 @@ class Test_TestLoader(unittest.TestCase): try: loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest) except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 23675b9af6..b65dc321cf 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1187,6 +1187,26 @@ class MockTest(unittest.TestCase): m = mock.create_autospec(object(), name='sweet_func') self.assertIn('sweet_func', repr(m)) + #Issue21238 + def test_mock_unsafe(self): + m = Mock() + with self.assertRaises(AttributeError): + m.assert_foo_call() + with self.assertRaises(AttributeError): + m.assret_foo_call() + m = Mock(unsafe=True) + m.assert_foo_call() + m.assret_foo_call() + + #Issue21262 + def test_assert_not_called(self): + m = Mock() + m.hello.assert_not_called() + m.hello() + with self.assertRaises(AssertionError): + m.hello.assert_not_called() + + def test_mock_add_spec(self): class _One(object): one = 1 diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py index b516f42af0..28fe86b0de 100644 --- a/Lib/unittest/test/testmock/testpatch.py +++ b/Lib/unittest/test/testmock/testpatch.py @@ -377,7 +377,7 @@ class PatchTest(unittest.TestCase): def test_patchobject_wont_create_by_default(self): try: - @patch.object(SomeClass, 'frooble', sentinel.Frooble) + @patch.object(SomeClass, 'ord', sentinel.Frooble) def test(): self.fail('Patching non existent attributes should fail') @@ -386,7 +386,27 @@ class PatchTest(unittest.TestCase): pass else: self.fail('Patching non existent attributes should fail') - self.assertFalse(hasattr(SomeClass, 'frooble')) + self.assertFalse(hasattr(SomeClass, 'ord')) + + + def test_patch_builtins_without_create(self): + @patch(__name__+'.ord') + def test_ord(mock_ord): + mock_ord.return_value = 101 + return ord('c') + + @patch(__name__+'.open') + def test_open(mock_open): + m = mock_open.return_value + m.read.return_value = 'abcd' + + fobj = open('doesnotexists.txt') + data = fobj.read() + fobj.close() + return data + + self.assertEqual(test_ord(), 101) + self.assertEqual(test_open(), 'abcd') def test_patch_with_static_methods(self): |