Skip to main content

Automatically mocks resources from serverless.yml in pytest using moto.

Project description

pytest-serverless

Automatically mocks resources from serverless.yml in pytest using moto.

master PyPI Python pytest Licence
Build Status PyPI 3.10.1, 4.6.6, 5.2.4

Installation

pip install pytest-serverless

Your project has to have pytest installed.

What problem it tries to solve?

When building your project with serverless most likely you will create resources like dynamodb tables, sqs queues, sns topics.

During writing tests you will have to mock those in moto.

This pytest plugin tries to automate this process by reading serverless.yml file and create moto mocks of resources for you.

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

To start using my-microservice.my-table table in your tests just mark your test with @pytest.mark.usefixtures("serverless"), and rest will be done by plugin.

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

Issues?

Plugin is in early stage of development, so you might find some bugs or missing functionality.

If possible create pull request (with tests) that fixes particular problem.

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.9.0.tar.gz (4.6 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.9.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest-serverless-0.9.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0a2 CPython/3.6.5 Darwin/19.0.0

File hashes

Hashes for pytest-serverless-0.9.0.tar.gz
Algorithm Hash digest
SHA256 e85507ebba451bd34721d2937637111fd860a0505e388df016640243aefcc255
MD5 a6f37122763ca1e7b22ba55db046892e
BLAKE2b-256 8836273892acf7c76753530fe43a9704720ab55836cacb8df16b9d9c04fa2bc0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_serverless-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0a2 CPython/3.6.5 Darwin/19.0.0

File hashes

Hashes for pytest_serverless-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8c30f4a40eb88932d09cafaff80198052c00f77a9dee0fd742a3495b9175a47
MD5 aaafca4f0cd08a67c1ee707fdeac6cde
BLAKE2b-256 9835fc599bd46c099b4a85be5b62a82074a552bc910cc5158befe08ab850eefe

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