aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Run tests in an environment which also includes layers

Open billyshambrook opened this issue 6 years ago • 2 comments

Describe your idea/feature/enhancement

AWS Lambda Layers now make it possible to package a collection of dependencies together and can be shared across multiple Lambda Functions. AWS Lambda Layers can include language specific libraries, which the function will import to use.

This can break unit-tests written for Lambda Functions, as there may be imports that are provided via a Lambda Layer instead of via a build manifest (e.g. requirements.txt).

Therefore, we need a way to execute unit-tests within the same environment as when you run sam local invoke.

This problem also exists for other CI related checks like linting.

Additional Details

When you run unit-tests, linters etc, these may depend on additional build dependencies that you only need when running the checks but don't want in the final deploy, for example py.test. This would require some way of providing additional dependencies to the docker container that is used when running the checks.

billyshambrook avatar Dec 29 '18 18:12 billyshambrook

It seems like some packagers provide a native way of specifying "dev" dependencies, for example pipenv and npm.

Perhaps we could add a new command sam dev build which would install the dev dependencies, and sam dev run to run commands under this environment which would also include layers.

billyshambrook avatar Mar 25 '19 02:03 billyshambrook

thanks @billyshambrook , this feature would precisely help with my use case. https://stackoverflow.com/questions/70253939/running-tests-pytest-in-ci-gitlab-ci-kubernetes-runner-for-aws-lambda-w-lay

Team - thoughts on prioritizing this anytime soon?

virenshub avatar Dec 07 '21 15:12 virenshub

What is the status on this?

robbyoconnor avatar Mar 09 '23 18:03 robbyoconnor

So I'm pretty new to python and especially testing in python, but after many questions posed to gpt I think I have a working solution / workaround. I have no idea if this is best practice or not but it's all I could get to work:

Create a tests/conftest.py file

# conftest.py
import sys
import os

sys.path.append(f"{os.getcwd()}/your/path/to/layers")

After creating that file and adding the path to where I'm storing my layers, I was able to test my code as I would expect: python -m pytest tests/unit -v

Additional context from the convo w/ gpt if you're interested:

Q: Is there an equivallent to a bootstrap.py file like in phpunit? A: Pytest doesn't use a bootstrap.py file like PHPUnit does. However, pytest supports setup code through fixtures and conftest.py files. conftest.py: This is a special file that pytest recognizes. Any file named conftest.py is automatically processed by pytest to define fixtures, hooks, and other configuration settings that apply to the tests in the same directory and any subdirectories. If you have setup code that needs to run before any tests, you can often put that code in a fixture in your root conftest.py file. ~ gpt4

Cheers 🍻

mtio avatar Jun 26 '23 23:06 mtio