| 1 |
.. mockito-python documentation master file, created by |
| 2 |
sphinx-quickstart on Tue Apr 26 14:00:19 2016. |
| 3 |
You can adapt this file completely to your liking, but it should at least |
| 4 |
contain the root `toctree` directive. |
| 5 |
|
| 6 |
.. module:: mockito |
| 7 |
|
| 8 |
Mockito is a spying framework originally based on the Java library with the same name. |
| 9 |
|
| 10 |
.. image:: https://travis-ci.org/kaste/mockito-python.svg?branch=master |
| 11 |
:target: https://travis-ci.org/kaste/mockito-python |
| 12 |
|
| 13 |
|
| 14 |
Install |
| 15 |
------- |
| 16 |
|
| 17 |
.. code-block:: python |
| 18 |
|
| 19 |
pip install mockito |
| 20 |
|
| 21 |
If you already use `pytest`, consider using the plugin `pytest-mockito <https://github.com/kaste/pytest-mockito>`_. |
| 22 |
|
| 23 |
|
| 24 |
Use |
| 25 |
--- |
| 26 |
|
| 27 |
.. code-block:: python |
| 28 |
|
| 29 |
from mockito import when, mock, unstub |
| 30 |
|
| 31 |
when(os.path).exists('/foo').thenReturn(True) |
| 32 |
|
| 33 |
# or: |
| 34 |
import requests # the famous library |
| 35 |
# you actually want to return a Response-like obj, we'll fake it |
| 36 |
response = mock({'status_code': 200, 'text': 'Ok'}) |
| 37 |
when(requests).get(...).thenReturn(response) |
| 38 |
|
| 39 |
# use it |
| 40 |
requests.get('http://google.com/') |
| 41 |
|
| 42 |
# clean up |
| 43 |
unstub() |
| 44 |
|
| 45 |
|
| 46 |
Features |
| 47 |
-------- |
| 48 |
|
| 49 |
Super easy to set up different answers. |
| 50 |
|
| 51 |
:: |
| 52 |
|
| 53 |
# Well, you know the internet |
| 54 |
when(requests).get(...).thenReturn(mock({'status': 501})) \ |
| 55 |
.thenRaise(Timeout("I'm flaky")) \ |
| 56 |
.thenReturn(mock({'status': 200, 'text': 'Ok'})) |
| 57 |
|
| 58 |
State-of-the-art, high-five argument matchers:: |
| 59 |
|
| 60 |
# Use the Ellipsis, if you don't care |
| 61 |
when(deferred).defer(...).thenRaise(Timeout) |
| 62 |
|
| 63 |
# Or **kwargs |
| 64 |
from mockito import kwargs # or KWARGS |
| 65 |
when(requests).get('http://my-api.com/user', **kwargs) |
| 66 |
|
| 67 |
# The usual matchers |
| 68 |
from mockito import ANY, or_, not_ |
| 69 |
number = or_(ANY(int), ANY(float)) |
| 70 |
when(math).sqrt(not_(number)).thenRaise( |
| 71 |
TypeError('argument must be a number')) |
| 72 |
|
| 73 |
No need to `verify` (`assert_called_with`) all the time:: |
| 74 |
|
| 75 |
# Different arguments, different answers |
| 76 |
when(foo).bar(1).thenReturn(2) |
| 77 |
when(foo).bar(2).thenReturn(3) |
| 78 |
|
| 79 |
# but: |
| 80 |
foo.bar(3) # throws immediately: unexpected invocation |
| 81 |
|
| 82 |
# because of that you just know that when |
| 83 |
# you get a `2`, you called it with `1` |
| 84 |
|
| 85 |
|
| 86 |
Signature checking:: |
| 87 |
|
| 88 |
# when stubbing |
| 89 |
when(requests).get() # throws immediately: TypeError url required |
| 90 |
|
| 91 |
# when calling |
| 92 |
request.get(location='http://example.com/') # TypeError |
| 93 |
|
| 94 |
|
| 95 |
Read |
| 96 |
---- |
| 97 |
|
| 98 |
.. toctree:: |
| 99 |
:maxdepth: 1 |
| 100 |
|
| 101 |
walk-through |
| 102 |
recipes |
| 103 |
the-functions |
| 104 |
the-matchers |
| 105 |
Changelog <changes> |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
Report issues, contribute more documentation or give feedback at `Github <https://github.com/kaste/mockito-python>`_! |
| 110 |
|