Skip to main content

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
Master PyPI 6.2

Pre installation requirements

  • serverless installed
  • pytest installed

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-serverless-0.15.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_serverless-0.15.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

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

Hashes for pytest-serverless-0.15.0.tar.gz
Algorithm Hash digest
SHA256 799a2409a7fb62151f91705228708424f5df631c8b9335fc0cf9db80bfa4a03f
MD5 224d6ea9cf5958c4a1783933ef13e69c
BLAKE2b-256 1045c0a98f50235a8a2289bd20000712a8e33bdeb31ab29b51cdbbc992b66240

See more details on using hashes here.

File details

Details for the file pytest_serverless-0.15.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_serverless-0.15.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8820e00e33c2c11845fb26b84ff405d02f52e75748164cb78dff95430e66cbb3
MD5 4704045feafdb9cd741d9b8c876da8cb
BLAKE2b-256 45b07e1e907e631473e962708850157f4fc206ae65ac0b6879c71f7833689e6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page