Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
fuchsia.googlesource.com-third_party-python-testing-cabal-mock
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fuchsia-mirror
fuchsia.googlesource.com-third_party-python-testing-cabal-mock
Commits
eddeeaf9
Commit
eddeeaf9
authored
13 years ago
by
Michael Foord
Browse files
Options
Downloads
Patches
Plain Diff
New example
parent
7630a805
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
README.txt
+6
-0
6 additions, 0 deletions
README.txt
docs/examples.txt
+48
-0
48 additions, 0 deletions
docs/examples.txt
with
54 additions
and
0 deletions
README.txt
+
6
−
0
View file @
eddeeaf9
...
@@ -6,6 +6,12 @@ needed attributes in the normal way.
...
@@ -6,6 +6,12 @@ needed attributes in the normal way.
mock is tested on Python versions 2.4-2.7 and Python 3.
mock is tested on Python versions 2.4-2.7 and Python 3.
.. note::
You can install the latest alpha version of `mock (0.8 alpha 1)
<http://www.voidspace.org.uk/downloads/mock-0.8.0alpha1.tar.gz#egg=mock-dev>`_
with `pip install mock==dev`.
The mock module also provides utility functions / objects to assist with
The mock module also provides utility functions / objects to assist with
testing, particularly monkey patching.
testing, particularly monkey patching.
...
...
This diff is collapsed.
Click to expand it.
docs/examples.txt
+
48
−
0
View file @
eddeeaf9
...
@@ -245,6 +245,54 @@ function in the module under test instead of patching it globally:
...
@@ -245,6 +245,54 @@ function in the module under test instead of patching it globally:
>>> file_handle.write.assert_called_with('something')
>>> file_handle.write.assert_called_with('something')
Mocking a Generator Method
==========================
A generator method is a method that uses the `yield statement
<http://docs.python.org/reference/simple_stmts.html#the-yield-statement>`_ to
return a series of values when iterated over.
A generator method is called to return the generator object. It is the generator
object that is then iterated over. The protocol method for iteration is
`__iter__ <http://docs.python.org/library/stdtypes.html#container.__iter__>`_,
so we can mock this using a `MagicMock`.
Here's an example class:
.. doctest::
>>> class Foo(object):
... def iter(self):
... for i in [1, 2, 3]:
... yield i
...
>>> foo = Foo()
>>> list(foo.iter())
[1, 2, 3]
To configure the values returned from the iteration (implicit in the call to
`list`), we need to configure the iterator returned by the call to `foo.iter()`.
.. doctest::
>>> values = [1, 2, 3]
>>> mock_foo = MagicMock()
>>> iterable = mock_foo.iter.return_value
>>> iterator = iter(values)
>>> iterable.__iter__.return_value = iterator
>>> list(mock_foo.iter())
[1, 2, 3]
The above example is done step-by-step. The shorter version is:
.. doctest::
>>> mock_foo = MagicMock()
>>> mock_foo.iter.return_value.__iter__.return_value = iter([1, 2, 3])
>>> list(mock_foo.iter())
[1, 2, 3]
Applying the same patch to every test method
Applying the same patch to every test method
============================================
============================================
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment