2017-03-15 03:20:17 +00:00
.. _getting_started:
2015-07-27 15:44:41 +00:00
=========================
Getting Started with Moto
=========================
Installing Moto
---------------
You can use `` pip `` to install the latest released version of `` moto `` ::
pip install moto
If you want to install `` moto `` from source::
git clone git://github.com/spulec/moto.git
cd moto
python setup.py install
Moto usage
----------
2018-04-20 00:25:28 +00:00
For example, we have the following code we want to test:
2015-07-27 15:44:41 +00:00
.. sourcecode :: python
2020-02-08 17:49:54 +00:00
import boto3
2015-07-27 15:44:41 +00:00
class MyModel(object):
def __init__(self, name, value):
self.name = name
self.value = value
def save(self):
2020-02-08 17:49:54 +00:00
s3 = boto3.client('s3', region_name='us-east-1')
s3.put_object(Bucket='mybucket', Key=self.name, Body=self.value)
2015-07-27 15:44:41 +00:00
2018-04-20 00:25:28 +00:00
There are several ways to do this, but you should keep in mind that Moto creates a full, blank environment.
2015-07-27 15:44:41 +00:00
Decorator
~~~~~~~~~
2018-04-20 00:25:28 +00:00
With a decorator wrapping, all the calls to S3 are automatically mocked out.
2015-07-27 15:44:41 +00:00
.. sourcecode :: python
2020-02-08 17:49:54 +00:00
import boto3
2015-07-27 15:44:41 +00:00
from moto import mock_s3
from mymodule import MyModel
@mock_s3
def test_my_model_save():
2020-02-08 17:49:54 +00:00
conn = boto3.resource('s3', region_name='us-east-1')
2015-07-27 15:44:41 +00:00
# We need to create the bucket since this is all in Moto's 'virtual' AWS account
2020-02-08 17:49:54 +00:00
conn.create_bucket(Bucket='mybucket')
2015-07-27 15:44:41 +00:00
model_instance = MyModel('steve', 'is awesome')
model_instance.save()
2020-02-08 17:49:54 +00:00
body = conn.Object('mybucket', 'steve').get()[
'Body'].read().decode("utf-8")
assert body == 'is awesome'
2015-07-27 15:44:41 +00:00
Context manager
~~~~~~~~~~~~~~~
2018-04-20 00:25:28 +00:00
Same as the Decorator, every call inside the `` with `` statement is mocked out.
2015-07-27 15:44:41 +00:00
.. sourcecode :: python
def test_my_model_save():
with mock_s3():
2020-02-08 17:49:54 +00:00
conn = boto3.resource('s3', region_name='us-east-1')
conn.create_bucket(Bucket='mybucket')
2015-07-27 15:44:41 +00:00
model_instance = MyModel('steve', 'is awesome')
model_instance.save()
2020-02-08 17:49:54 +00:00
body = conn.Object('mybucket', 'steve').get()[
'Body'].read().decode("utf-8")
assert body == 'is awesome'
2015-07-27 15:44:41 +00:00
Raw
~~~
2018-04-20 00:25:28 +00:00
You can also start and stop the mocking manually.
2015-07-27 15:44:41 +00:00
.. sourcecode :: python
def test_my_model_save():
mock = mock_s3()
mock.start()
2020-02-08 17:49:54 +00:00
conn = boto3.resource('s3', region_name='us-east-1')
conn.create_bucket(Bucket='mybucket')
2015-07-27 15:44:41 +00:00
model_instance = MyModel('steve', 'is awesome')
model_instance.save()
2020-02-08 17:49:54 +00:00
body = conn.Object('mybucket', 'steve').get()[
'Body'].read().decode("utf-8")
assert body == 'is awesome'
2015-07-27 15:44:41 +00:00
mock.stop()
Stand-alone server mode
~~~~~~~~~~~~~~~~~~~~~~~
2018-04-20 00:25:28 +00:00
Moto also comes with a stand-alone server allowing you to mock out an AWS HTTP endpoint. For testing purposes, it's extremely useful even if you don't use Python.
2015-07-27 15:44:41 +00:00
.. sourcecode :: bash
$ moto_server ec2 -p3000
2016-09-01 11:14:38 +00:00
* Running on http://127.0.0.1:3000/
2015-07-27 15:44:41 +00:00
2018-04-20 00:25:28 +00:00
However, this method isn't encouraged if you're using `` boto `` , the best solution would be to use a decorator method.