Automatically mocks resources from serverless.yml in pytest using moto.
Project description
pytest-serverless
Automatically mocks resources defined in serverless.yml file using moto and uses them in pytest.
This way you can focus on writing tests rather than defining enormous list of fixtures.
| master | PyPI | Python | pytest | Licence |
|---|---|---|---|---|
6.2 |
Pre installation requirements
serverlessinstalledpytestinstalled
Installation
pip install pytest-serverless
Usage
Assuming your serverless.yml file looks like:
service: my-microservice
resources:
Resources:
TableA:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Delete
Properties:
TableName: ${self:service}.my-table
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: company_id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: company_id
KeySchema:
- AttributeName: company_id
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 10
WriteCapacityUnits: 30
ProvisionedThroughput:
ReadCapacityUnits: 10
WriteCapacityUnits: 30
Just mark your test with @pytest.mark.usefixtures("serverless") and pytest-serverless will automatically create my-microservice.my-table dynamodb table.
import boto3
import pytest
@pytest.mark.usefixtures("serverless")
def test():
table = boto3.resource("dynamodb").Table("my-microservice.my-table")
count_of_items = len(table.scan()["Items"])
assert count_of_items == 0
You can use a custom serverless file path setting the envionmnet variable SERVERLESS_FILE_PATH.
$ export SERVERLESS_FILE_PATH=/path/to/serverless.yml
You can use choose both sls or serverless command to run, settings the environment variable SERVERLESS_COMMAND. It will only accpets sls or serverless values.
$ export SERVERLESS_FILE_PATH=sls
$ export SERVERLESS_FILE_PATH=serverless
Supported resources
AWS::DynamoDB::Table
AWS::SQS::Queue
AWS::SNS::Topic
AWS::S3::Bucket
AWS::KMS::Key
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-serverless-0.15.0.tar.gz.
File metadata
- Download URL: pytest-serverless-0.15.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.6.9 Darwin/20.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
799a2409a7fb62151f91705228708424f5df631c8b9335fc0cf9db80bfa4a03f
|
|
| MD5 |
224d6ea9cf5958c4a1783933ef13e69c
|
|
| BLAKE2b-256 |
1045c0a98f50235a8a2289bd20000712a8e33bdeb31ab29b51cdbbc992b66240
|
File details
Details for the file pytest_serverless-0.15.0-py3-none-any.whl.
File metadata
- Download URL: pytest_serverless-0.15.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.6.9 Darwin/20.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8820e00e33c2c11845fb26b84ff405d02f52e75748164cb78dff95430e66cbb3
|
|
| MD5 |
4704045feafdb9cd741d9b8c876da8cb
|
|
| BLAKE2b-256 |
45b07e1e907e631473e962708850157f4fc206ae65ac0b6879c71f7833689e6e
|