chunk_id
stringlengths
36
36
source
stringclasses
35 values
source_url
stringlengths
0
290
upstream_license
stringclasses
1 value
document_id
stringlengths
36
36
chunk_index
int64
0
324k
retrieved_at
stringclasses
2 values
chunker_version
stringclasses
4 values
content_hash
stringlengths
15
64
content
stringlengths
50
44.7k
namespace
stringclasses
9 values
source_name
stringclasses
35 values
raw_text
stringlengths
50
44.7k
cleaned_text
stringlengths
50
44.7k
tags
stringclasses
49 values
collection_name
stringclasses
11 values
71077796-828b-417d-be26-6a4af002183e
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,515
supabase-export-v2
4928fe96a0883bdd
... return new_mock ... >>> with patch('mymodule.frob') as mock_frob: ... new_mock = copy_call_args(mock_frob) ... val = {6} ... mymodule.grob(val) ... >>> new_mock.assert_called_with({6}) >>> new_mock.call_args call({6}) ``copy_call_args`` is called with the mock that will be called. It returns a new mock that we do t...
trusted_official_docs
CPython Docs
... return new_mock ... >>> with patch('mymodule.frob') as mock_frob: ... new_mock = copy_call_args(mock_frob) ... val = {6} ... mymodule.grob(val) ... >>> new_mock.assert_called_with({6}) >>> new_mock.call_args call({6}) ``copy_call_args`` is called with the mock that will be called. It returns a new mock that we do t...
... return new_mock ... >>> with patch('mymodule.frob') as mock_frob: ... new_mock = copy_call_args(mock_frob) ... val = {6} ... mymodule.grob(val) ... >>> new_mock.assert_called_with({6}) >>> new_mock.call_args call({6}) ``copy_call_args`` is called with the mock that will be called. It returns a new mock that we do t...
python, official-docs, cpython, P0
Local_Trusted_Corpus
715fd2e1-0454-4bb6-8505-06d42b7bbf53
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,523
supabase-export-v2
6d79f9f51a482cb2
Nesting patches ~~~~~~~~~~~~~~~ Using patch as a context manager is nice, but if you do multiple patches you can end up with nested with statements indenting further and further to the right::
trusted_official_docs
CPython Docs
Nesting patches ~~~~~~~~~~~~~~~ Using patch as a context manager is nice, but if you do multiple patches you can end up with nested with statements indenting further and further to the right::
Nesting patches ~~~~~~~~~~~~~~~ Using patch as a context manager is nice, but if you do multiple patches you can end up with nested with statements indenting further and further to the right::
python, official-docs, cpython, P0
Local_Trusted_Corpus
71f304fc-a080-440f-933e-5c9587c16d23
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,536
supabase-export-v2
5d5865571512d29a
>>> mock = Mock() >>> mock.__getitem__ = Mock(side_effect=getitem) >>> mock.__setitem__ = Mock(side_effect=setitem) A *third* option is to use ``MagicMock`` but passing in ``dict`` as the *spec* (or *spec_set*) argument so that the ``MagicMock`` created only has dictionary magic methods available:
trusted_official_docs
CPython Docs
>>> mock = Mock() >>> mock.__getitem__ = Mock(side_effect=getitem) >>> mock.__setitem__ = Mock(side_effect=setitem) A *third* option is to use ``MagicMock`` but passing in ``dict`` as the *spec* (or *spec_set*) argument so that the ``MagicMock`` created only has dictionary magic methods available:
>>> mock = Mock() >>> mock.__getitem__ = Mock(side_effect=getitem) >>> mock.__setitem__ = Mock(side_effect=setitem) A *third* option is to use ``MagicMock`` but passing in ``dict`` as the *spec* (or *spec_set*) argument so that the ``MagicMock`` created only has dictionary magic methods available:
python, official-docs, cpython, P0
Local_Trusted_Corpus
7625c3db-a2eb-4a9c-af34-0c0204c736c6
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,430
supabase-export-v2
f7f47662b485f5ef
If you are patching a module (including :mod:`builtins`) then use :func:`patch` instead of :func:`patch.object`: >>> mock = MagicMock(return_value=sentinel.file_handle) >>> with patch('builtins.open', mock): ... handle = open('filename', 'r') ... >>> mock.assert_called_with('filename', 'r') >>> assert handle == sen...
trusted_official_docs
CPython Docs
If you are patching a module (including :mod:`builtins`) then use :func:`patch` instead of :func:`patch.object`: >>> mock = MagicMock(return_value=sentinel.file_handle) >>> with patch('builtins.open', mock): ... handle = open('filename', 'r') ... >>> mock.assert_called_with('filename', 'r') >>> assert handle == sen...
If you are patching a module (including :mod:`builtins`) then use :func:`patch` instead of :func:`patch.object`: >>> mock = MagicMock(return_value=sentinel.file_handle) >>> with patch('builtins.open', mock): ... handle = open('filename', 'r') ... >>> mock.assert_called_with('filename', 'r') >>> assert handle == sen...
python, official-docs, cpython, P0
Local_Trusted_Corpus
775f33bf-80d3-4f6f-b930-9f9a41990b91
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,423
supabase-export-v2
1459f9774d03e7ce
.. note:: With :func:`patch` it matters that you patch objects in the namespace where they are looked up. This is normally straightforward, but for a quick guide read :ref:`where to patch <where-to-patch>`.
trusted_official_docs
CPython Docs
.. note:: With :func:`patch` it matters that you patch objects in the namespace where they are looked up. This is normally straightforward, but for a quick guide read :ref:`where to patch <where-to-patch>`.
.. note:: With :func:`patch` it matters that you patch objects in the namespace where they are looked up. This is normally straightforward, but for a quick guide read :ref:`where to patch <where-to-patch>`.
python, official-docs, cpython, P0
Local_Trusted_Corpus
781e1b26-de41-4804-b860-5a6f522af66f
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,450
supabase-export-v2
9cfc507cf3553d41
Mocking chained calls ~~~~~~~~~~~~~~~~~~~~~ Mocking chained calls is actually straightforward with mock once you understand the :attr:`~Mock.return_value` attribute. When a mock is called for the first time, or you fetch its ``return_value`` before it has been called, a new :class:`Mock` is created.
trusted_official_docs
CPython Docs
Mocking chained calls ~~~~~~~~~~~~~~~~~~~~~ Mocking chained calls is actually straightforward with mock once you understand the :attr:`~Mock.return_value` attribute. When a mock is called for the first time, or you fetch its ``return_value`` before it has been called, a new :class:`Mock` is created.
Mocking chained calls ~~~~~~~~~~~~~~~~~~~~~ Mocking chained calls is actually straightforward with mock once you understand the :attr:`~Mock.return_value` attribute. When a mock is called for the first time, or you fetch its ``return_value`` before it has been called, a new :class:`Mock` is created.
python, official-docs, cpython, P0
Local_Trusted_Corpus
788a5583-5615-4dfb-ad3f-d72bfdaa3ae1
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,350
supabase-export-v2
10c58ca7976c6884
* Patching methods * Recording method calls on objects You might want to replace a method on an object to check that it is called with the correct arguments by another part of the system:
trusted_official_docs
CPython Docs
* Patching methods * Recording method calls on objects You might want to replace a method on an object to check that it is called with the correct arguments by another part of the system:
* Patching methods * Recording method calls on objects You might want to replace a method on an object to check that it is called with the correct arguments by another part of the system:
python, official-docs, cpython, P0
Local_Trusted_Corpus
7c403e49-e981-475f-8c20-4715bed675f5
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,354
supabase-export-v2
200dfea3ceb59a4c
.. note:: In most of these examples the :class:`Mock` and :class:`MagicMock` classes are interchangeable. As the ``MagicMock`` is the more capable class it makes a sensible one to use by default.
trusted_official_docs
CPython Docs
.. note:: In most of these examples the :class:`Mock` and :class:`MagicMock` classes are interchangeable. As the ``MagicMock`` is the more capable class it makes a sensible one to use by default.
.. note:: In most of these examples the :class:`Mock` and :class:`MagicMock` classes are interchangeable. As the ``MagicMock`` is the more capable class it makes a sensible one to use by default.
python, official-docs, cpython, P0
Local_Trusted_Corpus
7d2c0e06-1b3c-4628-acf7-8adeaeef4c3a
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,546
supabase-export-v2
fee89cf066c0aec0
So if you're subclassing to add helper methods then they'll also be available on the attributes and return value mock of instances of your subclass. >>> mymock.foo <MyMock name='mock.foo' id='...'> >>> mymock.foo.has_been_called() False >>> mymock.foo() <MyMock name='mock.foo()' id='...'> >>> mymock.foo.has_been_...
trusted_official_docs
CPython Docs
So if you're subclassing to add helper methods then they'll also be available on the attributes and return value mock of instances of your subclass. >>> mymock.foo <MyMock name='mock.foo' id='...'> >>> mymock.foo.has_been_called() False >>> mymock.foo() <MyMock name='mock.foo()' id='...'> >>> mymock.foo.has_been_...
So if you're subclassing to add helper methods then they'll also be available on the attributes and return value mock of instances of your subclass. >>> mymock.foo <MyMock name='mock.foo' id='...'> >>> mymock.foo.has_been_called() False >>> mymock.foo() <MyMock name='mock.foo()' id='...'> >>> mymock.foo.has_been_...
python, official-docs, cpython, P0
Local_Trusted_Corpus
7e958e5f-cddb-4104-b300-555cfaae9bd3
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,394
supabase-export-v2
a2843a9ed5c7f10d
Raising exceptions with mocks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A useful attribute is :attr:`~Mock.side_effect`. If you set this to an exception class or instance then the exception will be raised when the mock is called.
trusted_official_docs
CPython Docs
Raising exceptions with mocks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A useful attribute is :attr:`~Mock.side_effect`. If you set this to an exception class or instance then the exception will be raised when the mock is called.
Raising exceptions with mocks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A useful attribute is :attr:`~Mock.side_effect`. If you set this to an exception class or instance then the exception will be raised when the mock is called.
python, official-docs, cpython, P0
Local_Trusted_Corpus
7f3f8276-4cb6-4b2e-8798-d6fd1b80538a
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,482
supabase-export-v2
a8f6c2a1f56520d2
configure the values returned from the iteration (implicit in the call to :class:`list`), we need to configure the object returned by the call to ``foo.iter()``. >>> mock_foo = MagicMock() >>> mock_foo.iter.return_value = iter([1, 2, 3]) >>> list(mock_foo.iter()) [1, 2, 3]
trusted_official_docs
CPython Docs
configure the values returned from the iteration (implicit in the call to :class:`list`), we need to configure the object returned by the call to ``foo.iter()``. >>> mock_foo = MagicMock() >>> mock_foo.iter.return_value = iter([1, 2, 3]) >>> list(mock_foo.iter()) [1, 2, 3]
configure the values returned from the iteration (implicit in the call to :class:`list`), we need to configure the object returned by the call to ``foo.iter()``. >>> mock_foo = MagicMock() >>> mock_foo.iter.return_value = iter([1, 2, 3]) >>> list(mock_foo.iter()) [1, 2, 3]
python, official-docs, cpython, P0
Local_Trusted_Corpus
82412b4f-6c5d-4e74-8a50-b9cb9b92047b
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,457
supabase-export-v2
70a7dd1d69a0ddb1
how do we test ``method()``? Specifically, we want to test that the code section ``# more code`` uses the response object in the correct way. As this chain of calls is made from an instance attribute we can monkey patch the ``backend`` attribute on a ``Something`` instance. In this particular case we are only intereste...
trusted_official_docs
CPython Docs
how do we test ``method()``? Specifically, we want to test that the code section ``# more code`` uses the response object in the correct way. As this chain of calls is made from an instance attribute we can monkey patch the ``backend`` attribute on a ``Something`` instance. In this particular case we are only intereste...
how do we test ``method()``? Specifically, we want to test that the code section ``# more code`` uses the response object in the correct way. As this chain of calls is made from an instance attribute we can monkey patch the ``backend`` attribute on a ``Something`` instance. In this particular case we are only intereste...
python, official-docs, cpython, P0
Local_Trusted_Corpus
83b2af99-818e-48b0-9939-3ca3fc5f8f87
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,553
supabase-export-v2
06dab32c0d7cc5e4
local import inside a function. These are harder to mock because they aren't using an object from the module namespace that we can patch out. Generally local imports are to be avoided. They are sometimes done to prevent circular dependencies, for which there is *usually* a much better way to solve the problem (refactor...
trusted_official_docs
CPython Docs
local import inside a function. These are harder to mock because they aren't using an object from the module namespace that we can patch out. Generally local imports are to be avoided. They are sometimes done to prevent circular dependencies, for which there is *usually* a much better way to solve the problem (refactor...
local import inside a function. These are harder to mock because they aren't using an object from the module namespace that we can patch out. Generally local imports are to be avoided. They are sometimes done to prevent circular dependencies, for which there is *usually* a much better way to solve the problem (refactor...
python, official-docs, cpython, P0
Local_Trusted_Corpus
8458dd92-b25a-4635-89c5-1b14d259057b
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,400
supabase-export-v2
127f5653e5b03bf6
can be a function. The function will be called with the same arguments as the mock. Whatever the function returns is what the call returns: >>> vals = {(1, 2): 1, (2, 3): 2} >>> def side_effect(*args): ... return vals[args] ... >>> mock = MagicMock(side_effect=side_effect) >>> mock(1, 2) 1 >>> mock(2, 3) 2
trusted_official_docs
CPython Docs
can be a function. The function will be called with the same arguments as the mock. Whatever the function returns is what the call returns: >>> vals = {(1, 2): 1, (2, 3): 2} >>> def side_effect(*args): ... return vals[args] ... >>> mock = MagicMock(side_effect=side_effect) >>> mock(1, 2) 1 >>> mock(2, 3) 2
can be a function. The function will be called with the same arguments as the mock. Whatever the function returns is what the call returns: >>> vals = {(1, 2): 1, (2, 3): 2} >>> def side_effect(*args): ... return vals[args] ... >>> mock = MagicMock(side_effect=side_effect) >>> mock(1, 2) 1 >>> mock(2, 3) 2
python, official-docs, cpython, P0
Local_Trusted_Corpus
87c3924c-6c43-479c-b9e8-d3737bc3e6ce
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,495
supabase-export-v2
3cf2e88a0a8ce624
foo(self): ... pass ... >>> with patch.object(Foo, 'foo', autospec=True) as mock_foo: ... mock_foo.return_value = 'foo' ... foo = Foo() ... foo.foo() ... 'foo' >>> mock_foo.assert_called_once_with(foo) If we don't use ``autospec=True`` then the unbound method is patched out with a Mock instance instead, and isn't calle...
trusted_official_docs
CPython Docs
foo(self): ... pass ... >>> with patch.object(Foo, 'foo', autospec=True) as mock_foo: ... mock_foo.return_value = 'foo' ... foo = Foo() ... foo.foo() ... 'foo' >>> mock_foo.assert_called_once_with(foo) If we don't use ``autospec=True`` then the unbound method is patched out with a Mock instance instead, and isn't calle...
foo(self): ... pass ... >>> with patch.object(Foo, 'foo', autospec=True) as mock_foo: ... mock_foo.return_value = 'foo' ... foo = Foo() ... foo.foo() ... 'foo' >>> mock_foo.assert_called_once_with(foo) If we don't use ``autospec=True`` then the unbound method is patched out with a Mock instance instead, and isn't calle...
python, official-docs, cpython, P0
Local_Trusted_Corpus
884b6569-b0a8-499f-a9c7-4a5ae2c79161
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,395
supabase-export-v2
87928f889c8eadb2
useful attribute is :attr:`~Mock.side_effect`. If you set this to an exception class or instance then the exception will be raised when the mock is called. >>> mock = Mock(side_effect=Exception('Boom!')) >>> mock() Traceback (most recent call last): ... Exception: Boom!
trusted_official_docs
CPython Docs
useful attribute is :attr:`~Mock.side_effect`. If you set this to an exception class or instance then the exception will be raised when the mock is called. >>> mock = Mock(side_effect=Exception('Boom!')) >>> mock() Traceback (most recent call last): ... Exception: Boom!
useful attribute is :attr:`~Mock.side_effect`. If you set this to an exception class or instance then the exception will be raised when the mock is called. >>> mock = Mock(side_effect=Exception('Boom!')) >>> mock() Traceback (most recent call last): ... Exception: Boom!
python, official-docs, cpython, P0
Local_Trusted_Corpus
89b13254-765f-4e17-a052-219f24a0b068
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,431
supabase-export-v2
81fb54e360668638
mock = MagicMock(return_value=sentinel.file_handle) >>> with patch('builtins.open', mock): ... handle = open('filename', 'r') ... >>> mock.assert_called_with('filename', 'r') >>> assert handle == sentinel.file_handle, "incorrect file handle returned" The module name can be 'dotted', in the form ``package.module`` if ne...
trusted_official_docs
CPython Docs
mock = MagicMock(return_value=sentinel.file_handle) >>> with patch('builtins.open', mock): ... handle = open('filename', 'r') ... >>> mock.assert_called_with('filename', 'r') >>> assert handle == sentinel.file_handle, "incorrect file handle returned" The module name can be 'dotted', in the form ``package.module`` if ne...
mock = MagicMock(return_value=sentinel.file_handle) >>> with patch('builtins.open', mock): ... handle = open('filename', 'r') ... >>> mock.assert_called_with('filename', 'r') >>> assert handle == sentinel.file_handle, "incorrect file handle returned" The module name can be 'dotted', in the form ``package.module`` if ne...
python, official-docs, cpython, P0
Local_Trusted_Corpus
89e640de-873c-44ff-833b-b79de64b5635
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,392
supabase-export-v2
e21a1e456d46a91f
= mock.connection.cursor.return_value >>> cursor.execute.return_value = ['foo'] >>> mock.connection.cursor().execute("SELECT 1") ['foo'] >>> expected = call.connection.cursor().execute("SELECT 1").call_list() >>> mock.mock_calls [call.connection.cursor(), call.connection.cursor().execute('SELECT 1')] >>> mock.mock_call...
trusted_official_docs
CPython Docs
= mock.connection.cursor.return_value >>> cursor.execute.return_value = ['foo'] >>> mock.connection.cursor().execute("SELECT 1") ['foo'] >>> expected = call.connection.cursor().execute("SELECT 1").call_list() >>> mock.mock_calls [call.connection.cursor(), call.connection.cursor().execute('SELECT 1')] >>> mock.mock_call...
= mock.connection.cursor.return_value >>> cursor.execute.return_value = ['foo'] >>> mock.connection.cursor().execute("SELECT 1") ['foo'] >>> expected = call.connection.cursor().execute("SELECT 1").call_list() >>> mock.mock_calls [call.connection.cursor(), call.connection.cursor().execute('SELECT 1')] >>> mock.mock_call...
python, official-docs, cpython, P0
Local_Trusted_Corpus
8ce392ab-d267-4cf3-b564-36515a783de6
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,517
supabase-export-v2
d4354e58affb12c3
.. note:: If your mock is only going to be used once there is an easier way of checking arguments at the point they are called. You can simply do the checking inside a ``side_effect`` function.
trusted_official_docs
CPython Docs
.. note:: If your mock is only going to be used once there is an easier way of checking arguments at the point they are called. You can simply do the checking inside a ``side_effect`` function.
.. note:: If your mock is only going to be used once there is an easier way of checking arguments at the point they are called. You can simply do the checking inside a ``side_effect`` function.
python, official-docs, cpython, P0
Local_Trusted_Corpus
8cf3fdae-50d5-4351-a7d8-2a7c4091b7ae
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,479
supabase-export-v2
30eeeffbddcdb75f
Here's an example class with an "iter" method implemented as a generator: >>> class Foo: ... def iter(self): ... for i in [1, 2, 3]: ... yield i ... >>> foo = Foo() >>> list(foo.iter()) [1, 2, 3]
trusted_official_docs
CPython Docs
Here's an example class with an "iter" method implemented as a generator: >>> class Foo: ... def iter(self): ... for i in [1, 2, 3]: ... yield i ... >>> foo = Foo() >>> list(foo.iter()) [1, 2, 3]
Here's an example class with an "iter" method implemented as a generator: >>> class Foo: ... def iter(self): ... for i in [1, 2, 3]: ... yield i ... >>> foo = Foo() >>> list(foo.iter()) [1, 2, 3]
python, official-docs, cpython, P0
Local_Trusted_Corpus
8f2d34bf-880e-4b5d-bb84-47669c045a9d
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,585
supabase-export-v2
ab233b2bd2ea18e5
== type(other): ... return False ... if self.a != other.a: ... return False ... if self.b != other.b: ... return False ... return True ... And a matcher object that can use comparison functions like this for its equality operation would look something like this:
trusted_official_docs
CPython Docs
== type(other): ... return False ... if self.a != other.a: ... return False ... if self.b != other.b: ... return False ... return True ... And a matcher object that can use comparison functions like this for its equality operation would look something like this:
== type(other): ... return False ... if self.a != other.a: ... return False ... if self.b != other.b: ... return False ... return True ... And a matcher object that can use comparison functions like this for its equality operation would look something like this:
python, official-docs, cpython, P0
Local_Trusted_Corpus
92194627-d97d-4ee4-be98-19780186a676
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,537
supabase-export-v2
cb4112c91c3c62b4
is to use ``MagicMock`` but passing in ``dict`` as the *spec* (or *spec_set*) argument so that the ``MagicMock`` created only has dictionary magic methods available: >>> mock = MagicMock(spec_set=dict) >>> mock.__getitem__.side_effect = getitem >>> mock.__setitem__.side_effect = setitem
trusted_official_docs
CPython Docs
is to use ``MagicMock`` but passing in ``dict`` as the *spec* (or *spec_set*) argument so that the ``MagicMock`` created only has dictionary magic methods available: >>> mock = MagicMock(spec_set=dict) >>> mock.__getitem__.side_effect = getitem >>> mock.__setitem__.side_effect = setitem
is to use ``MagicMock`` but passing in ``dict`` as the *spec* (or *spec_set*) argument so that the ``MagicMock`` created only has dictionary magic methods available: >>> mock = MagicMock(spec_set=dict) >>> mock.__getitem__.side_effect = getitem >>> mock.__setitem__.side_effect = setitem
python, official-docs, cpython, P0
Local_Trusted_Corpus
92a469b4-1c3a-4926-9c0a-b4a13a397a7f
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,535
supabase-export-v2
32b26feeff987cb3
An alternative to using ``MagicMock`` is to use ``Mock`` and *only* provide the magic methods you specifically want: >>> mock = Mock() >>> mock.__getitem__ = Mock(side_effect=getitem) >>> mock.__setitem__ = Mock(side_effect=setitem)
trusted_official_docs
CPython Docs
An alternative to using ``MagicMock`` is to use ``Mock`` and *only* provide the magic methods you specifically want: >>> mock = Mock() >>> mock.__getitem__ = Mock(side_effect=getitem) >>> mock.__setitem__ = Mock(side_effect=setitem)
An alternative to using ``MagicMock`` is to use ``Mock`` and *only* provide the magic methods you specifically want: >>> mock = Mock() >>> mock.__getitem__ = Mock(side_effect=getitem) >>> mock.__setitem__ = Mock(side_effect=setitem)
python, official-docs, cpython, P0
Local_Trusted_Corpus
9472b332-f9eb-43d9-8fee-969b468fe009
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,402
supabase-export-v2
d8fc75e119d0ee1d
Mocking asynchronous iterators ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Since Python 3.8, ``AsyncMock`` and ``MagicMock`` have support to mock :ref:`async-iterators` through ``__aiter__``. The :attr:`~Mock.return_value` attribute of ``__aiter__`` can be used to set the return values to be used for iteration.
trusted_official_docs
CPython Docs
Mocking asynchronous iterators ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Since Python 3.8, ``AsyncMock`` and ``MagicMock`` have support to mock :ref:`async-iterators` through ``__aiter__``. The :attr:`~Mock.return_value` attribute of ``__aiter__`` can be used to set the return values to be used for iteration.
Mocking asynchronous iterators ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Since Python 3.8, ``AsyncMock`` and ``MagicMock`` have support to mock :ref:`async-iterators` through ``__aiter__``. The :attr:`~Mock.return_value` attribute of ``__aiter__`` can be used to set the return values to be used for iteration.
python, official-docs, cpython, P0
Local_Trusted_Corpus
9523dc67-ebbb-45a2-a0e7-4d26802086a9
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,441
supabase-export-v2
4ad08d2f794feef9
is also :func:`patch.dict` for setting values in a dictionary just during a scope and restoring the dictionary to its original state when the test ends: >>> foo = {'key': 'value'} >>> original = foo.copy() >>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True): ... assert foo == {'newkey': 'newvalue'} ... >>>...
trusted_official_docs
CPython Docs
is also :func:`patch.dict` for setting values in a dictionary just during a scope and restoring the dictionary to its original state when the test ends: >>> foo = {'key': 'value'} >>> original = foo.copy() >>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True): ... assert foo == {'newkey': 'newvalue'} ... >>>...
is also :func:`patch.dict` for setting values in a dictionary just during a scope and restoring the dictionary to its original state when the test ends: >>> foo = {'key': 'value'} >>> original = foo.copy() >>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True): ... assert foo == {'newkey': 'newvalue'} ... >>>...
python, official-docs, cpython, P0
Local_Trusted_Corpus
95b41df6-59ec-4924-9e3c-1b2cb3afec89
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,404
supabase-export-v2
a828a0f31a663301
here >>> mock.__aiter__.return_value = [1, 2, 3] >>> async def main(): ... return [i async for i in mock] ... >>> asyncio.run(main()) [1, 2, 3] Mocking asynchronous context manager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
trusted_official_docs
CPython Docs
here >>> mock.__aiter__.return_value = [1, 2, 3] >>> async def main(): ... return [i async for i in mock] ... >>> asyncio.run(main()) [1, 2, 3] Mocking asynchronous context manager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
here >>> mock.__aiter__.return_value = [1, 2, 3] >>> async def main(): ... return [i async for i in mock] ... >>> asyncio.run(main()) [1, 2, 3] Mocking asynchronous context manager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
python, official-docs, cpython, P0
Local_Trusted_Corpus
97440128-5b7e-473e-b09f-8f8ee5f32550
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,357
supabase-export-v2
e54bb5acb4ddb801
This example tests that calling ``ProductionClass().method`` results in a call to the ``something`` method: >>> class ProductionClass: ... def method(self): ... self.something(1, 2, 3) ... def something(self, a, b, c): ... pass ... >>> real = ProductionClass() >>> real.something = MagicMock() >>> real.method() ...
trusted_official_docs
CPython Docs
This example tests that calling ``ProductionClass().method`` results in a call to the ``something`` method: >>> class ProductionClass: ... def method(self): ... self.something(1, 2, 3) ... def something(self, a, b, c): ... pass ... >>> real = ProductionClass() >>> real.something = MagicMock() >>> real.method() ...
This example tests that calling ``ProductionClass().method`` results in a call to the ``something`` method: >>> class ProductionClass: ... def method(self): ... self.something(1, 2, 3) ... def something(self, a, b, c): ... pass ... >>> real = ProductionClass() >>> real.something = MagicMock() >>> real.method() ...
python, official-docs, cpython, P0
Local_Trusted_Corpus
98f43ae2-670a-40a8-bf11-266a333cfb3c
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,352
supabase-export-v2
ce980f4dc87efea5
>>> real = SomeClass() >>> real.method = MagicMock(name='method') >>> real.method(3, 4, 5, key='value') <MagicMock name='method()' id='...'> Once our mock has been used (``real.method`` in this example) it has methods and attributes that allow you to make assertions about how it has been used.
trusted_official_docs
CPython Docs
>>> real = SomeClass() >>> real.method = MagicMock(name='method') >>> real.method(3, 4, 5, key='value') <MagicMock name='method()' id='...'> Once our mock has been used (``real.method`` in this example) it has methods and attributes that allow you to make assertions about how it has been used.
>>> real = SomeClass() >>> real.method = MagicMock(name='method') >>> real.method(3, 4, 5, key='value') <MagicMock name='method()' id='...'> Once our mock has been used (``real.method`` in this example) it has methods and attributes that allow you to make assertions about how it has been used.
python, official-docs, cpython, P0
Local_Trusted_Corpus
99e830b2-45e1-47a7-98cb-4cdcdadcc878
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,540
supabase-export-v2
e2115bc44ec707ae
3 >>> mock['d'] Traceback (most recent call last): ... KeyError: 'd' >>> mock['b'] = 'fish' >>> mock['d'] = 'eggs' >>> mock['b'] 'fish' >>> mock['d'] 'eggs' After it has been used you can make assertions about the access using the normal mock methods and attributes:
trusted_official_docs
CPython Docs
3 >>> mock['d'] Traceback (most recent call last): ... KeyError: 'd' >>> mock['b'] = 'fish' >>> mock['d'] = 'eggs' >>> mock['b'] 'fish' >>> mock['d'] 'eggs' After it has been used you can make assertions about the access using the normal mock methods and attributes:
3 >>> mock['d'] Traceback (most recent call last): ... KeyError: 'd' >>> mock['b'] = 'fish' >>> mock['d'] = 'eggs' >>> mock['b'] 'fish' >>> mock['d'] 'eggs' After it has been used you can make assertions about the access using the normal mock methods and attributes:
python, official-docs, cpython, P0
Local_Trusted_Corpus
9a7b0413-5c58-4811-be19-0fef3aabe084
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,538
supabase-export-v2
cf8a7f06d2981e04
>>> mock = MagicMock(spec_set=dict) >>> mock.__getitem__.side_effect = getitem >>> mock.__setitem__.side_effect = setitem With these side effect functions in place, the ``mock`` will behave like a normal dictionary but recording the access. It even raises a :exc:`KeyError` if you try to access a key that doesn't exist.
trusted_official_docs
CPython Docs
>>> mock = MagicMock(spec_set=dict) >>> mock.__getitem__.side_effect = getitem >>> mock.__setitem__.side_effect = setitem With these side effect functions in place, the ``mock`` will behave like a normal dictionary but recording the access. It even raises a :exc:`KeyError` if you try to access a key that doesn't exist.
>>> mock = MagicMock(spec_set=dict) >>> mock.__getitem__.side_effect = getitem >>> mock.__setitem__.side_effect = setitem With these side effect functions in place, the ``mock`` will behave like a normal dictionary but recording the access. It even raises a :exc:`KeyError` if you try to access a key that doesn't exist.
python, official-docs, cpython, P0
Local_Trusted_Corpus
9ba7e204-4272-4348-9341-5714f92babaa
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,555
supabase-export-v2
7891c98eb0bf8611
a module object being put in ``sys.modules``, so usually when you import something you get a module back. This need not be the case however. This means you can use :func:`patch.dict` to *temporarily* put a mock in place in :data:`sys.modules`. Any imports whilst this patch is active will fetch the mock. When the patch ...
trusted_official_docs
CPython Docs
a module object being put in ``sys.modules``, so usually when you import something you get a module back. This need not be the case however. This means you can use :func:`patch.dict` to *temporarily* put a mock in place in :data:`sys.modules`. Any imports whilst this patch is active will fetch the mock. When the patch ...
a module object being put in ``sys.modules``, so usually when you import something you get a module back. This need not be the case however. This means you can use :func:`patch.dict` to *temporarily* put a mock in place in :data:`sys.modules`. Any imports whilst this patch is active will fetch the mock. When the patch ...
python, official-docs, cpython, P0
Local_Trusted_Corpus
9ca5d407-ee15-4919-a629-f48abc2fb5e7
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,435
supabase-export-v2
1aac8f2b7f3f494b
>>> class MyTest(unittest.TestCase): ... @patch.object(SomeClass, 'attribute', sentinel.attribute) ... def test_something(self): ... self.assertEqual(SomeClass.attribute, sentinel.attribute) ... >>> original = SomeClass.attribute >>> MyTest('test_something').test_something() >>> assert SomeClass.attribute == original I...
trusted_official_docs
CPython Docs
>>> class MyTest(unittest.TestCase): ... @patch.object(SomeClass, 'attribute', sentinel.attribute) ... def test_something(self): ... self.assertEqual(SomeClass.attribute, sentinel.attribute) ... >>> original = SomeClass.attribute >>> MyTest('test_something').test_something() >>> assert SomeClass.attribute == original I...
>>> class MyTest(unittest.TestCase): ... @patch.object(SomeClass, 'attribute', sentinel.attribute) ... def test_something(self): ... self.assertEqual(SomeClass.attribute, sentinel.attribute) ... >>> original = SomeClass.attribute >>> MyTest('test_something').test_something() >>> assert SomeClass.attribute == original I...
python, official-docs, cpython, P0
Local_Trusted_Corpus
9d8dfe39-6ddb-48ce-b429-df8a064f4663
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,556
supabase-export-v2
be71bd09de1d45ba
is complete (the decorated function exits, the with statement body is complete or ``patcher.stop()`` is called) then whatever was there previously will be restored safely. Here's an example that mocks out the 'fooble' module.
trusted_official_docs
CPython Docs
is complete (the decorated function exits, the with statement body is complete or ``patcher.stop()`` is called) then whatever was there previously will be restored safely. Here's an example that mocks out the 'fooble' module.
is complete (the decorated function exits, the with statement body is complete or ``patcher.stop()`` is called) then whatever was there previously will be restored safely. Here's an example that mocks out the 'fooble' module.
python, official-docs, cpython, P0
Local_Trusted_Corpus
9e2802a2-7132-4b72-bc00-4dcbc8732421
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,379
supabase-export-v2
1f9e060d05921769
that return mocks are not recorded, which means it is not possible to track nested calls where the parameters used to create ancestors are important: >>> m = Mock() >>> m.factory(important=True).deliver() <Mock name='mock.factory().deliver()' id='...'> >>> m.mock_calls[-1] == call.factory(important=False).deliver() ...
trusted_official_docs
CPython Docs
that return mocks are not recorded, which means it is not possible to track nested calls where the parameters used to create ancestors are important: >>> m = Mock() >>> m.factory(important=True).deliver() <Mock name='mock.factory().deliver()' id='...'> >>> m.mock_calls[-1] == call.factory(important=False).deliver() ...
that return mocks are not recorded, which means it is not possible to track nested calls where the parameters used to create ancestors are important: >>> m = Mock() >>> m.factory(important=True).deliver() <Mock name='mock.factory().deliver()' id='...'> >>> m.mock_calls[-1] == call.factory(important=False).deliver() ...
python, official-docs, cpython, P0
Local_Trusted_Corpus
a1982cbe-cd23-433d-9859-a5ceea531bb0
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,417
supabase-export-v2
f299061c7fa352c1
can be used to return a new Mock object per call. This can be used to return different contents per file stored in a dictionary:: DEFAULT = "default" data_dict = {"file1": "data1", "file2": "data2"}
trusted_official_docs
CPython Docs
can be used to return a new Mock object per call. This can be used to return different contents per file stored in a dictionary:: DEFAULT = "default" data_dict = {"file1": "data1", "file2": "data2"}
can be used to return a new Mock object per call. This can be used to return different contents per file stored in a dictionary:: DEFAULT = "default" data_dict = {"file1": "data1", "file2": "data2"}
python, official-docs, cpython, P0
Local_Trusted_Corpus
a20e059a-e989-4925-956c-7805e92b9b36
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,362
supabase-export-v2
743ffd3387df81bf
>>> class ProductionClass: ... def closer(self, something): ... something.close() ... So to test it we need to pass in an object with a ``close`` method and check that it was called correctly.
trusted_official_docs
CPython Docs
>>> class ProductionClass: ... def closer(self, something): ... something.close() ... So to test it we need to pass in an object with a ``close`` method and check that it was called correctly.
>>> class ProductionClass: ... def closer(self, something): ... something.close() ... So to test it we need to pass in an object with a ``close`` method and check that it was called correctly.
python, official-docs, cpython, P0
Local_Trusted_Corpus
a3008e6f-2d24-4ee2-a59b-fb6a5637cbb7
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,529
supabase-export-v2
ea251165ba6377ce
You may want to mock a dictionary, or other container object, recording all access to it whilst having it still behave like a dictionary. We can do this with :class:`MagicMock`, which will behave like a dictionary, and using :data:`~Mock.side_effect` to delegate dictionary access to a real underlying dictionary that is...
trusted_official_docs
CPython Docs
You may want to mock a dictionary, or other container object, recording all access to it whilst having it still behave like a dictionary. We can do this with :class:`MagicMock`, which will behave like a dictionary, and using :data:`~Mock.side_effect` to delegate dictionary access to a real underlying dictionary that is...
You may want to mock a dictionary, or other container object, recording all access to it whilst having it still behave like a dictionary. We can do this with :class:`MagicMock`, which will behave like a dictionary, and using :data:`~Mock.side_effect` to delegate dictionary access to a real underlying dictionary that is...
python, official-docs, cpython, P0
Local_Trusted_Corpus
a3baf0a8-2322-4c53-9b44-dc36c0160db9
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,549
supabase-export-v2
53839f6aaf9e24b4
used for attributes by overriding this method. The signature is that it takes arbitrary keyword arguments (``**kwargs``) which are then passed onto the mock constructor: >>> class Subclass(MagicMock): ... def _get_child_mock(self, /, **kwargs): ... return MagicMock(**kwargs) ... >>> mymock = Subclass() >>> mymock.f...
trusted_official_docs
CPython Docs
used for attributes by overriding this method. The signature is that it takes arbitrary keyword arguments (``**kwargs``) which are then passed onto the mock constructor: >>> class Subclass(MagicMock): ... def _get_child_mock(self, /, **kwargs): ... return MagicMock(**kwargs) ... >>> mymock = Subclass() >>> mymock.f...
used for attributes by overriding this method. The signature is that it takes arbitrary keyword arguments (``**kwargs``) which are then passed onto the mock constructor: >>> class Subclass(MagicMock): ... def _get_child_mock(self, /, **kwargs): ... return MagicMock(**kwargs) ... >>> mymock = Subclass() >>> mymock.f...
python, official-docs, cpython, P0
Local_Trusted_Corpus
a4068048-fc97-44c0-bd4b-b41944a9e810
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,558
supabase-export-v2
77c06a5109961700
mock = Mock() >>> with patch.dict('sys.modules', {'fooble': mock}): ... import fooble ... fooble.blob() ... <Mock name='mock.blob()' id='...'> >>> assert 'fooble' not in sys.modules >>> mock.blob.assert_called_once_with() As you can see the ``import fooble`` succeeds, but on exit there is no 'fooble' left in :data:`sys...
trusted_official_docs
CPython Docs
mock = Mock() >>> with patch.dict('sys.modules', {'fooble': mock}): ... import fooble ... fooble.blob() ... <Mock name='mock.blob()' id='...'> >>> assert 'fooble' not in sys.modules >>> mock.blob.assert_called_once_with() As you can see the ``import fooble`` succeeds, but on exit there is no 'fooble' left in :data:`sys...
mock = Mock() >>> with patch.dict('sys.modules', {'fooble': mock}): ... import fooble ... fooble.blob() ... <Mock name='mock.blob()' id='...'> >>> assert 'fooble' not in sys.modules >>> mock.blob.assert_called_once_with() As you can see the ``import fooble`` succeeds, but on exit there is no 'fooble' left in :data:`sys...
python, official-docs, cpython, P0
Local_Trusted_Corpus
a514ce1d-85d4-47fa-8b53-474e47664892
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,411
supabase-export-v2
9d60ffe61cec8494
>>> mock = Mock(spec=SomeClass) >>> mock.old_method() Traceback (most recent call last): ... AttributeError: Mock object has no attribute 'old_method'. Did you mean: 'class_method'? Using a specification also enables a smarter matching of calls made to the mock, regardless of whether some parameters were passed as posi...
trusted_official_docs
CPython Docs
>>> mock = Mock(spec=SomeClass) >>> mock.old_method() Traceback (most recent call last): ... AttributeError: Mock object has no attribute 'old_method'. Did you mean: 'class_method'? Using a specification also enables a smarter matching of calls made to the mock, regardless of whether some parameters were passed as posi...
>>> mock = Mock(spec=SomeClass) >>> mock.old_method() Traceback (most recent call last): ... AttributeError: Mock object has no attribute 'old_method'. Did you mean: 'class_method'? Using a specification also enables a smarter matching of calls made to the mock, regardless of whether some parameters were passed as posi...
python, official-docs, cpython, P0
Local_Trusted_Corpus
a582a095-cf20-49fc-b24a-df8e17b08091
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,432
supabase-export-v2
fa969702e56e5189
The module name can be 'dotted', in the form ``package.module`` if needed:: >>> @patch('package.module.ClassName.attribute', sentinel.attribute) ... def test(): ... from package.module import ClassName ... assert ClassName.attribute == sentinel.attribute ... >>> test()
trusted_official_docs
CPython Docs
The module name can be 'dotted', in the form ``package.module`` if needed:: >>> @patch('package.module.ClassName.attribute', sentinel.attribute) ... def test(): ... from package.module import ClassName ... assert ClassName.attribute == sentinel.attribute ... >>> test()
The module name can be 'dotted', in the form ``package.module`` if needed:: >>> @patch('package.module.ClassName.attribute', sentinel.attribute) ... def test(): ... from package.module import ClassName ... assert ClassName.attribute == sentinel.attribute ... >>> test()
python, official-docs, cpython, P0
Local_Trusted_Corpus
a62910f7-1033-44ec-baa6-4b051929d361
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,398
supabase-export-v2
d42d35b0d04bc2fc
call to return a different value. When you set ``side_effect`` to an iterable every call to the mock returns the next value from the iterable: >>> mock = MagicMock(side_effect=[4, 5, 6]) >>> mock() 4 >>> mock() 5 >>> mock() 6
trusted_official_docs
CPython Docs
call to return a different value. When you set ``side_effect`` to an iterable every call to the mock returns the next value from the iterable: >>> mock = MagicMock(side_effect=[4, 5, 6]) >>> mock() 4 >>> mock() 5 >>> mock() 6
call to return a different value. When you set ``side_effect`` to an iterable every call to the mock returns the next value from the iterable: >>> mock = MagicMock(side_effect=[4, 5, 6]) >>> mock() 4 >>> mock() 5 >>> mock() 6
python, official-docs, cpython, P0
Local_Trusted_Corpus
a84a8eba-58a0-4e5f-8e7a-3cd5db7f38fe
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,361
supabase-export-v2
ba1ef8b32a4be60d
The simple ``ProductionClass`` below has a ``closer`` method. If it is called with an object then it calls ``close`` on it. >>> class ProductionClass: ... def closer(self, something): ... something.close() ...
trusted_official_docs
CPython Docs
The simple ``ProductionClass`` below has a ``closer`` method. If it is called with an object then it calls ``close`` on it. >>> class ProductionClass: ... def closer(self, something): ... something.close() ...
The simple ``ProductionClass`` below has a ``closer`` method. If it is called with an object then it calls ``close`` on it. >>> class ProductionClass: ... def closer(self, something): ... something.close() ...
python, official-docs, cpython, P0
Local_Trusted_Corpus
ab67e083-9b98-429b-b799-5f042cb0730d
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,358
supabase-export-v2
92e73f3abd57b430
2, 3) ... def something(self, a, b, c): ... pass ... >>> real = ProductionClass() >>> real.something = MagicMock() >>> real.method() >>> real.something.assert_called_once_with(1, 2, 3) Mock for method calls on an object ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
trusted_official_docs
CPython Docs
2, 3) ... def something(self, a, b, c): ... pass ... >>> real = ProductionClass() >>> real.something = MagicMock() >>> real.method() >>> real.something.assert_called_once_with(1, 2, 3) Mock for method calls on an object ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2, 3) ... def something(self, a, b, c): ... pass ... >>> real = ProductionClass() >>> real.something = MagicMock() >>> real.method() >>> real.something.assert_called_once_with(1, 2, 3) Mock for method calls on an object ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
python, official-docs, cpython, P0
Local_Trusted_Corpus
ade1d38f-0b80-42d0-bed2-270bfebfc14e
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,351
supabase-export-v2
36be85b48371f0dd
might want to replace a method on an object to check that it is called with the correct arguments by another part of the system: >>> real = SomeClass() >>> real.method = MagicMock(name='method') >>> real.method(3, 4, 5, key='value') <MagicMock name='method()' id='...'>
trusted_official_docs
CPython Docs
might want to replace a method on an object to check that it is called with the correct arguments by another part of the system: >>> real = SomeClass() >>> real.method = MagicMock(name='method') >>> real.method(3, 4, 5, key='value') <MagicMock name='method()' id='...'>
might want to replace a method on an object to check that it is called with the correct arguments by another part of the system: >>> real = SomeClass() >>> real.method = MagicMock(name='method') >>> real.method(3, 4, 5, key='value') <MagicMock name='method()' id='...'>
python, official-docs, cpython, P0
Local_Trusted_Corpus
afcc7f6c-98fb-46fd-a852-bf7b9f9d6453
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,570
supabase-export-v2
a74cee67fb6bc8af
We can then assert about the calls, including the order, by comparing with the ``mock_calls`` attribute on the manager mock: >>> expected_calls = [call.foo.something(), call.bar.other.thing()] >>> manager.mock_calls == expected_calls True
trusted_official_docs
CPython Docs
We can then assert about the calls, including the order, by comparing with the ``mock_calls`` attribute on the manager mock: >>> expected_calls = [call.foo.something(), call.bar.other.thing()] >>> manager.mock_calls == expected_calls True
We can then assert about the calls, including the order, by comparing with the ``mock_calls`` attribute on the manager mock: >>> expected_calls = [call.foo.something(), call.bar.other.thing()] >>> manager.mock_calls == expected_calls True
python, official-docs, cpython, P0
Local_Trusted_Corpus
b2cf5f77-df23-4e5e-9b87-ed207064c56d
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,409
supabase-export-v2
27760221112bf6f3
the first class, so that it no longer has ``some_method`` - then your tests will continue to pass even though your code is now broken! :class:`Mock` allows you to provide an object as a specification for the mock, using the *spec* keyword argument. Accessing methods / attributes on the mock that don't exist on your spe...
trusted_official_docs
CPython Docs
the first class, so that it no longer has ``some_method`` - then your tests will continue to pass even though your code is now broken! :class:`Mock` allows you to provide an object as a specification for the mock, using the *spec* keyword argument. Accessing methods / attributes on the mock that don't exist on your spe...
the first class, so that it no longer has ``some_method`` - then your tests will continue to pass even though your code is now broken! :class:`Mock` allows you to provide an object as a specification for the mock, using the *spec* keyword argument. Accessing methods / attributes on the mock that don't exist on your spe...
python, official-docs, cpython, P0
Local_Trusted_Corpus
b4635881-6504-4a39-b81d-d3362a977231
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,580
supabase-export-v2
ba1e6c67898a3e84
Using the same basic concept as :data:`ANY` we can implement matchers to do more complex assertions on objects used as arguments to mocks. Suppose we expect some object to be passed to a mock that by default compares equal based on object identity (which is the Python default for user defined classes). To use :meth:`~M...
trusted_official_docs
CPython Docs
Using the same basic concept as :data:`ANY` we can implement matchers to do more complex assertions on objects used as arguments to mocks. Suppose we expect some object to be passed to a mock that by default compares equal based on object identity (which is the Python default for user defined classes). To use :meth:`~M...
Using the same basic concept as :data:`ANY` we can implement matchers to do more complex assertions on objects used as arguments to mocks. Suppose we expect some object to be passed to a mock that by default compares equal based on object identity (which is the Python default for user defined classes). To use :meth:`~M...
python, official-docs, cpython, P0
Local_Trusted_Corpus
b5d29ab6-5fbc-43d2-827c-6f156a5d7ead
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,428
supabase-export-v2
419a8680c605ef88
>>> original = SomeClass.attribute >>> @patch.object(SomeClass, 'attribute', sentinel.attribute) ... def test(): ... assert SomeClass.attribute == sentinel.attribute ... >>> test() >>> assert SomeClass.attribute == original >>> @patch('package.module.attribute', sentinel.attribute) ... def test(): ... from package.mo...
trusted_official_docs
CPython Docs
>>> original = SomeClass.attribute >>> @patch.object(SomeClass, 'attribute', sentinel.attribute) ... def test(): ... assert SomeClass.attribute == sentinel.attribute ... >>> test() >>> assert SomeClass.attribute == original >>> @patch('package.module.attribute', sentinel.attribute) ... def test(): ... from package.mo...
>>> original = SomeClass.attribute >>> @patch.object(SomeClass, 'attribute', sentinel.attribute) ... def test(): ... assert SomeClass.attribute == sentinel.attribute ... >>> test() >>> assert SomeClass.attribute == original >>> @patch('package.module.attribute', sentinel.attribute) ... def test(): ... from package.mo...
python, official-docs, cpython, P0
Local_Trusted_Corpus
b77aa94a-2410-43c4-bb0f-de58c320aa38
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,532
supabase-export-v2
8d53ffc2f63b2f37
After the ``MagicMock`` has been used we can use attributes like :data:`~Mock.call_args_list` to assert about how the dictionary was used: >>> my_dict = {'a': 1, 'b': 2, 'c': 3} >>> def getitem(name): ... return my_dict[name] ... >>> def setitem(name, val): ... my_dict[name] = val ... >>> mock = MagicMock() >>> m...
trusted_official_docs
CPython Docs
After the ``MagicMock`` has been used we can use attributes like :data:`~Mock.call_args_list` to assert about how the dictionary was used: >>> my_dict = {'a': 1, 'b': 2, 'c': 3} >>> def getitem(name): ... return my_dict[name] ... >>> def setitem(name, val): ... my_dict[name] = val ... >>> mock = MagicMock() >>> m...
After the ``MagicMock`` has been used we can use attributes like :data:`~Mock.call_args_list` to assert about how the dictionary was used: >>> my_dict = {'a': 1, 'b': 2, 'c': 3} >>> def getitem(name): ... return my_dict[name] ... >>> def setitem(name, val): ... my_dict[name] = val ... >>> mock = MagicMock() >>> m...
python, official-docs, cpython, P0
Local_Trusted_Corpus
b7ab747f-fdee-4aa2-a245-cea620415588
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,493
supabase-export-v2
65a290e4a3a706b9
instead. The :func:`patch` decorator makes it so simple to patch out methods with a mock that having to create a real function becomes a nuisance. If you pass ``autospec=True`` to patch then it does the patching with a *real* function object. This function object has the same signature as the one it is replacing, but d...
trusted_official_docs
CPython Docs
instead. The :func:`patch` decorator makes it so simple to patch out methods with a mock that having to create a real function becomes a nuisance. If you pass ``autospec=True`` to patch then it does the patching with a *real* function object. This function object has the same signature as the one it is replacing, but d...
instead. The :func:`patch` decorator makes it so simple to patch out methods with a mock that having to create a real function becomes a nuisance. If you pass ``autospec=True`` to patch then it does the patching with a *real* function object. This function object has the same signature as the one it is replacing, but d...
python, official-docs, cpython, P0
Local_Trusted_Corpus
b8f15f36-ae22-47a8-964d-1212c4fe9985
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,403
supabase-export-v2
8662a6d09b453bba
have support to mock :ref:`async-iterators` through ``__aiter__``. The :attr:`~Mock.return_value` attribute of ``__aiter__`` can be used to set the return values to be used for iteration. >>> mock = MagicMock() # AsyncMock also works here >>> mock.__aiter__.return_value = [1, 2, 3] >>> async def main(): ... return [...
trusted_official_docs
CPython Docs
have support to mock :ref:`async-iterators` through ``__aiter__``. The :attr:`~Mock.return_value` attribute of ``__aiter__`` can be used to set the return values to be used for iteration. >>> mock = MagicMock() # AsyncMock also works here >>> mock.__aiter__.return_value = [1, 2, 3] >>> async def main(): ... return [...
have support to mock :ref:`async-iterators` through ``__aiter__``. The :attr:`~Mock.return_value` attribute of ``__aiter__`` can be used to set the return values to be used for iteration. >>> mock = MagicMock() # AsyncMock also works here >>> mock.__aiter__.return_value = [1, 2, 3] >>> async def main(): ... return [...
python, official-docs, cpython, P0
Local_Trusted_Corpus
bbeb7d9c-a46f-4e4c-a2fc-60bf29a04242
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,370
supabase-export-v2
c532da97fd9eb194
Naming your mocks ~~~~~~~~~~~~~~~~~ It can be useful to give your mocks a name. The name is shown in the repr of the mock and can be helpful when the mock appears in test failure messages. The name is also propagated to attributes or methods of the mock:
trusted_official_docs
CPython Docs
Naming your mocks ~~~~~~~~~~~~~~~~~ It can be useful to give your mocks a name. The name is shown in the repr of the mock and can be helpful when the mock appears in test failure messages. The name is also propagated to attributes or methods of the mock:
Naming your mocks ~~~~~~~~~~~~~~~~~ It can be useful to give your mocks a name. The name is shown in the repr of the mock and can be helpful when the mock appears in test failure messages. The name is also propagated to attributes or methods of the mock:
python, official-docs, cpython, P0
Local_Trusted_Corpus
bc390d09-8d5a-4508-a2bd-ce700fbae908
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,415
supabase-export-v2
990881c3f7f7ad06
form of specification that prevents the setting of arbitrary attributes as well as the getting of them then you can use *spec_set* instead of *spec*. Using side_effect to return per file content ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
trusted_official_docs
CPython Docs
form of specification that prevents the setting of arbitrary attributes as well as the getting of them then you can use *spec_set* instead of *spec*. Using side_effect to return per file content ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
form of specification that prevents the setting of arbitrary attributes as well as the getting of them then you can use *spec_set* instead of *spec*. Using side_effect to return per file content ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
python, official-docs, cpython, P0
Local_Trusted_Corpus
bcf3829a-50ad-4558-b303-f8db69bd2c95
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,366
supabase-export-v2
64bacae6a6491cef
Mocking classes ~~~~~~~~~~~~~~~ A common use case is to mock out classes instantiated by your code under test. When you patch a class, then that class is replaced with a mock. Instances are created by *calling the class*. This means you access the "mock instance" by looking at the return value of the mocked class.
trusted_official_docs
CPython Docs
Mocking classes ~~~~~~~~~~~~~~~ A common use case is to mock out classes instantiated by your code under test. When you patch a class, then that class is replaced with a mock. Instances are created by *calling the class*. This means you access the "mock instance" by looking at the return value of the mocked class.
Mocking classes ~~~~~~~~~~~~~~~ A common use case is to mock out classes instantiated by your code under test. When you patch a class, then that class is replaced with a mock. Instances are created by *calling the class*. This means you access the "mock instance" by looking at the return value of the mocked class.
python, official-docs, cpython, P0
Local_Trusted_Corpus
bd213945-2a41-47b1-a8ff-60c2dd7e1c45
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,586
supabase-export-v2
7282af74bb6478f8
And a matcher object that can use comparison functions like this for its equality operation would look something like this: >>> class Matcher: ... def __init__(self, compare, some_obj): ... self.compare = compare ... self.some_obj = some_obj ... def __eq__(self, other): ... return self.compare(self.some_obj, other...
trusted_official_docs
CPython Docs
And a matcher object that can use comparison functions like this for its equality operation would look something like this: >>> class Matcher: ... def __init__(self, compare, some_obj): ... self.compare = compare ... self.some_obj = some_obj ... def __eq__(self, other): ... return self.compare(self.some_obj, other...
And a matcher object that can use comparison functions like this for its equality operation would look something like this: >>> class Matcher: ... def __init__(self, compare, some_obj): ... self.compare = compare ... self.some_obj = some_obj ... def __eq__(self, other): ... return self.compare(self.some_obj, other...
python, official-docs, cpython, P0
Local_Trusted_Corpus
bd717fb4-b9d5-4b98-8bf0-986728792b4e
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,524
supabase-export-v2
b1d9c941a66a6153
context manager is nice, but if you do multiple patches you can end up with nested with statements indenting further and further to the right:: >>> class MyTest(unittest.TestCase): ... ... def test_foo(self): ... with patch('mymodule.Foo') as mock_foo: ... with patch('mymodule.Bar') as mock_bar: ... with patch('mym...
trusted_official_docs
CPython Docs
context manager is nice, but if you do multiple patches you can end up with nested with statements indenting further and further to the right:: >>> class MyTest(unittest.TestCase): ... ... def test_foo(self): ... with patch('mymodule.Foo') as mock_foo: ... with patch('mymodule.Bar') as mock_bar: ... with patch('mym...
context manager is nice, but if you do multiple patches you can end up with nested with statements indenting further and further to the right:: >>> class MyTest(unittest.TestCase): ... ... def test_foo(self): ... with patch('mymodule.Foo') as mock_foo: ... with patch('mymodule.Bar') as mock_bar: ... with patch('mym...
python, official-docs, cpython, P0
Local_Trusted_Corpus
bd76b79f-fabf-4118-855c-d184c96ca4d8
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,507
supabase-export-v2
e9e4ae5d76ede4bc
arguments are mutated by the code under test then you can no longer make assertions about what the values were when the mock was called. Here's some example code that shows the problem. Imagine the following functions defined in 'mymodule'::
trusted_official_docs
CPython Docs
arguments are mutated by the code under test then you can no longer make assertions about what the values were when the mock was called. Here's some example code that shows the problem. Imagine the following functions defined in 'mymodule'::
arguments are mutated by the code under test then you can no longer make assertions about what the values were when the mock was called. Here's some example code that shows the problem. Imagine the following functions defined in 'mymodule'::
python, official-docs, cpython, P0
Local_Trusted_Corpus
bf2b984e-62f5-4d8d-9d0e-0247f7c16e7c
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,566
supabase-export-v2
15b7aa2f770720ed
our separate mocks from a parent one. Calls to those child mock will then all be recorded, in order, in the ``mock_calls`` of the parent: >>> manager = Mock() >>> mock_foo = manager.foo >>> mock_bar = manager.bar
trusted_official_docs
CPython Docs
our separate mocks from a parent one. Calls to those child mock will then all be recorded, in order, in the ``mock_calls`` of the parent: >>> manager = Mock() >>> mock_foo = manager.foo >>> mock_bar = manager.bar
our separate mocks from a parent one. Calls to those child mock will then all be recorded, in order, in the ``mock_calls`` of the parent: >>> manager = Mock() >>> mock_foo = manager.foo >>> mock_bar = manager.bar
python, official-docs, cpython, P0
Local_Trusted_Corpus
bf762a46-e715-4f1a-9d34-4a5826718296
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,528
supabase-export-v2
d3f44ccfe72ab3b4
Mocking a dictionary with MagicMock ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You may want to mock a dictionary, or other container object, recording all access to it whilst having it still behave like a dictionary.
trusted_official_docs
CPython Docs
Mocking a dictionary with MagicMock ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You may want to mock a dictionary, or other container object, recording all access to it whilst having it still behave like a dictionary.
Mocking a dictionary with MagicMock ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You may want to mock a dictionary, or other container object, recording all access to it whilst having it still behave like a dictionary.
python, official-docs, cpython, P0
Local_Trusted_Corpus
c0eca45b-5331-46c7-b6fa-034ac1620677
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,499
supabase-export-v2
61ea8183ca53ff09
>>> mock = Mock() >>> mock.foo_bar.return_value = None >>> mock.foo_bar('baz', spam='eggs') >>> mock.foo_bar.assert_called_with('baz', spam='eggs') If your mock is only being called once you can use the :meth:`~Mock.assert_called_once_with` method that also asserts that the :attr:`~Mock.call_count` is one.
trusted_official_docs
CPython Docs
>>> mock = Mock() >>> mock.foo_bar.return_value = None >>> mock.foo_bar('baz', spam='eggs') >>> mock.foo_bar.assert_called_with('baz', spam='eggs') If your mock is only being called once you can use the :meth:`~Mock.assert_called_once_with` method that also asserts that the :attr:`~Mock.call_count` is one.
>>> mock = Mock() >>> mock.foo_bar.return_value = None >>> mock.foo_bar('baz', spam='eggs') >>> mock.foo_bar.assert_called_with('baz', spam='eggs') If your mock is only being called once you can use the :meth:`~Mock.assert_called_once_with` method that also asserts that the :attr:`~Mock.call_count` is one.
python, official-docs, cpython, P0
Local_Trusted_Corpus
c2395f4c-77b8-4def-a18c-5e17b94a6e91
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,547
supabase-export-v2
1d310fc296e611c5
>>> mymock.foo <MyMock name='mock.foo' id='...'> >>> mymock.foo.has_been_called() False >>> mymock.foo() <MyMock name='mock.foo()' id='...'> >>> mymock.foo.has_been_called() True Sometimes this is inconvenient. For example, `one user <https://code.google.com/archive/p/mock/issues/105>`_ is subclassing mock to created a...
trusted_official_docs
CPython Docs
>>> mymock.foo <MyMock name='mock.foo' id='...'> >>> mymock.foo.has_been_called() False >>> mymock.foo() <MyMock name='mock.foo()' id='...'> >>> mymock.foo.has_been_called() True Sometimes this is inconvenient. For example, `one user <https://code.google.com/archive/p/mock/issues/105>`_ is subclassing mock to created a...
>>> mymock.foo <MyMock name='mock.foo' id='...'> >>> mymock.foo.has_been_called() False >>> mymock.foo() <MyMock name='mock.foo()' id='...'> >>> mymock.foo.has_been_called() True Sometimes this is inconvenient. For example, `one user <https://code.google.com/archive/p/mock/issues/105>`_ is subclassing mock to created a...
python, official-docs, cpython, P0
Local_Trusted_Corpus
c25a0da9-ee8d-4376-a8aa-00d6f7b895b0
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,364
supabase-export-v2
32521c3676e5d149
>>> real = ProductionClass() >>> mock = Mock() >>> real.closer(mock) >>> mock.close.assert_called_with() We don't have to do any work to provide the 'close' method on our mock. Accessing close creates it. So, if 'close' hasn't already been called then accessing it in the test will create it, but :meth:`~Mock.assert_cal...
trusted_official_docs
CPython Docs
>>> real = ProductionClass() >>> mock = Mock() >>> real.closer(mock) >>> mock.close.assert_called_with() We don't have to do any work to provide the 'close' method on our mock. Accessing close creates it. So, if 'close' hasn't already been called then accessing it in the test will create it, but :meth:`~Mock.assert_cal...
>>> real = ProductionClass() >>> mock = Mock() >>> real.closer(mock) >>> mock.close.assert_called_with() We don't have to do any work to provide the 'close' method on our mock. Accessing close creates it. So, if 'close' hasn't already been called then accessing it in the test will create it, but :meth:`~Mock.assert_cal...
python, official-docs, cpython, P0
Local_Trusted_Corpus
c4999eb7-937c-404e-a786-e14353c87be5
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,405
supabase-export-v2
69b6d5be1a79ed96
Mocking asynchronous context manager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Since Python 3.8, ``AsyncMock`` and ``MagicMock`` have support to mock :ref:`async-context-managers` through ``__aenter__`` and ``__aexit__``. By default, ``__aenter__`` and ``__aexit__`` are ``AsyncMock`` instances that return an async function.
trusted_official_docs
CPython Docs
Mocking asynchronous context manager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Since Python 3.8, ``AsyncMock`` and ``MagicMock`` have support to mock :ref:`async-context-managers` through ``__aenter__`` and ``__aexit__``. By default, ``__aenter__`` and ``__aexit__`` are ``AsyncMock`` instances that return an async function.
Mocking asynchronous context manager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Since Python 3.8, ``AsyncMock`` and ``MagicMock`` have support to mock :ref:`async-context-managers` through ``__aenter__`` and ``__aexit__``. By default, ``__aenter__`` and ``__aexit__`` are ``AsyncMock`` instances that return an async function.
python, official-docs, cpython, P0
Local_Trusted_Corpus
c4a15d8d-ec56-4b8d-b860-ff105d5c56aa
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,477
supabase-export-v2
b138c094e00b8917
A Python generator is a function or method that uses the :keyword:`yield` statement to return a series of values when iterated over [#]_. A generator method / function is called to return the generator object. It is the generator object that is then iterated over. The protocol method for iteration is :meth:`~container....
trusted_official_docs
CPython Docs
A Python generator is a function or method that uses the :keyword:`yield` statement to return a series of values when iterated over [#]_. A generator method / function is called to return the generator object. It is the generator object that is then iterated over. The protocol method for iteration is :meth:`~container....
A Python generator is a function or method that uses the :keyword:`yield` statement to return a series of values when iterated over [#]_. A generator method / function is called to return the generator object. It is the generator object that is then iterated over. The protocol method for iteration is :meth:`~container....
python, official-docs, cpython, P0
Local_Trusted_Corpus
c5033e33-e522-4ef8-bfb6-703ec56ea07b
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,519
supabase-export-v2
a1fb67a59388de72
>>> def side_effect(arg): ... assert arg == {6} ... >>> mock = Mock(side_effect=side_effect) >>> mock({6}) >>> mock(set()) Traceback (most recent call last): ... AssertionError An alternative approach is to create a subclass of :class:`Mock` or :class:`MagicMock` that copies (using :func:`copy.deepcopy`) the arguments....
trusted_official_docs
CPython Docs
>>> def side_effect(arg): ... assert arg == {6} ... >>> mock = Mock(side_effect=side_effect) >>> mock({6}) >>> mock(set()) Traceback (most recent call last): ... AssertionError An alternative approach is to create a subclass of :class:`Mock` or :class:`MagicMock` that copies (using :func:`copy.deepcopy`) the arguments....
>>> def side_effect(arg): ... assert arg == {6} ... >>> mock = Mock(side_effect=side_effect) >>> mock({6}) >>> mock(set()) Traceback (most recent call last): ... AssertionError An alternative approach is to create a subclass of :class:`Mock` or :class:`MagicMock` that copies (using :func:`copy.deepcopy`) the arguments....
python, official-docs, cpython, P0
Local_Trusted_Corpus
c55e0598-f1a1-4e07-aba7-29d370e09422
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,486
supabase-export-v2
d54910c4db19ccbb
class decorator. This applies the patches to all test methods on the class. A test method is identified by methods whose names start with ``test``:: >>> @patch('mymodule.SomeClass') ... class MyTest(unittest.TestCase): ... ... def test_one(self, MockSomeClass): ... self.assertIs(mymodule.SomeClass, MockSomeClass) ....
trusted_official_docs
CPython Docs
class decorator. This applies the patches to all test methods on the class. A test method is identified by methods whose names start with ``test``:: >>> @patch('mymodule.SomeClass') ... class MyTest(unittest.TestCase): ... ... def test_one(self, MockSomeClass): ... self.assertIs(mymodule.SomeClass, MockSomeClass) ....
class decorator. This applies the patches to all test methods on the class. A test method is identified by methods whose names start with ``test``:: >>> @patch('mymodule.SomeClass') ... class MyTest(unittest.TestCase): ... ... def test_one(self, MockSomeClass): ... self.assertIs(mymodule.SomeClass, MockSomeClass) ....
python, official-docs, cpython, P0
Local_Trusted_Corpus
c59a430d-edb0-4988-bda0-5f3bcdbbe4a2
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,440
supabase-export-v2
f0926539d1a1eea1
*Python* order that decorators are applied). This means from the bottom up, so in the example above the mock for ``test_module.ClassName2`` is passed in first. There is also :func:`patch.dict` for setting values in a dictionary just during a scope and restoring the dictionary to its original state when the test ends:
trusted_official_docs
CPython Docs
*Python* order that decorators are applied). This means from the bottom up, so in the example above the mock for ``test_module.ClassName2`` is passed in first. There is also :func:`patch.dict` for setting values in a dictionary just during a scope and restoring the dictionary to its original state when the test ends:
*Python* order that decorators are applied). This means from the bottom up, so in the example above the mock for ``test_module.ClassName2`` is passed in first. There is also :func:`patch.dict` for setting values in a dictionary just during a scope and restoring the dictionary to its original state when the test ends:
python, official-docs, cpython, P0
Local_Trusted_Corpus
c617716a-f75e-46ab-8326-18c903ed7585
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,390
supabase-export-v2
6132a14fa8e067ce
like for example ``mock.connection.cursor().execute("SELECT 1")``. If we wanted this call to return a list, then we have to configure the result of the nested call. We can use :data:`call` to construct the set of calls in a "chained call" like this for easy assertion afterwards:
trusted_official_docs
CPython Docs
like for example ``mock.connection.cursor().execute("SELECT 1")``. If we wanted this call to return a list, then we have to configure the result of the nested call. We can use :data:`call` to construct the set of calls in a "chained call" like this for easy assertion afterwards:
like for example ``mock.connection.cursor().execute("SELECT 1")``. If we wanted this call to return a list, then we have to configure the result of the nested call. We can use :data:`call` to construct the set of calls in a "chained call" like this for easy assertion afterwards:
python, official-docs, cpython, P0
Local_Trusted_Corpus
c67c7f2e-7b39-496a-ae2d-fdef775e86a6
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,590
supabase-export-v2
d0d3f7b5947b73d5
was called with against the one we created our matcher with. If they match then ``assert_called_with`` passes, and if they don't an :exc:`AssertionError` is raised: >>> match_wrong = Matcher(compare, Foo(3, 4)) >>> mock.assert_called_with(match_wrong) Traceback (most recent call last): ... AssertionError: Expected: ...
trusted_official_docs
CPython Docs
was called with against the one we created our matcher with. If they match then ``assert_called_with`` passes, and if they don't an :exc:`AssertionError` is raised: >>> match_wrong = Matcher(compare, Foo(3, 4)) >>> mock.assert_called_with(match_wrong) Traceback (most recent call last): ... AssertionError: Expected: ...
was called with against the one we created our matcher with. If they match then ``assert_called_with`` passes, and if they don't an :exc:`AssertionError` is raised: >>> match_wrong = Matcher(compare, Foo(3, 4)) >>> mock.assert_called_with(match_wrong) Traceback (most recent call last): ... AssertionError: Expected: ...
python, official-docs, cpython, P0
Local_Trusted_Corpus
c68ee269-af50-451c-9ded-a125eceeb330
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,513
supabase-export-v2
348bc6e31a0c4df1
for mock to copy the arguments you pass in. This could then cause problems if you do assertions that rely on object identity for equality. Here's one solution that uses the :attr:`~Mock.side_effect` functionality. If you provide a ``side_effect`` function for a mock then ``side_effect`` will be called with the same arg...
trusted_official_docs
CPython Docs
for mock to copy the arguments you pass in. This could then cause problems if you do assertions that rely on object identity for equality. Here's one solution that uses the :attr:`~Mock.side_effect` functionality. If you provide a ``side_effect`` function for a mock then ``side_effect`` will be called with the same arg...
for mock to copy the arguments you pass in. This could then cause problems if you do assertions that rely on object identity for equality. Here's one solution that uses the :attr:`~Mock.side_effect` functionality. If you provide a ``side_effect`` function for a mock then ``side_effect`` will be called with the same arg...
python, official-docs, cpython, P0
Local_Trusted_Corpus
c7d803ae-c112-4a69-ab56-ea88982ccd3f
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,478
supabase-export-v2
e6a53edc8ff9e1f9
It is the generator object that is then iterated over. The protocol method for iteration is :meth:`~container.__iter__`, so we can mock this using a :class:`MagicMock`. Here's an example class with an "iter" method implemented as a generator:
trusted_official_docs
CPython Docs
It is the generator object that is then iterated over. The protocol method for iteration is :meth:`~container.__iter__`, so we can mock this using a :class:`MagicMock`. Here's an example class with an "iter" method implemented as a generator:
It is the generator object that is then iterated over. The protocol method for iteration is :meth:`~container.__iter__`, so we can mock this using a :class:`MagicMock`. Here's an example class with an "iter" method implemented as a generator:
python, official-docs, cpython, P0
Local_Trusted_Corpus
c8ef22ed-89f7-4bf1-8ebc-e1301126cb52
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,565
supabase-export-v2
70cb30cc2cde84bf
attribute. This doesn't allow you to track the order of calls between separate mock objects, however we can use :attr:`~Mock.mock_calls` to achieve the same effect. Because mocks track calls to child mocks in ``mock_calls``, and accessing an arbitrary attribute of a mock creates a child mock, we can create our separate...
trusted_official_docs
CPython Docs
attribute. This doesn't allow you to track the order of calls between separate mock objects, however we can use :attr:`~Mock.mock_calls` to achieve the same effect. Because mocks track calls to child mocks in ``mock_calls``, and accessing an arbitrary attribute of a mock creates a child mock, we can create our separate...
attribute. This doesn't allow you to track the order of calls between separate mock objects, however we can use :attr:`~Mock.mock_calls` to achieve the same effect. Because mocks track calls to child mocks in ``mock_calls``, and accessing an arbitrary attribute of a mock creates a child mock, we can create our separate...
python, official-docs, cpython, P0
Local_Trusted_Corpus
c9f59b46-f384-4d2b-9816-dd994e8683e7
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,442
supabase-export-v2
79fb92c5f765c3a8
= {'key': 'value'} >>> original = foo.copy() >>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True): ... assert foo == {'newkey': 'newvalue'} ... >>> assert foo == original ``patch``, ``patch.object`` and ``patch.dict`` can all be used as context managers.
trusted_official_docs
CPython Docs
= {'key': 'value'} >>> original = foo.copy() >>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True): ... assert foo == {'newkey': 'newvalue'} ... >>> assert foo == original ``patch``, ``patch.object`` and ``patch.dict`` can all be used as context managers.
= {'key': 'value'} >>> original = foo.copy() >>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True): ... assert foo == {'newkey': 'newvalue'} ... >>> assert foo == original ``patch``, ``patch.object`` and ``patch.dict`` can all be used as context managers.
python, official-docs, cpython, P0
Local_Trusted_Corpus
ca2bf018-2411-4c2a-8b63-f364b9774cbf
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,583
supabase-export-v2
94044d8a17397f73
2)) >>> mock.assert_called_with(Foo(1, 2)) Traceback (most recent call last): ... AssertionError: expected call not found. Expected: mock(<__main__.Foo object at 0x...>) Actual: mock(<__main__.Foo object at 0x...>) A comparison function for our ``Foo`` class might look something like this:
trusted_official_docs
CPython Docs
2)) >>> mock.assert_called_with(Foo(1, 2)) Traceback (most recent call last): ... AssertionError: expected call not found. Expected: mock(<__main__.Foo object at 0x...>) Actual: mock(<__main__.Foo object at 0x...>) A comparison function for our ``Foo`` class might look something like this:
2)) >>> mock.assert_called_with(Foo(1, 2)) Traceback (most recent call last): ... AssertionError: expected call not found. Expected: mock(<__main__.Foo object at 0x...>) Actual: mock(<__main__.Foo object at 0x...>) A comparison function for our ``Foo`` class might look something like this:
python, official-docs, cpython, P0
Local_Trusted_Corpus
ce5d8420-5dae-4d10-ac6d-93e997f83224
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,573
supabase-export-v2
0493aa0fb0c4050f
patch('mymodule.Class2') as MockClass2: ... manager.attach_mock(MockClass1, 'MockClass1') ... manager.attach_mock(MockClass2, 'MockClass2') ... MockClass1().foo() ... MockClass2().bar() <MagicMock name='mock.MockClass1().foo()' id='...'> <MagicMock name='mock.MockClass2().bar()' id='...'> >>> manager.mock_calls [call.M...
trusted_official_docs
CPython Docs
patch('mymodule.Class2') as MockClass2: ... manager.attach_mock(MockClass1, 'MockClass1') ... manager.attach_mock(MockClass2, 'MockClass2') ... MockClass1().foo() ... MockClass2().bar() <MagicMock name='mock.MockClass1().foo()' id='...'> <MagicMock name='mock.MockClass2().bar()' id='...'> >>> manager.mock_calls [call.M...
patch('mymodule.Class2') as MockClass2: ... manager.attach_mock(MockClass1, 'MockClass1') ... manager.attach_mock(MockClass2, 'MockClass2') ... MockClass1().foo() ... MockClass2().bar() <MagicMock name='mock.MockClass1().foo()' id='...'> <MagicMock name='mock.MockClass2().bar()' id='...'> >>> manager.mock_calls [call.M...
python, official-docs, cpython, P0
Local_Trusted_Corpus
d15e8837-a8ac-462a-8417-3dbc1ef1bb29
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,471
supabase-export-v2
700b340d1ab8e27a
... mock_date.side_effect = lambda *args, **kw: dt.date(*args, **kw) ... ... assert mymodule.date.today() == dt.date(2010, 10, 8) ... assert mymodule.date(2009, 6, 8) == dt.date(2009, 6, 8) Note that we don't patch :class:`datetime.date` globally, we patch ``date`` in the module that *uses* it. See :ref:`where to patch...
trusted_official_docs
CPython Docs
... mock_date.side_effect = lambda *args, **kw: dt.date(*args, **kw) ... ... assert mymodule.date.today() == dt.date(2010, 10, 8) ... assert mymodule.date(2009, 6, 8) == dt.date(2009, 6, 8) Note that we don't patch :class:`datetime.date` globally, we patch ``date`` in the module that *uses* it. See :ref:`where to patch...
... mock_date.side_effect = lambda *args, **kw: dt.date(*args, **kw) ... ... assert mymodule.date.today() == dt.date(2010, 10, 8) ... assert mymodule.date(2009, 6, 8) == dt.date(2009, 6, 8) Note that we don't patch :class:`datetime.date` globally, we patch ``date`` in the module that *uses* it. See :ref:`where to patch...
python, official-docs, cpython, P0
Local_Trusted_Corpus
d44a2b36-afad-4c32-96d7-85213d305638
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,560
supabase-export-v2
9469038c2f7eff5c
This also works for the ``from module import name`` form: >>> mock = Mock() >>> with patch.dict('sys.modules', {'fooble': mock}): ... from fooble import blob ... blob.blip() ... <Mock name='mock.blob.blip()' id='...'> >>> mock.blob.blip.assert_called_once_with()
trusted_official_docs
CPython Docs
This also works for the ``from module import name`` form: >>> mock = Mock() >>> with patch.dict('sys.modules', {'fooble': mock}): ... from fooble import blob ... blob.blip() ... <Mock name='mock.blob.blip()' id='...'> >>> mock.blob.blip.assert_called_once_with()
This also works for the ``from module import name`` form: >>> mock = Mock() >>> with patch.dict('sys.modules', {'fooble': mock}): ... from fooble import blob ... blob.blip() ... <Mock name='mock.blob.blip()' id='...'> >>> mock.blob.blip.assert_called_once_with()
python, official-docs, cpython, P0
Local_Trusted_Corpus
d627bf19-d5fc-408c-b0bf-1d95fdff819c
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,455
supabase-export-v2
b2bd92ef69bc344b
So, suppose we have some code that looks a little bit like this: >>> class Something: ... def __init__(self): ... self.backend = BackendProvider() ... def method(self): ... response = self.backend.get_endpoint('foobar').create_call('spam', 'eggs').start_call() ... # more code
trusted_official_docs
CPython Docs
So, suppose we have some code that looks a little bit like this: >>> class Something: ... def __init__(self): ... self.backend = BackendProvider() ... def method(self): ... response = self.backend.get_endpoint('foobar').create_call('spam', 'eggs').start_call() ... # more code
So, suppose we have some code that looks a little bit like this: >>> class Something: ... def __init__(self): ... self.backend = BackendProvider() ... def method(self): ... response = self.backend.get_endpoint('foobar').create_call('spam', 'eggs').start_call() ... # more code
python, official-docs, cpython, P0
Local_Trusted_Corpus
d7ac0d64-187f-4379-9a8f-f9b453f6e0c6
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,444
supabase-export-v2
991a15c65e4cb93a
you use :func:`patch` to create a mock for you, you can get a reference to the mock using the "as" form of the with statement: >>> class ProductionClass: ... def method(self): ... pass ... >>> with patch.object(ProductionClass, 'method') as mock_method: ... mock_method.return_value = None ... real = ProductionClas...
trusted_official_docs
CPython Docs
you use :func:`patch` to create a mock for you, you can get a reference to the mock using the "as" form of the with statement: >>> class ProductionClass: ... def method(self): ... pass ... >>> with patch.object(ProductionClass, 'method') as mock_method: ... mock_method.return_value = None ... real = ProductionClas...
you use :func:`patch` to create a mock for you, you can get a reference to the mock using the "as" form of the with statement: >>> class ProductionClass: ... def method(self): ... pass ... >>> with patch.object(ProductionClass, 'method') as mock_method: ... mock_method.return_value = None ... real = ProductionClas...
python, official-docs, cpython, P0
Local_Trusted_Corpus
d83bebb0-0326-415f-9234-12eb6b95a5b9
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,502
supabase-export-v2
7787887fea040d9c
call. If your mock is going to be called several times, and you want to make assertions about *all* those calls you can use :attr:`~Mock.call_args_list`: >>> mock = Mock(return_value=None) >>> mock(1, 2, 3) >>> mock(4, 5, 6) >>> mock() >>> mock.call_args_list [call(1, 2, 3), call(4, 5, 6), call()]
trusted_official_docs
CPython Docs
call. If your mock is going to be called several times, and you want to make assertions about *all* those calls you can use :attr:`~Mock.call_args_list`: >>> mock = Mock(return_value=None) >>> mock(1, 2, 3) >>> mock(4, 5, 6) >>> mock() >>> mock.call_args_list [call(1, 2, 3), call(4, 5, 6), call()]
call. If your mock is going to be called several times, and you want to make assertions about *all* those calls you can use :attr:`~Mock.call_args_list`: >>> mock = Mock(return_value=None) >>> mock(1, 2, 3) >>> mock(4, 5, 6) >>> mock() >>> mock.call_args_list [call(1, 2, 3), call(4, 5, 6), call()]
python, official-docs, cpython, P0
Local_Trusted_Corpus
d8d529b4-a978-43bf-94e2-b82f2921cbcb
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,412
supabase-export-v2
78e6ddf12308a83f
a specification also enables a smarter matching of calls made to the mock, regardless of whether some parameters were passed as positional or named arguments:: >>> def f(a, b, c): pass ... >>> mock = Mock(spec=f) >>> mock(1, 2, 3) <Mock name='mock()' id='140161580456576'> >>> mock.assert_called_with(a=1, b=2, c=3)
trusted_official_docs
CPython Docs
a specification also enables a smarter matching of calls made to the mock, regardless of whether some parameters were passed as positional or named arguments:: >>> def f(a, b, c): pass ... >>> mock = Mock(spec=f) >>> mock(1, 2, 3) <Mock name='mock()' id='140161580456576'> >>> mock.assert_called_with(a=1, b=2, c=3)
a specification also enables a smarter matching of calls made to the mock, regardless of whether some parameters were passed as positional or named arguments:: >>> def f(a, b, c): pass ... >>> mock = Mock(spec=f) >>> mock(1, 2, 3) <Mock name='mock()' id='140161580456576'> >>> mock.assert_called_with(a=1, b=2, c=3)
python, official-docs, cpython, P0
Local_Trusted_Corpus
de26b014-000a-4ecc-a552-16c851b4e498
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,439
supabase-export-v2
9b2664b90392d917
>>> class MyTest(unittest.TestCase): ... @patch('package.module.ClassName1') ... @patch('package.module.ClassName2') ... def test_something(self, MockClass2, MockClass1): ... self.assertIs(package.module.ClassName1, MockClass1) ... self.assertIs(package.module.ClassName2, MockClass2) ... >>> MyTest('test_something').te...
trusted_official_docs
CPython Docs
>>> class MyTest(unittest.TestCase): ... @patch('package.module.ClassName1') ... @patch('package.module.ClassName2') ... def test_something(self, MockClass2, MockClass1): ... self.assertIs(package.module.ClassName1, MockClass1) ... self.assertIs(package.module.ClassName2, MockClass2) ... >>> MyTest('test_something').te...
>>> class MyTest(unittest.TestCase): ... @patch('package.module.ClassName1') ... @patch('package.module.ClassName2') ... def test_something(self, MockClass2, MockClass1): ... self.assertIs(package.module.ClassName1, MockClass1) ... self.assertIs(package.module.ClassName2, MockClass2) ... >>> MyTest('test_something').te...
python, official-docs, cpython, P0
Local_Trusted_Corpus
dea387ee-49db-4ea1-8323-41fdd4c32f65
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,470
supabase-export-v2
ab4e60a33d549c7a
lambda function that returns a real date. When the mock date class is called a real date will be constructed and returned by ``side_effect``. :: >>> import datetime as dt >>> with patch('mymodule.date') as mock_date: ... mock_date.today.return_value = dt.date(2010, 10, 8) ... mock_date.side_effect = lambda *args, **...
trusted_official_docs
CPython Docs
lambda function that returns a real date. When the mock date class is called a real date will be constructed and returned by ``side_effect``. :: >>> import datetime as dt >>> with patch('mymodule.date') as mock_date: ... mock_date.today.return_value = dt.date(2010, 10, 8) ... mock_date.side_effect = lambda *args, **...
lambda function that returns a real date. When the mock date class is called a real date will be constructed and returned by ``side_effect``. :: >>> import datetime as dt >>> with patch('mymodule.date') as mock_date: ... mock_date.today.return_value = dt.date(2010, 10, 8) ... mock_date.side_effect = lambda *args, **...
python, official-docs, cpython, P0
Local_Trusted_Corpus
df374162-d3de-47e7-ba09-b8514239ca30
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,454
supabase-export-v2
e3ad289efade4578
configure and then make assertions about chained calls. Of course another alternative is writing your code in a more testable way in the first place... So, suppose we have some code that looks a little bit like this:
trusted_official_docs
CPython Docs
configure and then make assertions about chained calls. Of course another alternative is writing your code in a more testable way in the first place... So, suppose we have some code that looks a little bit like this:
configure and then make assertions about chained calls. Of course another alternative is writing your code in a more testable way in the first place... So, suppose we have some code that looks a little bit like this:
python, official-docs, cpython, P0
Local_Trusted_Corpus
e1909dd1-64ea-4973-8e85-13958bed3951
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,563
supabase-export-v2
63af06a2724ff716
Mock() >>> modules = {'package': mock, 'package.module': mock.module} >>> with patch.dict('sys.modules', modules): ... from package.module import fooble ... fooble() ... <Mock name='mock.module.fooble()' id='...'> >>> mock.module.fooble.assert_called_once_with() Tracking order of calls and less verbose call assertions ...
trusted_official_docs
CPython Docs
Mock() >>> modules = {'package': mock, 'package.module': mock.module} >>> with patch.dict('sys.modules', modules): ... from package.module import fooble ... fooble() ... <Mock name='mock.module.fooble()' id='...'> >>> mock.module.fooble.assert_called_once_with() Tracking order of calls and less verbose call assertions ...
Mock() >>> modules = {'package': mock, 'package.module': mock.module} >>> with patch.dict('sys.modules', modules): ... from package.module import fooble ... fooble() ... <Mock name='mock.module.fooble()' id='...'> >>> mock.module.fooble.assert_called_once_with() Tracking order of calls and less verbose call assertions ...
python, official-docs, cpython, P0
Local_Trusted_Corpus
e31f3971-ea26-4e07-8410-8506dc1227b8
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,557
supabase-export-v2
72209c23727dfe2c
Here's an example that mocks out the 'fooble' module. >>> import sys >>> mock = Mock() >>> with patch.dict('sys.modules', {'fooble': mock}): ... import fooble ... fooble.blob() ... <Mock name='mock.blob()' id='...'> >>> assert 'fooble' not in sys.modules >>> mock.blob.assert_called_once_with()
trusted_official_docs
CPython Docs
Here's an example that mocks out the 'fooble' module. >>> import sys >>> mock = Mock() >>> with patch.dict('sys.modules', {'fooble': mock}): ... import fooble ... fooble.blob() ... <Mock name='mock.blob()' id='...'> >>> assert 'fooble' not in sys.modules >>> mock.blob.assert_called_once_with()
Here's an example that mocks out the 'fooble' module. >>> import sys >>> mock = Mock() >>> with patch.dict('sys.modules', {'fooble': mock}): ... import fooble ... fooble.blob() ... <Mock name='mock.blob()' id='...'> >>> assert 'fooble' not in sys.modules >>> mock.blob.assert_called_once_with()
python, official-docs, cpython, P0
Local_Trusted_Corpus
e5e65c04-734c-4428-8c28-f4d897a2fd64
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,526
supabase-export-v2
7baa65b571c2d1da
achieve the same effect without the nested indentation. A simple helper method, ``create_patch``, puts the patch in place and returns the created mock for us:: >>> class MyTest(unittest.TestCase): ... ... def create_patch(self, name): ... patcher = patch(name) ... thing = patcher.start() ... self.addCleanup(patcher...
trusted_official_docs
CPython Docs
achieve the same effect without the nested indentation. A simple helper method, ``create_patch``, puts the patch in place and returns the created mock for us:: >>> class MyTest(unittest.TestCase): ... ... def create_patch(self, name): ... patcher = patch(name) ... thing = patcher.start() ... self.addCleanup(patcher...
achieve the same effect without the nested indentation. A simple helper method, ``create_patch``, puts the patch in place and returns the created mock for us:: >>> class MyTest(unittest.TestCase): ... ... def create_patch(self, name): ... patcher = patch(name) ... thing = patcher.start() ... self.addCleanup(patcher...
python, official-docs, cpython, P0
Local_Trusted_Corpus
e7416d14-cbe7-43aa-9a75-df3b15298a6f
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,452
supabase-export-v2
6d00c361061475b9
This means that you can see how the object returned from a call to a mocked object has been used by interrogating the ``return_value`` mock: >>> mock = Mock() >>> mock().foo(a=2, b=3) <Mock name='mock().foo()' id='...'> >>> mock.return_value.foo.assert_called_with(a=2, b=3)
trusted_official_docs
CPython Docs
This means that you can see how the object returned from a call to a mocked object has been used by interrogating the ``return_value`` mock: >>> mock = Mock() >>> mock().foo(a=2, b=3) <Mock name='mock().foo()' id='...'> >>> mock.return_value.foo.assert_called_with(a=2, b=3)
This means that you can see how the object returned from a call to a mocked object has been used by interrogating the ``return_value`` mock: >>> mock = Mock() >>> mock().foo(a=2, b=3) <Mock name='mock().foo()' id='...'> >>> mock.return_value.foo.assert_called_with(a=2, b=3)
python, official-docs, cpython, P0
Local_Trusted_Corpus
e7a3e7ba-95ee-4ac4-b30d-a8e3fdb5b8c7
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,473
supabase-export-v2
1008b34a1558dbcb
can find yourself having to calculate an expected result using exactly the same algorithm as the code under test, which is a classic testing anti-pattern. Calls to the date constructor are recorded in the ``mock_date`` attributes (``call_count`` and friends) which may also be useful for your tests.
trusted_official_docs
CPython Docs
can find yourself having to calculate an expected result using exactly the same algorithm as the code under test, which is a classic testing anti-pattern. Calls to the date constructor are recorded in the ``mock_date`` attributes (``call_count`` and friends) which may also be useful for your tests.
can find yourself having to calculate an expected result using exactly the same algorithm as the code under test, which is a classic testing anti-pattern. Calls to the date constructor are recorded in the ``mock_date`` attributes (``call_count`` and friends) which may also be useful for your tests.
python, official-docs, cpython, P0
Local_Trusted_Corpus
e7eb70bc-77ad-46e2-9dc6-a64900b5f350
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,520
supabase-export-v2
bb0ff9a5568a6cd6
An alternative approach is to create a subclass of :class:`Mock` or :class:`MagicMock` that copies (using :func:`copy.deepcopy`) the arguments. Here's an example implementation: >>> from copy import deepcopy >>> class CopyingMock(MagicMock): ... def __call__(self, /, *args, **kwargs): ... args = deepcopy(args) ... ...
trusted_official_docs
CPython Docs
An alternative approach is to create a subclass of :class:`Mock` or :class:`MagicMock` that copies (using :func:`copy.deepcopy`) the arguments. Here's an example implementation: >>> from copy import deepcopy >>> class CopyingMock(MagicMock): ... def __call__(self, /, *args, **kwargs): ... args = deepcopy(args) ... ...
An alternative approach is to create a subclass of :class:`Mock` or :class:`MagicMock` that copies (using :func:`copy.deepcopy`) the arguments. Here's an example implementation: >>> from copy import deepcopy >>> class CopyingMock(MagicMock): ... def __call__(self, /, *args, **kwargs): ... args = deepcopy(args) ... ...
python, official-docs, cpython, P0
Local_Trusted_Corpus
e8a7b44a-a6b3-412c-8334-26f9ec69125c
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,376
supabase-export-v2
db1a66cf6214a156
that the calls you expected have been made, you are also checking that they were made in the right order and with no additional calls: You use the :data:`call` object to construct lists for comparing with ``mock_calls``:
trusted_official_docs
CPython Docs
that the calls you expected have been made, you are also checking that they were made in the right order and with no additional calls: You use the :data:`call` object to construct lists for comparing with ``mock_calls``:
that the calls you expected have been made, you are also checking that they were made in the right order and with no additional calls: You use the :data:`call` object to construct lists for comparing with ``mock_calls``:
python, official-docs, cpython, P0
Local_Trusted_Corpus
e8bc9ff6-40f2-411d-8b97-a4af104c9f41
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,363
supabase-export-v2
11da982f02108f9e
So to test it we need to pass in an object with a ``close`` method and check that it was called correctly. >>> real = ProductionClass() >>> mock = Mock() >>> real.closer(mock) >>> mock.close.assert_called_with()
trusted_official_docs
CPython Docs
So to test it we need to pass in an object with a ``close`` method and check that it was called correctly. >>> real = ProductionClass() >>> mock = Mock() >>> real.closer(mock) >>> mock.close.assert_called_with()
So to test it we need to pass in an object with a ``close`` method and check that it was called correctly. >>> real = ProductionClass() >>> mock = Mock() >>> real.closer(mock) >>> mock.close.assert_called_with()
python, official-docs, cpython, P0
Local_Trusted_Corpus
e8d9a3e1-6d35-4314-b136-7a48cb34d457
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,359
supabase-export-v2
64b0c1c3d43310be
Mock for method calls on an object ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the last example we patched a method directly on an object to check that it was called correctly. Another common use case is to pass an object into a method (or some part of the system under test) and then check that it is used in the correct way.
trusted_official_docs
CPython Docs
Mock for method calls on an object ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the last example we patched a method directly on an object to check that it was called correctly. Another common use case is to pass an object into a method (or some part of the system under test) and then check that it is used in the correct way.
Mock for method calls on an object ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the last example we patched a method directly on an object to check that it was called correctly. Another common use case is to pass an object into a method (or some part of the system under test) and then check that it is used in the correct way.
python, official-docs, cpython, P0
Local_Trusted_Corpus
e935004c-5f6e-4a37-9eb8-e17807f158dd
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,545
supabase-export-v2
11fe75c921ed25e3
class MyMock(MagicMock): ... def has_been_called(self): ... return self.called ... >>> mymock = MyMock(return_value=None) >>> mymock <MyMock id='...'> >>> mymock.has_been_called() False >>> mymock() >>> mymock.has_been_called() True The standard behaviour for ``Mock`` instances is that attributes and the return value m...
trusted_official_docs
CPython Docs
class MyMock(MagicMock): ... def has_been_called(self): ... return self.called ... >>> mymock = MyMock(return_value=None) >>> mymock <MyMock id='...'> >>> mymock.has_been_called() False >>> mymock() >>> mymock.has_been_called() True The standard behaviour for ``Mock`` instances is that attributes and the return value m...
class MyMock(MagicMock): ... def has_been_called(self): ... return self.called ... >>> mymock = MyMock(return_value=None) >>> mymock <MyMock id='...'> >>> mymock.has_been_called() False >>> mymock() >>> mymock.has_been_called() True The standard behaviour for ``Mock`` instances is that attributes and the return value m...
python, official-docs, cpython, P0
Local_Trusted_Corpus
ea66431f-d1bf-4bcc-bd59-20249591619a
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,501
supabase-export-v2
195c084ef46ef3bc
>>> mock.foo_bar() >>> mock.foo_bar.assert_called_once_with('baz', spam='eggs') Traceback (most recent call last): ... AssertionError: Expected 'foo_bar' to be called once. Called 2 times. Calls: [call('baz', spam='eggs'), call()]. Both ``assert_called_with`` and ``assert_called_once_with`` make assertions about the *m...
trusted_official_docs
CPython Docs
>>> mock.foo_bar() >>> mock.foo_bar.assert_called_once_with('baz', spam='eggs') Traceback (most recent call last): ... AssertionError: Expected 'foo_bar' to be called once. Called 2 times. Calls: [call('baz', spam='eggs'), call()]. Both ``assert_called_with`` and ``assert_called_once_with`` make assertions about the *m...
>>> mock.foo_bar() >>> mock.foo_bar.assert_called_once_with('baz', spam='eggs') Traceback (most recent call last): ... AssertionError: Expected 'foo_bar' to be called once. Called 2 times. Calls: [call('baz', spam='eggs'), call()]. Both ``assert_called_with`` and ``assert_called_once_with`` make assertions about the *m...
python, official-docs, cpython, P0
Local_Trusted_Corpus
ea72b0f2-09f7-4077-8d8f-c86adc923485
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,425
supabase-export-v2
5f0ca6abbfea6611
so patching on them has to be undone after the test or the patch will persist into other tests and cause hard to diagnose problems. mock provides three convenient decorators for this: :func:`patch`, :func:`patch.object` and :func:`patch.dict`. ``patch`` takes a single string, of the form ``package.module.Class.attribut...
trusted_official_docs
CPython Docs
so patching on them has to be undone after the test or the patch will persist into other tests and cause hard to diagnose problems. mock provides three convenient decorators for this: :func:`patch`, :func:`patch.object` and :func:`patch.dict`. ``patch`` takes a single string, of the form ``package.module.Class.attribut...
so patching on them has to be undone after the test or the patch will persist into other tests and cause hard to diagnose problems. mock provides three convenient decorators for this: :func:`patch`, :func:`patch.object` and :func:`patch.dict`. ``patch`` takes a single string, of the form ``package.module.Class.attribut...
python, official-docs, cpython, P0
Local_Trusted_Corpus
ea9cee1e-bf58-4729-ad0f-44435b54338f
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,574
supabase-export-v2
2fb6a06c3cef29a3
:meth:`~Mock.assert_has_calls` method. This takes a list of calls (constructed with the :data:`call` object). If that sequence of calls are in :attr:`~Mock.mock_calls` then the assert succeeds. >>> m = MagicMock() >>> m().foo().bar().baz() <MagicMock name='mock().foo().bar().baz()' id='...'> >>> m.one().two().three(...
trusted_official_docs
CPython Docs
:meth:`~Mock.assert_has_calls` method. This takes a list of calls (constructed with the :data:`call` object). If that sequence of calls are in :attr:`~Mock.mock_calls` then the assert succeeds. >>> m = MagicMock() >>> m().foo().bar().baz() <MagicMock name='mock().foo().bar().baz()' id='...'> >>> m.one().two().three(...
:meth:`~Mock.assert_has_calls` method. This takes a list of calls (constructed with the :data:`call` object). If that sequence of calls are in :attr:`~Mock.mock_calls` then the assert succeeds. >>> m = MagicMock() >>> m().foo().bar().baz() <MagicMock name='mock().foo().bar().baz()' id='...'> >>> m.one().two().three(...
python, official-docs, cpython, P0
Local_Trusted_Corpus
eb22e211-283e-4f64-8238-22ff63c7b949
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,416
supabase-export-v2
cb9d23a0830f690b
Using side_effect to return per file content ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :func:`mock_open` is used to patch :func:`open` method. :attr:`~Mock.side_effect` can be used to return a new Mock object per call. This can be used to return different contents per file stored in a dictionary::
trusted_official_docs
CPython Docs
Using side_effect to return per file content ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :func:`mock_open` is used to patch :func:`open` method. :attr:`~Mock.side_effect` can be used to return a new Mock object per call. This can be used to return different contents per file stored in a dictionary::
Using side_effect to return per file content ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :func:`mock_open` is used to patch :func:`open` method. :attr:`~Mock.side_effect` can be used to return a new Mock object per call. This can be used to return different contents per file stored in a dictionary::
python, official-docs, cpython, P0
Local_Trusted_Corpus
ec20d13e-90ef-4367-b170-3bf93bdf85f8
CPython Docs
file://datasets/cpython/Doc/library/unittest.mock-examples.rst
unknown
7bcbcebe-cc8a-441d-b05d-03531e5ebd16
14,489
supabase-export-v2
2d508dabc8d59832
setUp(self): ... self.patcher = patch('mymodule.foo') ... self.mock_foo = self.patcher.start() ... ... def test_foo(self): ... self.assertIs(mymodule.foo, self.mock_foo) ... ... def tearDown(self): ... self.patcher.stop() ... >>> MyTest('test_foo').run() If you use this technique you must ensure that the patching is "u...
trusted_official_docs
CPython Docs
setUp(self): ... self.patcher = patch('mymodule.foo') ... self.mock_foo = self.patcher.start() ... ... def test_foo(self): ... self.assertIs(mymodule.foo, self.mock_foo) ... ... def tearDown(self): ... self.patcher.stop() ... >>> MyTest('test_foo').run() If you use this technique you must ensure that the patching is "u...
setUp(self): ... self.patcher = patch('mymodule.foo') ... self.mock_foo = self.patcher.start() ... ... def test_foo(self): ... self.assertIs(mymodule.foo, self.mock_foo) ... ... def tearDown(self): ... self.patcher.stop() ... >>> MyTest('test_foo').run() If you use this technique you must ensure that the patching is "u...
python, official-docs, cpython, P0
Local_Trusted_Corpus