# 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:
```console
=========================================================================================================== 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:
```python
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:
```python
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
```
Raw data
{
"_id": null,
"home_page": "https://github.com/phillipuniverse/pytest-aioboto3",
"name": "pytest-aioboto3",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "pytest, aioboto3, moto",
"author": "Shipwell Engineering",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/00/92/0bc9f99e4f6454bf0147a61b3efe3ea0d8a8e5c8da72e58b96fc13690482/pytest_aioboto3-0.4.0.tar.gz",
"platform": null,
"description": "# aioboto3 mocking with Pytest and Moto\n\nThis 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`.\n\nMost 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:\n\n- Only initializes a moto server for S3\n- Includes a sustainable pattern for adding additional AWS mocked services\n- Fully type-hinted\n\n## Running the example test\n\n- Poetry 1.6+\n- Python 3.10+ (although probably Python 3.7+ will work)\n\n```\npoetry install\npoetry run pytest\n```\n\nExample output:\n\n```console\n=========================================================================================================== test session starts ===========================================================================================================\nplatform darwin -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0\nrootdir: /Users/phillip/pytest-aioboto3\nconfigfile: pyproject.toml\ntestpaths: tests\nplugins: asyncio-0.21.1, aioboto3-0.1.0\nasyncio: mode=Mode.AUTO\ncollected 1 item\n\ntests/test_s3.py . [100%]\n\n============================================================================================================ 1 passed in 0.94s ============================================================================================================\n```\n\n## Installation\n\n`pip install pytest-aioboto3`\n\n## Usage\n\nInject the included `aioboto3_s3_client` fixture:\n```python\nasync def test_aio_aws_bucket_access(aioboto3_s3_client: S3Client) -> None:\n resp = await aioboto3_s3_client.list_buckets()\n ...\n```\n\nOr create a new `Session` yourself by injecting the `moto_patch_session` fixture:\n```python\nasync def test_some_s3_thing(moto_patch_session: None) -> None:\n session = aioboto3.Session(region_name=\"us-east-1\")\n async with session.client(\"s3\", region_name=\"us-east-1\") as client: # type: S3Client\n yield client\n```\n",
"bugtrack_url": null,
"license": "WTFPL",
"summary": "Aioboto3 Pytest with Moto",
"version": "0.4.0",
"project_urls": {
"Bug Tracker": "https://github.com/phillipuniverse/pytest-aioboto3/issues",
"Homepage": "https://github.com/phillipuniverse/pytest-aioboto3",
"Repository": "https://github.com/phillipuniverse/pytest-aioboto3"
},
"split_keywords": [
"pytest",
" aioboto3",
" moto"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a604da23da79726841feb3bc39796686f7aa16d45093b4cc749e90fc033347ca",
"md5": "13b6c2e577b3c048e2213c44bfa3f759",
"sha256": "c067b4e052bde91e7561ede14ca40d05f2140ce04904660f98f74816387c2c9c"
},
"downloads": -1,
"filename": "pytest_aioboto3-0.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "13b6c2e577b3c048e2213c44bfa3f759",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 5898,
"upload_time": "2024-11-26T13:19:15",
"upload_time_iso_8601": "2024-11-26T13:19:15.444020Z",
"url": "https://files.pythonhosted.org/packages/a6/04/da23da79726841feb3bc39796686f7aa16d45093b4cc749e90fc033347ca/pytest_aioboto3-0.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "00920bc9f99e4f6454bf0147a61b3efe3ea0d8a8e5c8da72e58b96fc13690482",
"md5": "e2052fde64ef6c325b96ca34290c5aef",
"sha256": "71533780bed3f4dcb78da223ba6498db6d7b6970f0def91adb331ad6e3626ac4"
},
"downloads": -1,
"filename": "pytest_aioboto3-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "e2052fde64ef6c325b96ca34290c5aef",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 4758,
"upload_time": "2024-11-26T13:19:16",
"upload_time_iso_8601": "2024-11-26T13:19:16.273553Z",
"url": "https://files.pythonhosted.org/packages/00/92/0bc9f99e4f6454bf0147a61b3efe3ea0d8a8e5c8da72e58b96fc13690482/pytest_aioboto3-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-26 13:19:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "phillipuniverse",
"github_project": "pytest-aioboto3",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pytest-aioboto3"
}