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
d6acf17c
Unverified
Kaydet (Commit)
d6acf17c
authored
Ock 09, 2019
tarafından
Pablo Galindo
Kaydeden (comit)
GitHub
Ock 09, 2019
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add example to the documentation for calling unittest.mock.patch with create=True (GH-11056)
üst
ee655943
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
7 deletions
+28
-7
unittest.mock.rst
Doc/library/unittest.mock.rst
+28
-7
No files found.
Doc/library/unittest.mock.rst
Dosyayı görüntüle @
d6acf17c
...
...
@@ -1119,13 +1119,13 @@ patch
Instead of ``autospec=True`` you can pass ``autospec=some_object`` to use an
arbitrary object as the spec instead of the one being replaced.
By default :func:`patch` will fail to replace attributes that don't exist.
If
you pass in ``create=True``, and the attribute doesn't exist, patch will
create the attribute for you when the patched function is called, and
delete it again afterwards. This is useful for writing tests against
attributes that your production code creates at runtime. It is off by
default because it can be dangerous. With it switched on you can write
passing tests against APIs that don't actually exist!
By default :func:`patch` will fail to replace attributes that don't exist.
If
you pass in ``create=True``, and the attribute doesn't exist, patch will
create the attribute for you when the patched function is called, and
delete
it again after the patched function has exited. This is useful for writing
tests against attributes that your production code creates at runtime. It is
off by default because it can be dangerous. With it switched on you can
write
passing tests against APIs that don't actually exist!
.. note::
...
...
@@ -1247,6 +1247,27 @@ into a :func:`patch` call using ``**``::
...
KeyError
By default, attempting to patch a function in a module (or a method or an
attribute in a class) that does not exist will fail with :exc:`AttributeError`::
>>> @patch('sys.non_existing_attribute', 42)
... def test():
... assert sys.non_existing_attribute == 42
...
>>> test()
Traceback (most recent call last):
...
AttributeError: <module 'sys' (built-in)> does not have the attribute 'non_existing'
but adding ``create=True`` in the call to :func:`patch` will make the previous example
work as expected::
>>> @patch('sys.non_existing_attribute', 42, create=True)
... def test(mock_stdout):
... assert sys.non_existing_attribute == 42
...
>>> test()
patch.object
~~~~~~~~~~~~
...
...
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