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

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.

Files for pytest-serverless, version 0.13.3
Filename, size File type Python version Upload date Hashes
Filename, size pytest_serverless-0.13.3-py3-none-any.whl (6.1 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size pytest-serverless-0.13.3.tar.gz (4.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page