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_COMMAND=sls

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

Uploaded Source

Built Distribution

pytest_serverless-1.0.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest-serverless-1.0.0.tar.gz.

File metadata

  • Download URL: pytest-serverless-1.0.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.7.9 Darwin/21.4.0

File hashes

Hashes for pytest-serverless-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aa221b90a095a62d8dea0f97c74c43be8ad5dc04637b1a82c16ad8a3ed29f7e6
MD5 0e534dc615ade9800a02b497585f4f4c
BLAKE2b-256 3dac2cf86b407bb959898d71a169b99ece24024452816d120e47a3ee328597eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_serverless-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42be79fadf3696fd1e08a6d34b752f6e7ae86624462ad58e13dfc226a1c70aad
MD5 f767ae3e605d47299edb75ef7fd38383
BLAKE2b-256 2c95e9cc29caf478b3e28cc9f87c33a3aaa80bfd8e3933bba574919e553afa4f

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