Skip to main content

Fixtures for testing code that interacts with AWS

Project description

pytest-moto-fixtures

pytest fixtures for testing code that integrates with AWS using moto as a mock.

Install

Install pytest-moto-fixtures package from PyPI:

pip install pytest-moto-fixtures[pytest]

Examples of Use

Code for test:

class Example:
    def __init__(self, sqs_client, queue_url):
        self._sqs_client = sqs_client
        self._queue_url = queue_url

    def run(self, values):
        total = 0
        for value in values:
            total += value
            self._sqs_client.send_message(QueueUrl=self._queue_url, MessageBody=f'Value processed: {value}')
        return total

Test example using fixture:

from random import randint

def test_example_with_fixture(sqs_queue):
    values = [randint(1, 10) for _ in range(randint(3, 10))]
    expected = sum(values)

    sut = Example(sqs_client=sqs_queue.client, queue_url=sqs_queue.url)
    returned = sut.run(values)

    assert returned == expected
    assert len(sqs_queue) == len(values)
    for value, message in zip(values, sqs_queue):
        assert message['Body'] == f'Value processed: {value}'

Test example using context:

from random import randint
from pytest_moto_fixtures.clients.sqs import sqs_create_queue

def test_example_with_context(sqs_client):
    values = [randint(1, 10) for _ in range(randint(3, 10))]
    expected = sum(values)

    with sqs_create_queue(sqs_client=sqs_client, name='my-queue') as sqs_queue:
        sut = Example(sqs_client=sqs_client, queue_url=sqs_queue.url)
        returned = sut.run(values)

        assert returned == expected
        assert len(sqs_queue) == len(values)
        for value, message in zip(values, sqs_queue):
            assert message['Body'] == f'Value processed: {value}'

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_moto_fixtures-0.0.1a2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

pytest_moto_fixtures-0.0.1a2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_moto_fixtures-0.0.1a2.tar.gz.

File metadata

File hashes

Hashes for pytest_moto_fixtures-0.0.1a2.tar.gz
Algorithm Hash digest
SHA256 0e6d9c79dd908f0bd7df8c7b4d5335ce571f58b19c9bdb4b3b2445c5f4745384
MD5 8d27bd0e7e46c90eaec81133cdebc41d
BLAKE2b-256 cb22fbcdc805cf84bbd864f95dea08b20801666943686cef2ae936963e580989

See more details on using hashes here.

File details

Details for the file pytest_moto_fixtures-0.0.1a2-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_moto_fixtures-0.0.1a2-py3-none-any.whl
Algorithm Hash digest
SHA256 557165f6bfe0161ff7c57c474c143b95bda634457a0ba8be5843929a35f2300f
MD5 655bd5979c4a4617216c703709bef1fc
BLAKE2b-256 1b2c3901317a505a927ca814ac3d03a464dc7b40670bbe620d5f80eebfc5c75f

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