Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
88ba360c
Kaydet (Commit)
88ba360c
authored
Tem 23, 2015
tarafından
Robert Collins
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
Issue #21750: Further fixup to be styled like other mock APIs.
üst
4f4913b3
ca647ef6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
42 deletions
+73
-42
unittest.mock.rst
Doc/library/unittest.mock.rst
+5
-3
mock.py
Lib/unittest/mock.py
+43
-36
testmock.py
Lib/unittest/test/testmock/testmock.py
+23
-1
testwith.py
Lib/unittest/test/testmock/testwith.py
+2
-2
No files found.
Doc/library/unittest.mock.rst
Dosyayı görüntüle @
88ba360c
...
@@ -2037,9 +2037,11 @@ mock_open
...
@@ -2037,9 +2037,11 @@ mock_open
:meth:`~io.IOBase.readline`, and :meth:`~io.IOBase.readlines` methods
:meth:`~io.IOBase.readline`, and :meth:`~io.IOBase.readlines` methods
of the file handle to return. Calls to those methods will take data from
of the file handle to return. Calls to those methods will take data from
*read_data* until it is depleted. The mock of these methods is pretty
*read_data* until it is depleted. The mock of these methods is pretty
simplistic. If you need more control over the data that you are feeding to
simplistic: every time the *mock* is called, the *read_data* is rewound to
the tested code you will need to customize this mock for yourself.
the start. If you need more control over the data that you are feeding to
*read_data* is an empty string by default.
the tested code you will need to customize this mock for yourself. When that
is insufficient, one of the in-memory filesystem packages on `PyPI
<https://pypi.python.org/pypi>`_ can offer a realistic filesystem for testing.
Using :func:`open` as a context manager is a great way to ensure your file handles
Using :func:`open` as a context manager is a great way to ensure your file handles
are closed properly and is becoming common::
are closed properly and is becoming common::
...
...
Lib/unittest/mock.py
Dosyayı görüntüle @
88ba360c
...
@@ -2299,6 +2299,24 @@ def mock_open(mock=None, read_data=''):
...
@@ -2299,6 +2299,24 @@ def mock_open(mock=None, read_data=''):
`read_data` is a string for the `read` methoddline`, and `readlines` of the
`read_data` is a string for the `read` methoddline`, and `readlines` of the
file handle to return. This is an empty string by default.
file handle to return. This is an empty string by default.
"""
"""
def
_readlines_side_effect
(
*
args
,
**
kwargs
):
if
handle
.
readlines
.
return_value
is
not
None
:
return
handle
.
readlines
.
return_value
return
list
(
_state
[
0
])
def
_read_side_effect
(
*
args
,
**
kwargs
):
if
handle
.
read
.
return_value
is
not
None
:
return
handle
.
read
.
return_value
return
''
.
join
(
_state
[
0
])
def
_readline_side_effect
():
if
handle
.
readline
.
return_value
is
not
None
:
while
True
:
yield
handle
.
readline
.
return_value
for
line
in
_state
[
0
]:
yield
line
global
file_spec
global
file_spec
if
file_spec
is
None
:
if
file_spec
is
None
:
import
_io
import
_io
...
@@ -2307,42 +2325,31 @@ def mock_open(mock=None, read_data=''):
...
@@ -2307,42 +2325,31 @@ def mock_open(mock=None, read_data=''):
if
mock
is
None
:
if
mock
is
None
:
mock
=
MagicMock
(
name
=
'open'
,
spec
=
open
)
mock
=
MagicMock
(
name
=
'open'
,
spec
=
open
)
def
make_handle
(
*
args
,
**
kwargs
):
handle
=
MagicMock
(
spec
=
file_spec
)
# Arg checking is handled by __call__
handle
.
__enter__
.
return_value
=
handle
def
_readlines_side_effect
(
*
args
,
**
kwargs
):
if
handle
.
readlines
.
return_value
is
not
None
:
_state
=
[
_iterate_read_data
(
read_data
),
None
]
return
handle
.
readlines
.
return_value
return
list
(
_data
)
handle
.
write
.
return_value
=
None
handle
.
read
.
return_value
=
None
def
_read_side_effect
(
*
args
,
**
kwargs
):
handle
.
readline
.
return_value
=
None
if
handle
.
read
.
return_value
is
not
None
:
handle
.
readlines
.
return_value
=
None
return
handle
.
read
.
return_value
return
''
.
join
(
_data
)
handle
.
read
.
side_effect
=
_read_side_effect
_state
[
1
]
=
_readline_side_effect
()
def
_readline_side_effect
():
handle
.
readline
.
side_effect
=
_state
[
1
]
if
handle
.
readline
.
return_value
is
not
None
:
handle
.
readlines
.
side_effect
=
_readlines_side_effect
while
True
:
yield
handle
.
readline
.
return_value
def
reset_data
(
*
args
,
**
kwargs
):
for
line
in
_data
:
_state
[
0
]
=
_iterate_read_data
(
read_data
)
yield
line
if
handle
.
readline
.
side_effect
==
_state
[
1
]:
# Only reset the side effect if the user hasn't overridden it.
handle
=
MagicMock
(
spec
=
file_spec
)
_state
[
1
]
=
_readline_side_effect
()
handle
.
__enter__
.
return_value
=
handle
handle
.
readline
.
side_effect
=
_state
[
1
]
return
DEFAULT
_data
=
_iterate_read_data
(
read_data
)
mock
.
side_effect
=
reset_data
handle
.
write
.
return_value
=
None
mock
.
return_value
=
handle
handle
.
read
.
return_value
=
None
handle
.
readline
.
return_value
=
None
handle
.
readlines
.
return_value
=
None
handle
.
read
.
side_effect
=
_read_side_effect
handle
.
readline
.
side_effect
=
_readline_side_effect
()
handle
.
readlines
.
side_effect
=
_readlines_side_effect
_check_and_set_parent
(
mock
,
handle
,
None
,
'()'
)
return
handle
mock
.
side_effect
=
make_handle
return
mock
return
mock
...
...
Lib/unittest/test/testmock/testmock.py
Dosyayı görüntüle @
88ba360c
import
copy
import
copy
import
sys
import
sys
import
tempfile
import
unittest
import
unittest
from
unittest.test.testmock.support
import
is_instance
from
unittest.test.testmock.support
import
is_instance
...
@@ -1374,8 +1375,29 @@ class MockTest(unittest.TestCase):
...
@@ -1374,8 +1375,29 @@ class MockTest(unittest.TestCase):
def
test_mock_open_reuse_issue_21750
(
self
):
def
test_mock_open_reuse_issue_21750
(
self
):
mocked_open
=
mock
.
mock_open
(
read_data
=
'data'
)
mocked_open
=
mock
.
mock_open
(
read_data
=
'data'
)
f1
=
mocked_open
(
'a-name'
)
f1
=
mocked_open
(
'a-name'
)
f1_data
=
f1
.
read
()
f2
=
mocked_open
(
'another-name'
)
f2
=
mocked_open
(
'another-name'
)
self
.
assertEqual
(
f1
.
read
(),
f2
.
read
())
f2_data
=
f2
.
read
()
self
.
assertEqual
(
f1_data
,
f2_data
)
def
test_mock_open_write
(
self
):
# Test exception in file writing write()
mock_namedtemp
=
mock
.
mock_open
(
mock
.
MagicMock
(
name
=
'JLV'
))
with
mock
.
patch
(
'tempfile.NamedTemporaryFile'
,
mock_namedtemp
):
mock_filehandle
=
mock_namedtemp
.
return_value
mock_write
=
mock_filehandle
.
write
mock_write
.
side_effect
=
OSError
(
'Test 2 Error'
)
def
attempt
():
tempfile
.
NamedTemporaryFile
()
.
write
(
'asd'
)
self
.
assertRaises
(
OSError
,
attempt
)
def
test_mock_open_alter_readline
(
self
):
mopen
=
mock
.
mock_open
(
read_data
=
'foo
\n
barn'
)
mopen
.
return_value
.
readline
.
side_effect
=
lambda
*
args
:
'abc'
first
=
mopen
()
.
readline
()
second
=
mopen
()
.
readline
()
self
.
assertEqual
(
'abc'
,
first
)
self
.
assertEqual
(
'abc'
,
second
)
def
test_mock_parents
(
self
):
def
test_mock_parents
(
self
):
for
Klass
in
Mock
,
MagicMock
:
for
Klass
in
Mock
,
MagicMock
:
...
...
Lib/unittest/test/testmock/testwith.py
Dosyayı görüntüle @
88ba360c
...
@@ -141,6 +141,7 @@ class TestMockOpen(unittest.TestCase):
...
@@ -141,6 +141,7 @@ class TestMockOpen(unittest.TestCase):
def
test_mock_open_context_manager
(
self
):
def
test_mock_open_context_manager
(
self
):
mock
=
mock_open
()
mock
=
mock_open
()
handle
=
mock
.
return_value
with
patch
(
'
%
s.open'
%
__name__
,
mock
,
create
=
True
):
with
patch
(
'
%
s.open'
%
__name__
,
mock
,
create
=
True
):
with
open
(
'foo'
)
as
f
:
with
open
(
'foo'
)
as
f
:
f
.
read
()
f
.
read
()
...
@@ -148,8 +149,7 @@ class TestMockOpen(unittest.TestCase):
...
@@ -148,8 +149,7 @@ class TestMockOpen(unittest.TestCase):
expected_calls
=
[
call
(
'foo'
),
call
()
.
__enter__
(),
call
()
.
read
(),
expected_calls
=
[
call
(
'foo'
),
call
()
.
__enter__
(),
call
()
.
read
(),
call
()
.
__exit__
(
None
,
None
,
None
)]
call
()
.
__exit__
(
None
,
None
,
None
)]
self
.
assertEqual
(
mock
.
mock_calls
,
expected_calls
)
self
.
assertEqual
(
mock
.
mock_calls
,
expected_calls
)
# mock_open.return_value is no longer static, because
self
.
assertIs
(
f
,
handle
)
# readline support requires that it mutate state
def
test_mock_open_context_manager_multiple_times
(
self
):
def
test_mock_open_context_manager_multiple_times
(
self
):
mock
=
mock_open
()
mock
=
mock_open
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment