Skip to main content

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

Metadata

Release files for pytest-aioboto3 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-aioboto3 0.6.0
File Size Uploaded Provenance
pytest_aioboto3-0.6.0.tar.gz 5.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-aioboto3 0.6.0
File Interpreter ABI Platform
pytest_aioboto3-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / pytest_aioboto3-0.6.0.tar.gz

Download URL pytest_aioboto3-0.6.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
21c787b393a811d0773d755f762c07a5078d9b648e2b8bc73eb67763dd8b3d5c
BLAKE2b-256 checksum
How to use checksums
9a7b340d045faed9b7579ff0cb7cf35c485bbe4b06702a17b00efa7ac97fdfaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via poetry/2.0.1 CPython/3.10.16 Linux/6.8.0-1017-azure

Release files / pytest_aioboto3-0.6.0-py3-none-any.whl

Download URL pytest_aioboto3-0.6.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4657e1e9a3fa8b3eaf1d216dbfae9ce436e2fcfb1931987408d2d37788bf443e
BLAKE2b-256 checksum
How to use checksums
c1a3dbe02c69ffab277a5175568d3ab11ea5719e2746cac1c6b36ea18b201bba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via poetry/2.0.1 CPython/3.10.16 Linux/6.8.0-1017-azure

Security for pytest-aioboto3 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page