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
04530812
Kaydet (Commit)
04530812
authored
May 28, 2019
tarafından
Mario Corchero
Kaydeden (comit)
Cheryl Sabella
May 28, 2019
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
bpo-32299: Return patched dict when using patch.dict as a context manager (GH-11062)
üst
eb65e244
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
20 additions
and
1 deletion
+20
-1
unittest.mock.rst
Doc/library/unittest.mock.rst
+10
-1
mock.py
Lib/unittest/mock.py
+1
-0
testpatch.py
Lib/unittest/test/testmock/testpatch.py
+7
-0
2017-12-13-17-49-56.bpo-32299.eqAPWs.rst
...S.d/next/Library/2017-12-13-17-49-56.bpo-32299.eqAPWs.rst
+2
-0
No files found.
Doc/library/unittest.mock.rst
Dosyayı görüntüle @
04530812
...
@@ -1556,15 +1556,24 @@ patch.dict
...
@@ -1556,15 +1556,24 @@ patch.dict
decorator. When used as a class decorator :func:`
patch
.
dict
` honours
decorator. When used as a class decorator :func:`
patch
.
dict
` honours
``patch.TEST_PREFIX`` for choosing which methods to wrap.
``patch.TEST_PREFIX`` for choosing which methods to wrap.
.. versionchanged:: 3.8
:func:`
patch
.
dict
` now returns the patched dictionary when used as a context
manager.
:func:`
patch
.
dict
` can be used to add members to a dictionary, or simply let a test
:func:`
patch
.
dict
` can be used to add members to a dictionary, or simply let a test
change a dictionary, and ensure the dictionary is restored when the test
change a dictionary, and ensure the dictionary is restored when the test
ends.
ends.
>>> foo = {}
>>> foo = {}
>>> with patch.dict(foo, {'newkey': 'newvalue'}):
>>> with patch.dict(foo, {'newkey': 'newvalue'})
as patched_foo
:
... assert foo == {'newkey': 'newvalue'}
... assert foo == {'newkey': 'newvalue'}
... assert patched_foo == {'newkey': 'newvalue'}
... # You can add, update or delete keys of foo (or patched_foo, it's the same dict)
... patched_foo['spam'] = 'eggs'
...
...
>>> assert foo == {}
>>> assert foo == {}
>>> assert patched_foo == {}
>>> import os
>>> import os
>>> with patch.dict('os.environ', {'newkey': 'newvalue'}):
>>> with patch.dict('os.environ', {'newkey': 'newvalue'}):
...
...
Lib/unittest/mock.py
Dosyayı görüntüle @
04530812
...
@@ -1730,6 +1730,7 @@ class _patch_dict(object):
...
@@ -1730,6 +1730,7 @@ class _patch_dict(object):
def
__enter__
(
self
):
def
__enter__
(
self
):
"""Patch the dict."""
"""Patch the dict."""
self
.
_patch_dict
()
self
.
_patch_dict
()
return
self
.
in_dict
def
_patch_dict
(
self
):
def
_patch_dict
(
self
):
...
...
Lib/unittest/test/testmock/testpatch.py
Dosyayı görüntüle @
04530812
...
@@ -619,6 +619,13 @@ class PatchTest(unittest.TestCase):
...
@@ -619,6 +619,13 @@ class PatchTest(unittest.TestCase):
self
.
assertEqual
(
foo
.
values
,
original
)
self
.
assertEqual
(
foo
.
values
,
original
)
def
test_patch_dict_as_context_manager
(
self
):
foo
=
{
'a'
:
'b'
}
with
patch
.
dict
(
foo
,
a
=
'c'
)
as
patched
:
self
.
assertEqual
(
patched
,
{
'a'
:
'c'
})
self
.
assertEqual
(
foo
,
{
'a'
:
'b'
})
def
test_name_preserved
(
self
):
def
test_name_preserved
(
self
):
foo
=
{}
foo
=
{}
...
...
Misc/NEWS.d/next/Library/2017-12-13-17-49-56.bpo-32299.eqAPWs.rst
0 → 100644
Dosyayı görüntüle @
04530812
Changed :func:`unittest.mock.patch.dict` to return the patched
dictionary when used as context manager. Patch by Vadim Tsander.
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