本文整理汇总了Python中unittest.mock.foo函数的典型用法代码示例。如果您正苦于以下问题:Python foo函数的具体用法?Python foo怎么用?Python foo使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了foo函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_assert_has_calls
def test_assert_has_calls(self):
kalls1 = [call(1, 2), ({"a": 3},), ((3, 4),), call(b=6), ("", (1,), {"b": 6})]
kalls2 = [call.foo(), call.bar(1)]
kalls2.extend(call.spam().baz(a=3).call_list())
kalls2.extend(call.bam(set(), foo={}).fish([1]).call_list())
mocks = []
for mock in Mock(), MagicMock():
mock(1, 2)
mock(a=3)
mock(3, 4)
mock(b=6)
mock(1, b=6)
mocks.append((mock, kalls1))
mock = Mock()
mock.foo()
mock.bar(1)
mock.spam().baz(a=3)
mock.bam(set(), foo={}).fish([1])
mocks.append((mock, kalls2))
for mock, kalls in mocks:
for i in range(len(kalls)):
for step in 1, 2, 3:
these = kalls[i : i + step]
mock.assert_has_calls(these)
if len(these) > 1:
self.assertRaises(AssertionError, mock.assert_has_calls, list(reversed(these)))
示例2: test_arg_lists
def test_arg_lists(self):
mocks = [
Mock(),
MagicMock(),
NonCallableMock(),
NonCallableMagicMock()
]
def assert_attrs(mock):
names = 'call_args_list', 'method_calls', 'mock_calls'
for name in names:
attr = getattr(mock, name)
self.assertIsInstance(attr, _CallList)
self.assertIsInstance(attr, list)
self.assertEqual(attr, [])
for mock in mocks:
assert_attrs(mock)
if callable(mock):
mock()
mock(1, 2)
mock(a=3)
mock.reset_mock()
assert_attrs(mock)
mock.foo()
mock.foo.bar(1, a=3)
mock.foo(1).bar().baz(3)
mock.reset_mock()
assert_attrs(mock)
示例3: test_assert_called_with_failure_message
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')
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')
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)
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)
示例4: test_repr
def test_repr(self):
mock = Mock(name='foo')
self.assertIn('foo', repr(mock))
self.assertIn("'%s'" % id(mock), repr(mock))
mocks = [(Mock(), 'mock'), (Mock(name='bar'), 'bar')]
for (mock, name) in mocks:
self.assertIn('%s.bar' % name, repr(mock.bar))
self.assertIn('%s.foo()' % name, repr(mock.foo()))
self.assertIn('%s.foo().bing' % name, repr(mock.foo().bing))
self.assertIn('%s()' % name, repr(mock()))
self.assertIn('%s()()' % name, repr(mock()()))
self.assertIn('%s()().foo.bar.baz().bing' % name, repr(mock()().foo.bar.baz().bing))
示例5: test_repr
def test_repr(self):
mock = Mock(name="foo")
self.assertIn("foo", repr(mock))
self.assertIn("'%s'" % id(mock), repr(mock))
mocks = [(Mock(), "mock"), (Mock(name="bar"), "bar")]
for mock, name in mocks:
self.assertIn("%s.bar" % name, repr(mock.bar))
self.assertIn("%s.foo()" % name, repr(mock.foo()))
self.assertIn("%s.foo().bing" % name, repr(mock.foo().bing))
self.assertIn("%s()" % name, repr(mock()))
self.assertIn("%s()()" % name, repr(mock()()))
self.assertIn("%s()().foo.bar.baz().bing" % name, repr(mock()().foo.bar.baz().bing))
示例6: test_mock_calls
def test_mock_calls(self):
mock = MagicMock()
self.assertIs(mock.mock_calls == [], True)
mock = MagicMock()
mock()
expected = [('', (), {})]
self.assertEqual(mock.mock_calls, expected)
mock.foo()
expected.append(call.foo())
self.assertEqual(mock.mock_calls, expected)
self.assertEqual(mock.foo.mock_calls, [('', (), {})])
mock = MagicMock()
mock().foo(1, 2, 3, a=4, b=5)
expected = [('', (), {}), ('().foo', (1, 2, 3), dict(a=4, b=5))]
self.assertEqual(mock.mock_calls, expected)
self.assertEqual(mock.return_value.foo.mock_calls, [('', (1, 2, 3), dict(a=4, b=5))])
self.assertEqual(mock.return_value.mock_calls, [('foo', (1, 2, 3), dict(a=4, b=5))])
mock = MagicMock()
mock().foo.bar().baz()
expected = [('', (), {}), ('().foo.bar', (), {}), ('().foo.bar().baz', (), {})]
self.assertEqual(mock.mock_calls, expected)
self.assertEqual(mock().mock_calls, call.foo.bar().baz().call_list())
for kwargs in (dict(), dict(name='bar')):
mock = MagicMock(**kwargs)
int(mock.foo)
expected = [('foo.__int__', (), {})]
self.assertEqual(mock.mock_calls, expected)
mock = MagicMock(**kwargs)
mock.a()()
expected = [('a', (), {}), ('a()', (), {})]
self.assertEqual(mock.mock_calls, expected)
self.assertEqual(mock.a().mock_calls, [call()])
mock = MagicMock(**kwargs)
mock(1)(2)(3)
self.assertEqual(mock.mock_calls, call(1)(2)(3).call_list())
self.assertEqual(mock().mock_calls, call(2)(3).call_list())
self.assertEqual(mock()().mock_calls, call(3).call_list())
mock = MagicMock(**kwargs)
mock(1)(2)(3).a.b.c(4)
self.assertEqual(mock.mock_calls, call(1)(2)(3).a.b.c(4).call_list())
self.assertEqual(mock().mock_calls, call(2)(3).a.b.c(4).call_list())
self.assertEqual(mock()().mock_calls, call(3).a.b.c(4).call_list())
mock = MagicMock(**kwargs)
int(mock().foo.bar().baz())
last_call = ('().foo.bar().baz().__int__', (), {})
self.assertEqual(mock.mock_calls[-1], last_call)
self.assertEqual(mock().mock_calls, call.foo.bar().baz().__int__().call_list())
self.assertEqual(mock().foo.bar().mock_calls, call.baz().__int__().call_list())
self.assertEqual(mock().foo.bar().baz.mock_calls, call().__int__().call_list())
示例7: test_adding_child_mock
def test_adding_child_mock(self):
for Klass in (NonCallableMock, Mock, MagicMock, NonCallableMagicMock):
mock = Klass()
mock.foo = Mock()
mock.foo()
self.assertEqual(mock.method_calls, [call.foo()])
self.assertEqual(mock.mock_calls, [call.foo()])
mock = Klass()
mock.bar = Mock(name='name')
mock.bar()
self.assertEqual(mock.method_calls, [])
self.assertEqual(mock.mock_calls, [])
mock = Klass()
mock.baz = MagicMock()()
mock.baz()
self.assertEqual(mock.method_calls, [])
self.assertEqual(mock.mock_calls, [])
示例8: test_adding_child_mock
def test_adding_child_mock(self):
for Klass in NonCallableMock, Mock, MagicMock, NonCallableMagicMock:
mock = Klass()
mock.foo = Mock()
mock.foo()
self.assertEqual(mock.method_calls, [call.foo()])
self.assertEqual(mock.mock_calls, [call.foo()])
mock = Klass()
mock.bar = Mock(name='name')
mock.bar()
self.assertEqual(mock.method_calls, [])
self.assertEqual(mock.mock_calls, [])
# mock with an existing _new_parent but no name
mock = Klass()
mock.baz = MagicMock()()
mock.baz()
self.assertEqual(mock.method_calls, [])
self.assertEqual(mock.mock_calls, [])
示例9: test_assert_called_with_failure_message
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)
示例10: test_mock_calls
def test_mock_calls(self):
mock = MagicMock()
# need to do this because MagicMock.mock_calls used to just return
# a MagicMock which also returned a MagicMock when __eq__ was called
self.assertIs(mock.mock_calls == [], True)
mock = MagicMock()
mock()
expected = [('', (), {})]
self.assertEqual(mock.mock_calls, expected)
mock.foo()
expected.append(call.foo())
self.assertEqual(mock.mock_calls, expected)
# intermediate mock_calls work too
self.assertEqual(mock.foo.mock_calls, [('', (), {})])
mock = MagicMock()
mock().foo(1, 2, 3, a=4, b=5)
expected = [
('', (), {}), ('().foo', (1, 2, 3), dict(a=4, b=5))
]
self.assertEqual(mock.mock_calls, expected)
self.assertEqual(mock.return_value.foo.mock_calls,
[('', (1, 2, 3), dict(a=4, b=5))])
self.assertEqual(mock.return_value.mock_calls,
[('foo', (1, 2, 3), dict(a=4, b=5))])
mock = MagicMock()
mock().foo.bar().baz()
expected = [
('', (), {}), ('().foo.bar', (), {}),
('().foo.bar().baz', (), {})
]
self.assertEqual(mock.mock_calls, expected)
self.assertEqual(mock().mock_calls,
call.foo.bar().baz().call_list())
for kwargs in dict(), dict(name='bar'):
mock = MagicMock(**kwargs)
int(mock.foo)
expected = [('foo.__int__', (), {})]
self.assertEqual(mock.mock_calls, expected)
mock = MagicMock(**kwargs)
mock.a()()
expected = [('a', (), {}), ('a()', (), {})]
self.assertEqual(mock.mock_calls, expected)
self.assertEqual(mock.a().mock_calls, [call()])
mock = MagicMock(**kwargs)
mock(1)(2)(3)
self.assertEqual(mock.mock_calls, call(1)(2)(3).call_list())
self.assertEqual(mock().mock_calls, call(2)(3).call_list())
self.assertEqual(mock()().mock_calls, call(3).call_list())
mock = MagicMock(**kwargs)
mock(1)(2)(3).a.b.c(4)
self.assertEqual(mock.mock_calls,
call(1)(2)(3).a.b.c(4).call_list())
self.assertEqual(mock().mock_calls,
call(2)(3).a.b.c(4).call_list())
self.assertEqual(mock()().mock_calls,
call(3).a.b.c(4).call_list())
mock = MagicMock(**kwargs)
int(mock().foo.bar().baz())
last_call = ('().foo.bar().baz().__int__', (), {})
self.assertEqual(mock.mock_calls[-1], last_call)
self.assertEqual(mock().mock_calls,
call.foo.bar().baz().__int__().call_list())
self.assertEqual(mock().foo.bar().mock_calls,
call.baz().__int__().call_list())
self.assertEqual(mock().foo.bar().baz.mock_calls,
call().__int__().call_list())
示例11: MagicMock
# programme ...).
# Pour cela il y a les mock :
"""Un objet mock, c’est un objet basé sur le null object pattern qui sert à
faire semblant. Quand on l’instancie avec n’importe quoi, ça marche, quand on
appelle n’importe quelle méthode, ça marche et ça renvoie un mock"""
import unittest.mock
# Un objet mock peut être appeler comme une classe ou une fonction
# et retouren toujours un mock. Il est possible d'appeler n'importe quel
# méthode et retournera toujors un mock
from unittest.mock import MagicMock # ou from mock import MagicMock
mock = MagicMock()
print(mock)
print(mock())
print(mock(1, True, [Exception, {}]))
print(mock.foo())
print(mock.nimporte().nawak().je().te().dis())
print(mock + mock - 10000)
# Il est possible de faire en sorte qu'ils aient un comportement spécifique.
mock.you = MagicMock(side_effect=ValueError('mofo !')) # un callable marche aussi
#mock.you() # Provoque une ValueError
mock.mock = MagicMock(return_value="moooooooooock")
print(mock.mock())
# Il est possible de combiner de vrai objets avec des mock
class maClasse:
def func1(self):
print("coucou")
def func2(self):