Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
2cd48738
Kaydet (Commit)
2cd48738
authored
Nis 21, 2012
tarafından
Michael Foord
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Closes issue 14636. mock objects raise exceptions from an iterable side_effect
üst
24117a74
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
53 deletions
+31
-53
unittest.mock-examples.rst
Doc/library/unittest.mock-examples.rst
+0
-50
unittest.mock.rst
Doc/library/unittest.mock.rst
+14
-0
mock.py
Lib/unittest/mock.py
+7
-3
testmock.py
Lib/unittest/test/testmock/testmock.py
+10
-0
No files found.
Doc/library/unittest.mock-examples.rst
Dosyayı görüntüle @
2cd48738
...
...
@@ -838,56 +838,6 @@ and the `return_value` will use your subclass automatically. That means all
children of a `CopyingMock` will also have the type `CopyingMock`.
Multiple calls with different effects
-------------------------------------
Handling code that needs to behave differently on subsequent calls during the
test can be tricky. For example you may have a function that needs to raise
an exception the first time it is called but returns a response on the second
call (testing retry behaviour).
One approach is to use a :attr:`side_effect` function that replaces itself. The
first time it is called the `side_effect` sets a new `side_effect` that will
be used for the second call. It then raises an exception:
>>> def side_effect(*args):
... def second_call(*args):
... return 'response'
... mock.side_effect = second_call
... raise Exception('boom')
...
>>> mock = Mock(side_effect=side_effect)
>>> mock('first')
Traceback (most recent call last):
...
Exception: boom
>>> mock('second')
'response'
>>> mock.assert_called_with('second')
Another perfectly valid way would be to pop return values from a list. If the
return value is an exception, raise it instead of returning it:
>>> returns = [Exception('boom'), 'response']
>>> def side_effect(*args):
... result = returns.pop(0)
... if isinstance(result, Exception):
... raise result
... return result
...
>>> mock = Mock(side_effect=side_effect)
>>> mock('first')
Traceback (most recent call last):
...
Exception: boom
>>> mock('second')
'response'
>>> mock.assert_called_with('second')
Which approach you prefer is a matter of taste. The first approach is actually
a line shorter but maybe the second approach is more readable.
Nesting Patches
---------------
...
...
Doc/library/unittest.mock.rst
Dosyayı görüntüle @
2cd48738
...
...
@@ -823,6 +823,20 @@ a `StopIteration` is raised):
...
StopIteration
If any members of the iterable are exceptions they will be raised instead of
returned::
>>> iterable = (33, ValueError, 66)
>>> m = MagicMock(side_effect=iterable)
>>> m()
33
>>> m()
Traceback (most recent call last):
...
ValueError
>>> m()
66
.. _deleting-attributes:
...
...
Lib/unittest/mock.py
Dosyayı görüntüle @
2cd48738
...
...
@@ -891,7 +891,10 @@ class CallableMixin(Base):
raise
effect
if
not
_callable
(
effect
):
return
next
(
effect
)
result
=
next
(
effect
)
if
_is_exception
(
result
):
raise
result
return
result
ret_val
=
effect
(
*
args
,
**
kwargs
)
if
ret_val
is
DEFAULT
:
...
...
@@ -931,8 +934,9 @@ class Mock(CallableMixin, NonCallableMock):
arguments as the mock, and unless it returns `DEFAULT`, the return
value of this function is used as the return value.
Alternatively `side_effect` can be an exception class or instance. In
this case the exception will be raised when the mock is called.
If `side_effect` is an iterable then each call to the mock will return
the next value from the iterable. If any of the members of the iterable
are exceptions they will be raised instead of returned.
If `side_effect` is an iterable then each call to the mock will return
the next value from the iterable.
...
...
Lib/unittest/test/testmock/testmock.py
Dosyayı görüntüle @
2cd48738
...
...
@@ -868,6 +868,16 @@ class MockTest(unittest.TestCase):
self
.
assertRaises
(
StopIteration
,
mock
)
def
test_side_effect_iterator_exceptions
(
self
):
for
Klass
in
Mock
,
MagicMock
:
iterable
=
(
ValueError
,
3
,
KeyError
,
6
)
m
=
Klass
(
side_effect
=
iterable
)
self
.
assertRaises
(
ValueError
,
m
)
self
.
assertEqual
(
m
(),
3
)
self
.
assertRaises
(
KeyError
,
m
)
self
.
assertEqual
(
m
(),
6
)
def
test_side_effect_setting_iterator
(
self
):
mock
=
Mock
()
mock
.
side_effect
=
iter
([
1
,
2
,
3
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment