https://github.com/whisller/pytest-serverless
Automatically mocks resources from serverless.yml in pytest using moto.
https://github.com/whisller/pytest-serverless
boto3 moto pytest serverless
Last synced: 5 months ago
JSON representation
Automatically mocks resources from serverless.yml in pytest using moto.
- Host: GitHub
- URL: https://github.com/whisller/pytest-serverless
- Owner: whisller
- License: mit
- Created: 2019-05-20T13:27:12.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-06-27T18:48:11.000Z (over 3 years ago)
- Last Synced: 2024-04-29T12:20:51.485Z (over 1 year ago)
- Topics: boto3, moto, pytest, serverless
- Language: Python
- Homepage:
- Size: 132 KB
- Stars: 27
- Watchers: 2
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
pytest-serverless
---
Automatically mocks resources defined in serverless.yml file using [moto](https://github.com/spulec/moto) and uses them in [pytest](https://github.com/pytest-dev/pytest).This way you can focus on writing tests rather than defining enormous list of fixtures.
| master | PyPI | Python | pytest | Licence |
| --- | --- | --- | --- | --- |
|  | [](https://pypi.org/project/pytest-serverless/) |  | `6.2` |  |## Pre installation requirements
- `serverless` installed
- `pytest` installed## Installation
```sh
pip install pytest-serverless
```## Usage
Assuming your `serverless.yml` file looks like:
```yaml
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.
```python
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`.
```shell
$ 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.
```shell
$ export SERVERLESS_COMMAND=sls
```## Supported resources
### AWS::DynamoDB::Table
### AWS::SQS::Queue
### AWS::SNS::Topic
### AWS::S3::Bucket
### AWS::KMS::Key