Skip to main content

Aioboto3 Pytest with Moto

Project description

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.6+
  • Python 3.10+ (although probably Python 3.7+ will work)
poetry install
poetry run pytest

Example output:

=========================================================================================================== test session starts ===========================================================================================================
platform darwin -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0
rootdir: /Users/phillip/pytest-aioboto3
configfile: pyproject.toml
testpaths: tests
plugins: asyncio-0.21.1, aioboto3-0.1.0
asyncio: mode=Mode.AUTO
collected 1 item

tests/test_s3.py .                                                                                                                                                                                                                  [100%]

============================================================================================================ 1 passed in 0.94s ============================================================================================================

Installation

pip install 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_aioboto3-0.5.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

pytest_aioboto3-0.5.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_aioboto3-0.5.0.tar.gz.

File metadata

  • Download URL: pytest_aioboto3-0.5.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.4 CPython/3.10.15 Linux/6.5.0-1025-azure

File hashes

Hashes for pytest_aioboto3-0.5.0.tar.gz
Algorithm Hash digest
SHA256 ef157087a8a879ba18f07aa90e1f45d33cae4ce241c93e826a64fec87173950e
MD5 fb28856e0d2e4e9479bb45364d8bbece
BLAKE2b-256 8f9372ef4c27f79a5e75e97f5595c48cb2dbf9be781f48dad421c6a3b786a305

See more details on using hashes here.

File details

Details for the file pytest_aioboto3-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_aioboto3-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.4 CPython/3.10.15 Linux/6.5.0-1025-azure

File hashes

Hashes for pytest_aioboto3-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ecde3765e793200ea1904d4f54fce01441e7d52870a5ccd83e131d8019048f96
MD5 9239a9517e94518a7da75f4dc207a515
BLAKE2b-256 5e7cf595148799aa96554d77210faaa5d176c1e18f13acc0061d887e4b425bc4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page