Skip to content

Commit a8c74e7

Browse files
committed
fix: #24 correct example test and docs
1 parent 91d8953 commit a8c74e7

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Diff for: python/example/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ This example uses both [tracing](https://github.com/awslabs/aws-lambda-powertool
99
* **Deploy**: `sam deploy --guided`
1010
* **Unit Tests**: We recommend proceeding with the following commands in a virtual environment
1111
- **Install deps**: `pip install -r hello_world/requirements.txt && pip install -r requirements-dev.txt`
12-
- **Run tests with tracing disabled**: `POWERTOOLS_TRACE_DISABLED=1 python -m pytest`
12+
- **Run tests with tracing disabled and namespace set**
13+
- `POWERTOOLS_METRICS_NAMESPACE="Example" POWERTOOLS_TRACE_DISABLED=1 python -m pytest`
14+
- Both are necessary because `app.py` initializes them in the global scope, since both Tracer and Metrics will be initialized and configured during import time. For unit tests, we could always patch and explicitly config but env vars do just fine for this example.
1315

1416
# Example code
1517

Diff for: python/example/tests/unit/test_handler.py renamed to python/example/tests/test_handler.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from hello_world import app
77

8-
98
@pytest.fixture()
109
def apigw_event():
1110
""" Generates API GW Event"""
@@ -69,13 +68,24 @@ class Context:
6968
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:298026489:function:test"
7069
aws_request_id: str = "5b441b59-a550-11c8-6564-f1c833cf438c"
7170

72-
def test_lambda_handler(apigw_event, mocker):
73-
74-
71+
def test_lambda_handler(apigw_event, mocker, capsys):
7572
ret = app.lambda_handler(apigw_event, Context())
7673
data = json.loads(ret["body"])
7774

75+
output = capsys.readouterr()
76+
output = output.out.split('\n')
77+
stdout_one_string = '\t'.join(output)
78+
7879
assert ret["statusCode"] == 200
79-
assert "message" in ret["body"]
8080
assert data["message"] == "hello world"
81-
# assert "location" in data.dict_keys()
81+
assert "location" in data
82+
assert "message" in ret["body"]
83+
84+
# assess custom metric was flushed in stdout/logs
85+
assert "SuccessfulLocations" in stdout_one_string
86+
assert "ColdStart" in stdout_one_string
87+
assert "UniqueMetricDimension" in stdout_one_string
88+
89+
# assess our custom middleware ran
90+
assert "Logging response after Handler is called" in stdout_one_string
91+
assert "Logging event before Handler is called" in stdout_one_string

0 commit comments

Comments
 (0)