Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 35266f7

Browse files
committed
Merge
2 parents 1a48b9d + f575230 commit 35266f7

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Doc/library/unittest.mock.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,25 @@ will raise an `AttributeError`.
887887
AttributeError: f
888888

889889

890+
Mock names and the name attribute
891+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
892+
893+
Since "name" is an argument to the :class:`Mock` constructor, if you want your
894+
mock object to have a "name" attribute you can't just pass it in at creation
895+
time. There are two alternatives. One option is to use
896+
:meth:`~Mock.configure_mock`::
897+
898+
>>> mock = MagicMock()
899+
>>> mock.configure_mock(name='my_name')
900+
>>> mock.name
901+
'my_name'
902+
903+
A simpler option is to simply set the "name" attribute after mock creation::
904+
905+
>>> mock = MagicMock()
906+
>>> mock.name = "foo"
907+
908+
890909
Attaching Mocks as Attributes
891910
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
892911

0 commit comments

Comments
 (0)