I have a Chalice API project with the following config under .chalice/config.json
:
{
"version": "2.0",
"app_name": "my-app",
"manage_iam_role": true,
"stages": {
"local": {
"api_gateway_stage": "local",
"environment_variables": {
"STAGE_NAME": "local",
"MY_ENV_VAR": "value-local"
}
},
"staging": {
"api_gateway_stage": "stg",
"environment_variables": {
"STAGE_NAME": "staging",
"MY_ENV_VAR": "value-staging"
}
},
"production": {
"api_gateway_stage": "prod",
"environment_variables": {
"STAGE_NAME": "production",
"MY_ENV_VAR": "value-prod"
}
}
}
}
The app code uses some env variables like MY_ENV_VAR
, loaded with os.environ["MY_ENV_VAR"]
.
The tests look like the following (let's say in a tests/test_my_endpoint.py
file):
from http import HTTPStatus
from app import app
from chalice.test import Client
def test_my_endpoint() -> None:
with Client(app, stage_name="local") as client:
response = client.http.get("/my-endpoint")
assert response.status_code == HTTPStatus.OK
But it seems stage_name="local"
doesn't mean variables for the stage local
are loaded for the tests, so during tests execution, env variables are not found and tests fail. I'm not sure how setting this argument for chalice.test.Client
is useful then.
For testing locally, I can always load environment variables locally, but for CI/CD, I would like the tests to use the env variable of a stage defined in .chalice/config.json
. Chalice documentation isn't very clear about the good practice here, and about how stage_name
argument works exactly.
How can I fix that, and generally speaking what is the proper way to create tests for a Chalice app using the env variables?