0

I have a Python AWS Lambda function located in a directory structure like this:

|-lambda
      |
      |-lambda_function.py
               |
               |-tests
                     |
                     |-test.py
                     |-mockdata.json

I'm trying to perform unit testing using the unittest module, but I'm having trouble importing the lambda_handler function from the lambda_function.py file inside the test.py file.

I've tried using the following import statement in test.py:

from lambda_function import lambda_handler 

However, since test.py is in a subdirectory, I'm getting undefined results and the test is failing.

How can I properly import the lambda_handler function from lambda_function.py when running tests from a subdirectory? Any help would be appreciated. Thanks!

I have tried add parent directory of the test file which is lambda_function.py like below:

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

but it didn't solve the problem.

  • 1
    Try running `python -m unittest tests/test.py` from the `lambda` folder while having `from lambda_function import lambda_handler ` in the test. Also make sure a `__init__.py` is present in the `lambda` folder too – Paolo Apr 18 '23 at 06:38
  • Hi thanks the issue is now resolved by adding __init__.py under test and lambda source path. – sai variam Apr 23 '23 at 16:56

0 Answers0