Skip to content

Commit 993e8e5

Browse files
author
Tom McCarthy
authored
improv: include example tests in make tests (#63)
* chore: add example/tests to test paths * chore: update example tests to remove requirement to manually specify env vars * chore: try to fix build for python3.6 (install dataclasses backport) * chore: fix build for python 3.6 * chore: dont run example test on python3.6 since it requires asyncio
1 parent 158fd57 commit 993e8e5

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

Diff for: example/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Summary
22

3-
This example uses both [tracing](https://github.com/awslabs/aws-lambda-powertools/tree/develop/python#tracing) and [logging](https://github.com/awslabs/aws-lambda-powertools/tree/develop/python#logging) features, includes all environment variables that can be used, and demonstrates how to explicitly disable tracing while running unit tests - That is not necessary when running within SAM CLI as it detects the local env automatically.
3+
This example uses [tracer](https://awslabs.github.io/aws-lambda-powertools-python/core/tracer/), [metrics](https://awslabs.github.io/aws-lambda-powertools-python/core/metrics/),and [logger](https://awslabs.github.io/aws-lambda-powertools-python/core/logger/) features, includes all environment variables that can be used, and demonstrates how to explicitly disable tracing while running unit tests - That is not necessary when running within SAM CLI as it detects the local env automatically.
44

55
**Quick commands**
66

@@ -118,7 +118,7 @@ Tests are defined in the `tests` folder in this project. Use PIP to install the
118118
```bash
119119
example$ pip install -r hello_world/requirements.txt
120120
example$ pip install -r requirements-dev.txt
121-
example$ POWERTOOLS_TRACE_DISABLED=1 python -m pytest tests/ -v
121+
example$ pytest -v
122122
```
123123

124124
## Cleanup

Diff for: example/tests/__init__.py

Whitespace-only changes.

Diff for: example/tests/test_handler.py

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import json
2+
import os
3+
import sys
24
from dataclasses import dataclass
35

46
import pytest
57

6-
from hello_world import app
8+
9+
@pytest.fixture()
10+
def env_vars(monkeypatch):
11+
monkeypatch.setenv("POWERTOOLS_METRICS_NAMESPACE", "example_namespace")
12+
monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", "example_service")
13+
monkeypatch.setenv("POWERTOOLS_TRACE_DISABLED", "1")
14+
15+
16+
@pytest.fixture()
17+
def lambda_handler(env_vars):
18+
from hello_world import app
19+
20+
return app.lambda_handler
21+
722

823
@pytest.fixture()
924
def apigw_event():
@@ -61,30 +76,33 @@ def apigw_event():
6176
"path": "/examplepath",
6277
}
6378

79+
6480
@dataclass
6581
class Context:
6682
function_name: str = "test"
6783
memory_limit_in_mb: int = 128
6884
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:298026489:function:test"
6985
aws_request_id: str = "5b441b59-a550-11c8-6564-f1c833cf438c"
7086

71-
def test_lambda_handler(apigw_event, mocker, capsys):
72-
ret = app.lambda_handler(apigw_event, Context())
87+
88+
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
89+
def test_lambda_handler(lambda_handler, apigw_event, mocker, capsys):
90+
ret = lambda_handler(apigw_event, Context())
7391
data = json.loads(ret["body"])
7492

7593
output = capsys.readouterr()
76-
output = output.out.split('\n')
77-
stdout_one_string = '\t'.join(output)
94+
output = output.out.split("\n")
95+
stdout_one_string = "\t".join(output)
7896

7997
assert ret["statusCode"] == 200
8098
assert data["message"] == "hello world"
8199
assert "location" in data
82100
assert "message" in ret["body"]
83101
assert "async_http" in data
84-
102+
85103
# assess custom metric was flushed in stdout/logs
86-
assert "SuccessfulLocations" in stdout_one_string
87-
assert "ColdStart" in stdout_one_string
104+
assert "SuccessfulLocations" in stdout_one_string
105+
assert "ColdStart" in stdout_one_string
88106
assert "UniqueMetricDimension" in stdout_one_string
89107

90108
# assess our custom middleware ran

Diff for: poetry.lock

+14-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ radon = "^4.1.0"
4646
xenon = "^0.7.0"
4747
flake8-bugbear = "^20.1.4"
4848
flake8-eradicate = "^0.3.0"
49+
dataclasses = {version = "*", python = "~3.6"}
4950

5051
[tool.coverage.run]
5152
source = ["aws_lambda_powertools"]

Diff for: pytest.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[pytest]
22
addopts = -ra --cov --cov-config=.coveragerc
3-
testpaths = ./tests
3+
testpaths = ./tests ./example/tests

0 commit comments

Comments
 (0)