Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run tests in an environment which also includes layers #900

Open
billyshambrook opened this issue Dec 29, 2018 · 4 comments
Open

Run tests in an environment which also includes layers #900

billyshambrook opened this issue Dec 29, 2018 · 4 comments
Labels
area/layers maintainer/need-response stage/bug-repro The issue/bug needs to be reproduced type/feature Feature request

Comments

@billyshambrook
Copy link

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
Copy link
Author

billyshambrook commented Mar 25, 2019

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.

@jfuss jfuss added area/layers type/feature Feature request labels Apr 23, 2019
@mingkun2020 mingkun2020 added the stage/bug-repro The issue/bug needs to be reproduced label Dec 14, 2020
@aahung aahung added stage/bug-repro The issue/bug needs to be reproduced type/bug and removed stage/bug-repro The issue/bug needs to be reproduced type/bug labels May 13, 2021
@virenshub
Copy link

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?

@mndeveci mndeveci removed the type/bug label Jan 10, 2023
@robbyoconnor
Copy link

What is the status on this?

@crwgregory
Copy link

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 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/layers maintainer/need-response stage/bug-repro The issue/bug needs to be reproduced type/feature Feature request
Projects
None yet
Development

No branches or pull requests

10 participants