Skip to main content

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

Metadata

Release files for pytest-serverless 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-serverless 1.0.0
File Size Uploaded
pytest-serverless-1.0.0.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-serverless 1.0.0
File Interpreter ABI Platform
pytest_serverless-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / pytest-serverless-1.0.0.tar.gz

Download URL pytest-serverless-1.0.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
aa221b90a095a62d8dea0f97c74c43be8ad5dc04637b1a82c16ad8a3ed29f7e6
BLAKE2b-256 checksum
How to use checksums
3dac2cf86b407bb959898d71a169b99ece24024452816d120e47a3ee328597eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.7.9 Darwin/21.4.0

Release files / pytest_serverless-1.0.0-py3-none-any.whl

Download URL pytest_serverless-1.0.0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
42be79fadf3696fd1e08a6d34b752f6e7ae86624462ad58e13dfc226a1c70aad
BLAKE2b-256 checksum
How to use checksums
2c95e9cc29caf478b3e28cc9f87c33a3aaa80bfd8e3933bba574919e553afa4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.7.9 Darwin/21.4.0

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.16.0

2 release files

0.14.0

2 release files

0.13.3

2 release files

0.13.2

2 release files

0.13.1

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page