1

I've been working a on a CRUD SAM application using a Python 3.8 runtime. My lambda functions reference a lambda layer that has code shared amongst the functions. My application builds/deploys and I can invoke the functions locally, however, in composing unit tests (using pytest), I'm not sure how to get around my imports referencing a layer in line that doesn't match the file structure.

File structure:

.
├── template.yaml
├── _layer-folder
│   └── _python
│       └── shared_code.py 
├── _lambda
│   ├── some_function.py
│   └── _tests
│       └── test-some-function.py

When running my tests for my lambda functions, I get an import error when I reference a module in that lives in the shared layer. For example:

from some_module_in_a_layer import some_layer_function

Is there a way to configure pytest to reference the correct file directory when running the tests?

3 Answers 3

3

I ended up resolving this by appending to the system path when testing or running locally within my __init__.py file.

if os.environ.get("ENVIRONMENT") == "test":
    sys.path.append(os.getcwd() + '/path/to/layer')
Sign up to request clarification or add additional context in comments.

Comments

0

It's a pain indeed to test layers properly and it kind of depends on your method of running the test (e.g. in Pycharm or using the terminal). In PyCharm you can add the layers directory as a source (right click use as source). To run from terminal you can add it to your PYTHONPATH before running, but it's quite ugly. So PYTHONPATH='/path/to/layer' python main.py

It's not pretty but I don't know another way to fix that tbh.

1 Comment

I appreciate the response!
0

I added this line in my test and it worked perfectly.

test_file.py

import pytest

sys.path.append(os.getcwd() + '/../layer_location')

Inside layer location my file system looks like this:

layer-location\
  __init__.py
  layer_code.py

The import in my non-test code is then:

code_under_test.py

from layer_code import foo

foo()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.