我们从Python开源项目中,提取了以下31个代码示例,用于说明如何使用unittest.mock.called()。
def test_assert_called_once_with_function_spec(self): def f(a, b, c, d=None): pass mock = Mock(spec=f) mock(1, b=2, c=3) mock.assert_called_once_with(1, 2, 3) mock.assert_called_once_with(a=1, b=2, c=3) self.assertRaises(AssertionError, mock.assert_called_once_with, 1, b=3, c=2) # Expected call doesn't match the spec's signature with self.assertRaises(AssertionError) as cm: mock.assert_called_once_with(e=8) self.assertIsInstance(cm.exception.__cause__, TypeError) # Mock called more than once => always fails mock(4, 5, 6) self.assertRaises(AssertionError, mock.assert_called_once_with, 1, 2, 3) self.assertRaises(AssertionError, mock.assert_called_once_with, 4, 5, 6)
def test_can_decode_utf8_strings_with_french_content(self, encoding_mocker_stopall): # mock mock_getpreferredencoding = encoding_mocker_stopall.MagicMock(name="mock_getpreferredencoding") # patch encoding_mocker_stopall.patch.object(scarlett_os.internal.encoding.locale, 'getpreferredencoding', mock_getpreferredencoding) mock.return_value = 'UTF-8' # source: http://pythoncentral.io/encoding-and-decoding-strings-in-python-3-x/ # NOTE: In Python 2.x, prefixing a string literal with a "b" (or "B") is legal syntax, but it does nothing special: # NOTE: In Python 3.x, however, this prefix indicates the string is a bytes object which differs from the normal string (which as we know is by default a Unicode string), and even the 'b' prefix is preserved: result = encoding.locale_decode( b'[Errno 98] Adresse d\xc3\xa9j\xc3\xa0 utilis\xc3\xa9e') assert "b'[Errno 98] Adresse d\\xc3\\xa9j\\xc3\\xa0 utilis\\xc3\\xa9e'" == result # def test_can_decode_an_ioerror_with_french_content(self, mock): # mock.return_value = 'UTF-8' # # error = IOError(98, b'Adresse d\xc3\xa9j\xc3\xa0 utilis\xc3\xa9e') # result = encoding.locale_decode(error) # expected = '[Errno 98] Adresse d\xe9j\xe0 utilis\xe9e' # # self.assertEqual( # expected, result, # '%r decoded to %r does not match expected %r' % ( # error, result, expected)) # # def test_does_not_use_locale_to_decode_unicode_strings(self, mock): # mock.return_value = 'UTF-8' # # encoding.locale_decode('abc') # # self.assertFalse(mock.called) # # def test_does_not_use_locale_to_decode_ascii_bytestrings(self, mock): # mock.return_value = 'UTF-8' # # encoding.locale_decode('abc') # # self.assertFalse(mock.called)
def test_draw_on_texture(self, _draw_content_mock, _save): self.img.draw_on_texture() self.assertIsNotNone(self.img.opposite_to_bg_color) self.assertTrue(_draw_content_mock.called)
def test_draw_on_bg(self, _draw_content_mock, _save): self.img.draw_on_bg() self.assertIsNotNone(self.img.opposite_to_bg_color) self.assertTrue(_draw_content_mock.called)
def test_draw_on_image(self, _draw_content_mock, _save): with create_test_image(): self.img.draw_on_image( image_path=os.path.abspath('test.png')) self.assertIsNotNone(self.img.opposite_to_bg_color) self.assertTrue(_draw_content_mock.called)
def test_draw_on_image_with_enhancements(self, _draw_content_mock, _save, enhance_mock): with create_test_image(): enhance_mock.return_value = PIL_Image.open('test.png') self.img.draw_on_image( image_path=os.path.abspath('test.png'), image_enhancements=((ImageEnhance.Sharpness, 0.5), (ImageEnhance.Brightness, 0.5))) self.assertTrue(enhance_mock.called) self.assertTrue(_draw_content_mock.called)
def test_draw_on_image_with_filters(self, _draw_content_mock, _save, filter_mock): filters = (ImageFilter.BLUR, ImageFilter.GaussianBlur(2)) with create_test_image(): filter_mock.return_value = PIL_Image.open('test.png') self.img.draw_on_image( image_path=os.path.abspath('test.png'), image_filters=filters) self.assertTrue(filter_mock.called) self.assertTrue(_draw_content_mock.called)
def test_draw_linkback(self, mock): self.img.color = '#000' aligns = ['center', 'right', 'left'] for align in aligns: with self.subTest(): self.img.linkback.align = align self.img._draw_linkback() self.assertTrue(mock.called)
def test_draw_content(self, _draw_header_mock, _draw_para_mock, _draw_linkback_mock): self.img._draw_content() self.assertTrue(_draw_header_mock.called) self.assertTrue(_draw_para_mock.called) self.assertTrue(_draw_linkback_mock.called)
def test_draw_unit_with_outline(self, text_mock): available_outlines = [None, Outline(2, '#111')] self.img.color = '#000' start_height = 0 aligns = ['center', 'right', 'left'] for align in aligns: for outline in available_outlines: with self.subTest(): unit = MultilineTextUnit( text='foo', outline=outline, align=align) self.img._draw_unit(start_height, unit) self.assertTrue(text_mock.called)
def test_constructor(self): mock = Mock() self.assertFalse(mock.called, "called not initialised correctly") self.assertEqual(mock.call_count, 0, "call_count not initialised correctly") self.assertTrue(is_instance(mock.return_value, Mock), "return_value not initialised correctly") self.assertEqual(mock.call_args, None, "call_args not initialised correctly") self.assertEqual(mock.call_args_list, [], "call_args_list not initialised correctly") self.assertEqual(mock.method_calls, [], "method_calls not initialised correctly") # Can't use hasattr for this test as it always returns True on a mock self.assertFalse('_items' in mock.__dict__, "default mock should not have '_items' attribute") self.assertIsNone(mock._mock_parent, "parent not initialised correctly") self.assertIsNone(mock._mock_methods, "methods not initialised correctly") self.assertEqual(mock._mock_children, {}, "children not initialised incorrectly")
def test_reset_mock(self): parent = Mock() spec = ["something"] mock = Mock(name="child", parent=parent, spec=spec) mock(sentinel.Something, something=sentinel.SomethingElse) something = mock.something mock.something() mock.side_effect = sentinel.SideEffect return_value = mock.return_value return_value() mock.reset_mock() self.assertEqual(mock._mock_name, "child", "name incorrectly reset") self.assertEqual(mock._mock_parent, parent, "parent incorrectly reset") self.assertEqual(mock._mock_methods, spec, "methods incorrectly reset") self.assertFalse(mock.called, "called not reset") self.assertEqual(mock.call_count, 0, "call_count not reset") self.assertEqual(mock.call_args, None, "call_args not reset") self.assertEqual(mock.call_args_list, [], "call_args_list not reset") self.assertEqual(mock.method_calls, [], "method_calls not initialised correctly: %r != %r" % (mock.method_calls, [])) self.assertEqual(mock.mock_calls, []) self.assertEqual(mock.side_effect, sentinel.SideEffect, "side_effect incorrectly reset") self.assertEqual(mock.return_value, return_value, "return_value incorrectly reset") self.assertFalse(return_value.called, "return value mock not reset") self.assertEqual(mock._mock_children, {'something': something}, "children reset incorrectly") self.assertEqual(mock.something, something, "children incorrectly cleared") self.assertFalse(mock.something.called, "child not reset")
def test_call(self): mock = Mock() self.assertTrue(is_instance(mock.return_value, Mock), "Default return_value should be a Mock") result = mock() self.assertEqual(mock(), result, "different result from consecutive calls") mock.reset_mock() ret_val = mock(sentinel.Arg) self.assertTrue(mock.called, "called not set") self.assertEqual(mock.call_count, 1, "call_count incoreect") self.assertEqual(mock.call_args, ((sentinel.Arg,), {}), "call_args not set") self.assertEqual(mock.call_args_list, [((sentinel.Arg,), {})], "call_args_list not initialised correctly") mock.return_value = sentinel.ReturnValue ret_val = mock(sentinel.Arg, key=sentinel.KeyArg) self.assertEqual(ret_val, sentinel.ReturnValue, "incorrect return value") self.assertEqual(mock.call_count, 2, "call_count incorrect") self.assertEqual(mock.call_args, ((sentinel.Arg,), {'key': sentinel.KeyArg}), "call_args not set") self.assertEqual(mock.call_args_list, [ ((sentinel.Arg,), {}), ((sentinel.Arg,), {'key': sentinel.KeyArg}) ], "call_args_list not set")
def test_attribute_access_returns_mocks(self): mock = Mock() something = mock.something self.assertTrue(is_instance(something, Mock), "attribute isn't a mock") self.assertEqual(mock.something, something, "different attributes returned for same name") # Usage example mock = Mock() mock.something.return_value = 3 self.assertEqual(mock.something(), 3, "method returned wrong value") self.assertTrue(mock.something.called, "method didn't record being called")
def test_wraps_call_with_nondefault_return_value(self): real = Mock() mock = Mock(wraps=real) mock.return_value = 3 self.assertEqual(mock(), 3) self.assertFalse(real.called)
def test_constructor(self): mock = Mock() self.assertFalse(mock.called, "called not initialised correctly") self.assertEqual(mock.call_count, 0, "call_count not initialised correctly") self.assertTrue(is_instance(mock.return_value, Mock), "return_value not initialised correctly") self.assertEqual(mock.call_args, None, "call_args not initialised correctly") self.assertEqual(mock.call_args_list, [], "call_args_list not initialised correctly") self.assertEqual(mock.method_calls, [], "method_calls not initialised correctly") # Can't use hasattr for this test as it always returns True on a mock self.assertNotIn('_items', mock.__dict__, "default mock should not have '_items' attribute") self.assertIsNone(mock._mock_parent, "parent not initialised correctly") self.assertIsNone(mock._mock_methods, "methods not initialised correctly") self.assertEqual(mock._mock_children, {}, "children not initialised incorrectly")
def test_assert_called_with_message(self): mock = Mock() self.assertRaisesRegex(AssertionError, 'Not called', mock.assert_called_with)
def test_assert_called_once_with_message(self): mock = Mock(name='geoffrey') self.assertRaisesRegex(AssertionError, r"Expected 'geoffrey' to be called once\.", mock.assert_called_once_with)
def test_assert_called_with_failure_message(self): mock = NonCallableMock() expected = "mock(1, '2', 3, bar='foo')" message = 'Expected call: %s\nNot called' self.assertRaisesWithMsg( AssertionError, message % (expected,), mock.assert_called_with, 1, '2', 3, bar='foo' ) mock.foo(1, '2', 3, foo='foo') asserters = [ mock.foo.assert_called_with, mock.foo.assert_called_once_with ] for meth in asserters: actual = "foo(1, '2', 3, foo='foo')" expected = "foo(1, '2', 3, bar='foo')" message = 'Expected call: %s\nActual call: %s' self.assertRaisesWithMsg( AssertionError, message % (expected, actual), meth, 1, '2', 3, bar='foo' ) # just kwargs for meth in asserters: actual = "foo(1, '2', 3, foo='foo')" expected = "foo(bar='foo')" message = 'Expected call: %s\nActual call: %s' self.assertRaisesWithMsg( AssertionError, message % (expected, actual), meth, bar='foo' ) # just args for meth in asserters: actual = "foo(1, '2', 3, foo='foo')" expected = "foo(1, 2, 3)" message = 'Expected call: %s\nActual call: %s' self.assertRaisesWithMsg( AssertionError, message % (expected, actual), meth, 1, 2, 3 ) # empty for meth in asserters: actual = "foo(1, '2', 3, foo='foo')" expected = "foo()" message = 'Expected call: %s\nActual call: %s' self.assertRaisesWithMsg( AssertionError, message % (expected, actual), meth )