aws / aws/chalice

Environment variables producing errors during testing

Open
#1,893 4 comments 6 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
11.1k
Forks
1k
Avg merge
1d 22h
Merged PRs (30d)
2

Description

Hello, firstly I'd like to thank you for Chalice ! It's really easy to use and it worked very well to develop my API.

I use environment variables from `.chalice/config.json` in my app and it works fine for local and deployment.

But now that I want to write tests for my app, those environment variables produce errors.

I tried following the documentation about testing with environment variables (https://aws.github.io/chalice/topics/testing.html#environment-variables) but to no avail.

Here is the problematic code in question from `tests/test_app.py` where 'dev' is the stage name with all environment variables.

```
from chalice.test import Client

from app import app
def test_myPath():
with Client(app, stage_name='dev') as client:
response = client.http.get('/myPath')
assert response.status_code == 200
```

Then when I run :
> py.test tests/test_app.py

I get :

```
================================================================================================================= ERRORS =================================================================================================================
___________________________________________________________________________________________________ ERROR collecting tests/test_app.py ___________________________________________________________________________________________________
tests\test_app.py:5: in
from app import app
app.py:21: in
MY_URL = os.environ["MY_URL"]
C:\Python310\lib\os.py:679: in __getitem__
raise KeyError(key) from None
E KeyError: 'MY_URL'

```

I checked by printing `print(os.environ)` from `tests/test_app.py` and the environment variables from `.chalice/config.json` aren't there.

Contributor guide

Open the contributing guide

Research direction

Start with tests/test_app.py, the Client(app, stage_name='dev') entry point, and the testing documentation linked in the issue; reproduce the failure with pytest and compare the environment loaded from .chalice/config.json. Done means the configured MY_URL and other stage variables are available during tests without causing import-time errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
backend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.