Coder Social home page Coder Social logo

pytest-aioboto3's Introduction

aioboto3 mocking with Pytest and Moto

This is a Pytest plugin that sets up a Moto background service and patch the aioboto3 session to forward all calls to it. This is designed to work against any calls that create an aiobotocore3.Session() or botocore3.Session() automatically. This means you should not have to change any implementation code for how you are managing the aioboto3 Session.

Most of the code for this came from the aioboto3 tests themselves at https://github.com/terrycain/aioboto3/blob/92a7a9b8a32615ab6a9ea51ef360475ede94bb1f/tests/mock_server.py. Divergence in this gist:

  • Only initializes a moto server for S3
  • Includes a sustainable pattern for adding additional AWS mocked services
  • Fully type-hinted

Running the example test

  • Poetry 1.1+
  • Python 3.9+ (although probably Python 3.7+ will work)
poetry install
poetry run pytest

Example output:

================================================================================================= test session starts =================================================================================================
platform darwin -- Python 3.9.11, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/phillip/aioboto3-testing, configfile: pyproject.toml, testpaths: tests
plugins: asyncio-0.18.3
asyncio: mode=auto
collected 1 item

tests/test_s3.py .                                                                                                                                                                                              [100%]

================================================================================================== 1 passed in 0.96s ==================================================================================================

Installation

This project is not yet hosted on PyPI. To install it, you can use the git url for this repo:

pip install git+https://github.com/phillipuniverse/pytest-aioboto3

Usage

Inject the included aioboto3_s3_client fixture:

async def test_aio_aws_bucket_access(aioboto3_s3_client: S3Client) -> None:
    resp = await aioboto3_s3_client.list_buckets()
    ...

Or create a new Session yourself by injecting the moto_patch_session fixture:

async def test_some_s3_thing(moto_patch_session: None) -> None:
    session = aioboto3.Session(region_name="us-east-1")
    async with session.client("s3", region_name="us-east-1") as client:  # type: S3Client
        yield client

pytest-aioboto3's People

Contributors

ocaballeror avatar phillipuniverse avatar arnaldojvg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.