DynamoDB fixtures for pytest
Project description
pytest-dynamodb
DynamoDB fixtures for pytest.
What is this?
pytest-dynamodb is a pytest plugin for tests that need a running DynamoDB instance. It provides fixtures for both a managed local process and a connection to an already running instance.
Quickstart: first test
Install the plugin and your test dependencies.
Download and unpack DynamoDB Local (see AWS docs): https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html Place extracted files in dynamodb_dir (default: /tmp/dynamodb), or choose another location and pass it via --dynamodb-dir when running tests.
Make sure Java is available in your environment (DynamoDB Local runs as a JAR).
Write a test that uses the built-in dynamodb fixture:
import uuid
def test_can_put_and_get_item(dynamodb):
table = dynamodb.create_table(
TableName="Test",
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
ProvisionedThroughput={"ReadCapacityUnits": 10, "WriteCapacityUnits": 10},
)
_id = str(uuid.uuid4())
table.put_item(Item={"id": _id, "test_key": "test_value"})
item = table.get_item(Key={"id": _id})
assert item["Item"]["test_key"] == "test_value"
Run tests:
pytest
For a full example, see tests/test_dynamodb.py.
How to use
The plugin contains three fixtures:
dynamodb - function-scoped DynamoDBServiceResource fixture.
dynamodb_proc - session-scoped fixture that starts DynamoDB Local on first use and stops it at the end of the test session.
dynamodb_noproc - session-scoped fixture that connects to an externally managed DynamoDB instance (for example, Docker).
When to use which fixture:
Use dynamodb (default) when tests should manage a local process automatically.
Use dynamodb_noproc when DynamoDB is already running elsewhere and lifecycle is managed outside pytest.
Simply include one of these fixtures in your test fixture list.
You can also create additional client and process fixtures:
from pytest_dynamodb import factories
dynamodb_my_proc = factories.dynamodb_proc(port=None, delay=True)
dynamodb_my = factories.dynamodb("dynamodb_my_proc")
from pytest_dynamodb import factories
dynamodb_my_noproc = factories.dynamodb_noproc(host="dynamodb", port=8088)
dynamodb_my = factories.dynamodb("dynamodb_my_noproc")
Configuration
You can define settings in three ways:
Fixture factory argument
Command line option
Configuration option in pytest.ini
Precedence order:
Fixture factory argument
Command line option
pytest.ini option
DynamoDB option |
Fixture factory argument |
Command line option |
pytest.ini option |
Default |
|---|---|---|---|---|
Path to DynamoDB JAR directory |
dynamodb_dir |
–dynamodb-dir |
dynamodb_dir |
/tmp/dynamodb |
Host |
host |
–dynamodb-host |
dynamodb_host |
127.0.0.1 |
Port |
port |
–dynamodb-port |
dynamodb_port |
random |
AWS Access Key |
access_key |
–dynamodb-aws_access_key |
dynamodb_aws_access_key |
fakeMyKeyId |
AWS Secret Key |
secret_key |
–dynamodb-aws_secret_key |
dynamodb_aws_secret_key |
fakeSecretAccessKey |
AWS Region |
region |
–dynamodb-aws_region |
dynamodb_aws_region |
us-west-1 |
delay |
–dynamodb-delay |
dynamodb_delay |
false |
Example usage:
Pass as fixture factory argument:
from pytest_dynamodb import factories dynamodb_proc = factories.dynamodb_proc(port=8888)Use command line option:
pytest tests --dynamodb-port=8888Set in pytest.ini:
[pytest] dynamodb_port = 8888
Known issues
Parallel runs with a fixed --dynamodb-port may fail because workers contend for the same port.
Package resources
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_dynamodb-3.0.0.tar.gz.
File metadata
- Download URL: pytest_dynamodb-3.0.0.tar.gz
- Upload date:
- Size: 30.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88a494e03ab256d9a62c0a540885ef73b5c3e1fcd6b6651526bfdd4000e4a9bc
|
|
| MD5 |
294c9224086f753a43119607b615c654
|
|
| BLAKE2b-256 |
711623ac2938164fd6de4975668d2a8a81b1a7e42ce0a50482e14bcd430e15a4
|
File details
Details for the file pytest_dynamodb-3.0.0-py3-none-any.whl.
File metadata
- Download URL: pytest_dynamodb-3.0.0-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a0c251a66d147e58dc10a30c2bc1d081ab7557c3408f1351b08333a0d018091
|
|
| MD5 |
a48b3a47fad0f99346860fa2e58da526
|
|
| BLAKE2b-256 |
70895272000b115633be421650a5521cc8d5b80e6f37c335bef57a10bd4cbf5f
|