diff options
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r-- | Lib/unittest/test/test_assertions.py | 18 | ||||
-rw-r--r-- | Lib/unittest/test/test_loader.py | 8 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/support.py | 2 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testhelpers.py | 14 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmagicmethods.py | 1 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 39 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testpatch.py | 10 |
7 files changed, 74 insertions, 18 deletions
diff --git a/Lib/unittest/test/test_assertions.py b/Lib/unittest/test/test_assertions.py index e6e2bc2..1b0e833 100644 --- a/Lib/unittest/test/test_assertions.py +++ b/Lib/unittest/test/test_assertions.py @@ -240,7 +240,7 @@ class TestLongMessage(unittest.TestCase): # Error messages are multiline so not testing on full message # assertTupleEqual and assertListEqual delegate to this method self.assertMessages('assertSequenceEqual', ([], [None]), - ["\+ \[None\]$", "^oops$", r"\+ \[None\]$", + [r"\+ \[None\]$", "^oops$", r"\+ \[None\]$", r"\+ \[None\] : oops$"]) def testAssertSetEqual(self): @@ -250,21 +250,21 @@ class TestLongMessage(unittest.TestCase): def testAssertIn(self): self.assertMessages('assertIn', (None, []), - ['^None not found in \[\]$', "^oops$", - '^None not found in \[\]$', - '^None not found in \[\] : oops$']) + [r'^None not found in \[\]$', "^oops$", + r'^None not found in \[\]$', + r'^None not found in \[\] : oops$']) def testAssertNotIn(self): self.assertMessages('assertNotIn', (None, [None]), - ['^None unexpectedly found in \[None\]$', "^oops$", - '^None unexpectedly found in \[None\]$', - '^None unexpectedly found in \[None\] : oops$']) + [r'^None unexpectedly found in \[None\]$', "^oops$", + r'^None unexpectedly found in \[None\]$', + r'^None unexpectedly found in \[None\] : oops$']) def testAssertDictEqual(self): self.assertMessages('assertDictEqual', ({}, {'key': 'value'}), [r"\+ \{'key': 'value'\}$", "^oops$", - "\+ \{'key': 'value'\}$", - "\+ \{'key': 'value'\} : oops$"]) + r"\+ \{'key': 'value'\}$", + r"\+ \{'key': 'value'\} : oops$"]) def testAssertDictContainsSubset(self): with warnings.catch_warnings(): diff --git a/Lib/unittest/test/test_loader.py b/Lib/unittest/test/test_loader.py index 31e2f0f..1131a75 100644 --- a/Lib/unittest/test/test_loader.py +++ b/Lib/unittest/test/test_loader.py @@ -390,7 +390,7 @@ class Test_TestLoader(unittest.TestCase): suite = loader.loadTestsFromName('abc () //') error, test = self.check_deferred_error(loader, suite) expected = "Failed to import test module: abc () //" - expected_regex = "Failed to import test module: abc \(\) //" + expected_regex = r"Failed to import test module: abc \(\) //" self.assertIn( expected, error, 'missing error string in %r' % error) @@ -502,7 +502,7 @@ class Test_TestLoader(unittest.TestCase): suite = loader.loadTestsFromName('abc () //', unittest) error, test = self.check_deferred_error(loader, suite) expected = "module 'unittest' has no attribute 'abc () //'" - expected_regex = "module 'unittest' has no attribute 'abc \(\) //'" + expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'" self.assertIn( expected, error, 'missing error string in %r' % error) @@ -809,7 +809,7 @@ class Test_TestLoader(unittest.TestCase): suite = loader.loadTestsFromNames(['abc () //']) error, test = self.check_deferred_error(loader, list(suite)[0]) expected = "Failed to import test module: abc () //" - expected_regex = "Failed to import test module: abc \(\) //" + expected_regex = r"Failed to import test module: abc \(\) //" self.assertIn( expected, error, 'missing error string in %r' % error) @@ -928,7 +928,7 @@ class Test_TestLoader(unittest.TestCase): suite = loader.loadTestsFromNames(['abc () //'], unittest) error, test = self.check_deferred_error(loader, list(suite)[0]) expected = "module 'unittest' has no attribute 'abc () //'" - expected_regex = "module 'unittest' has no attribute 'abc \(\) //'" + expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'" self.assertIn( expected, error, 'missing error string in %r' % error) diff --git a/Lib/unittest/test/testmock/support.py b/Lib/unittest/test/testmock/support.py index f473879..205431a 100644 --- a/Lib/unittest/test/testmock/support.py +++ b/Lib/unittest/test/testmock/support.py @@ -1,5 +1,3 @@ -import sys - def is_instance(obj, klass): """Version of is_instance that doesn't access __class__""" return issubclass(type(obj), klass) diff --git a/Lib/unittest/test/testmock/testhelpers.py b/Lib/unittest/test/testmock/testhelpers.py index 3477634..d5f9e7c 100644 --- a/Lib/unittest/test/testmock/testhelpers.py +++ b/Lib/unittest/test/testmock/testhelpers.py @@ -306,6 +306,20 @@ class CallTest(unittest.TestCase): other_args = _Call(((1, 2), {'a': 3})) self.assertEqual(args, other_args) + def test_call_with_name(self): + self.assertEqual( + 'foo', + _Call((), 'foo')[0], + ) + self.assertEqual( + '', + _Call((('bar', 'barz'), ), )[0] + ) + self.assertEqual( + '', + _Call((('bar', 'barz'), {'hello': 'world'}), )[0] + ) + class SpecSignatureTest(unittest.TestCase): diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py index bb9b956..24569b5 100644 --- a/Lib/unittest/test/testmock/testmagicmethods.py +++ b/Lib/unittest/test/testmock/testmagicmethods.py @@ -1,5 +1,4 @@ import unittest -import inspect import sys from unittest.mock import Mock, MagicMock, _magics diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index a96ec68..b64c866 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1250,6 +1250,27 @@ class MockTest(unittest.TestCase): with self.assertRaises(AssertionError): m.hello.assert_not_called() + def test_assert_called(self): + m = Mock() + with self.assertRaises(AssertionError): + m.hello.assert_called() + m.hello() + m.hello.assert_called() + + m.hello() + m.hello.assert_called() + + def test_assert_called_once(self): + m = Mock() + with self.assertRaises(AssertionError): + m.hello.assert_called_once() + m.hello() + m.hello.assert_called_once() + + m.hello() + with self.assertRaises(AssertionError): + m.hello.assert_called_once() + #Issue21256 printout of keyword args should be in deterministic order def test_sorted_call_signature(self): m = Mock() @@ -1267,6 +1288,24 @@ class MockTest(unittest.TestCase): self.assertEqual(m.method_calls[0], c) self.assertEqual(m.method_calls[1], i) + def test_reset_return_sideeffect(self): + m = Mock(return_value=10, side_effect=[2,3]) + m.reset_mock(return_value=True, side_effect=True) + self.assertIsInstance(m.return_value, Mock) + self.assertEqual(m.side_effect, None) + + def test_reset_return(self): + m = Mock(return_value=10, side_effect=[2,3]) + m.reset_mock(return_value=True) + self.assertIsInstance(m.return_value, Mock) + self.assertNotEqual(m.side_effect, None) + + def test_reset_sideeffect(self): + m = Mock(return_value=10, side_effect=[2,3]) + m.reset_mock(side_effect=True) + self.assertEqual(m.return_value, 10) + self.assertEqual(m.side_effect, None) + 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 dfce369..fe4ecef 100644 --- a/Lib/unittest/test/testmock/testpatch.py +++ b/Lib/unittest/test/testmock/testpatch.py @@ -10,9 +10,9 @@ from unittest.test.testmock import support from unittest.test.testmock.support import SomeClass, is_instance from unittest.mock import ( - NonCallableMock, CallableMixin, patch, sentinel, + NonCallableMock, CallableMixin, sentinel, MagicMock, Mock, NonCallableMagicMock, patch, _patch, - DEFAULT, call, _get_target, _patch + DEFAULT, call, _get_target ) @@ -969,8 +969,14 @@ class PatchTest(unittest.TestCase): def test_autospec_function(self): @patch('%s.function' % __name__, autospec=True) def test(mock): + function.assert_not_called() + self.assertRaises(AssertionError, function.assert_called) + self.assertRaises(AssertionError, function.assert_called_once) function(1) + self.assertRaises(AssertionError, function.assert_not_called) function.assert_called_with(1) + function.assert_called() + function.assert_called_once() function(2, 3) function.assert_called_with(2, 3) |