Skip to content

Commit 1f60f31

Browse files
author
Tom McCarthy
authored
docs(parameters): add testing your code section (#1017)
* docs: Add testing section to docs for parameters utility * docs: Correct imported and monkeypatched paths
1 parent 2cc1135 commit 1f60f31

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

docs/utilities/parameters.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,76 @@ The **`config`** and **`boto3_session`** parameters enable you to pass in a cust
515515
value = ssm_provider.get("/my/parameter")
516516
...
517517
```
518+
519+
## Testing your code
520+
521+
For unit testing your applications, you can mock the calls to the parameters utility to avoid calling AWS APIs. This
522+
can be achieved in a number of ways - in this example, we use the [pytest monkeypatch fixture](https://docs.pytest.org/en/latest/how-to/monkeypatch.html)
523+
to patch the `parameters.get_parameter` method:
524+
525+
=== "tests.py"
526+
```python
527+
from src import index
528+
529+
def test_handler(monkeypatch):
530+
531+
def mockreturn(name):
532+
return "mock_value"
533+
534+
monkeypatch.setattr(index.parameters, "get_parameter", mockreturn)
535+
return_val = index.handler({}, {})
536+
assert return_val.get('message') == 'mock_value'
537+
```
538+
539+
=== "src/index.py"
540+
```python
541+
from aws_lambda_powertools.utilities import parameters
542+
543+
def handler(event, context):
544+
# Retrieve a single parameter
545+
value = parameters.get_parameter("my-parameter-name")
546+
return {"message": value}
547+
```
548+
549+
If we need to use this pattern across multiple tests, we can avoid repetition by refactoring to use our own pytest fixture:
550+
551+
=== "tests.py"
552+
```python
553+
import pytest
554+
555+
from src import index
556+
557+
@pytest.fixture
558+
def mock_parameter_response(monkeypatch):
559+
def mockreturn(name):
560+
return "mock_value"
561+
562+
monkeypatch.setattr(index.parameters, "get_parameter", mockreturn)
563+
564+
# Pass our fixture as an argument to all tests where we want to mock the get_parameter response
565+
def test_handler(mock_parameter_response):
566+
return_val = index.handler({}, {})
567+
assert return_val.get('message') == 'mock_value'
568+
569+
```
570+
571+
Alternatively, if we need more fully featured mocking (for example checking the arguments passed to `get_parameter`), we
572+
can use [unittest.mock](https://docs.python.org/3/library/unittest.mock.html) from the python stdlib instead of pytest's `monkeypatch` fixture. In this example, we use the
573+
[patch](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch) decorator to replace the `aws_lambda_powertools.utilities.parameters.get_parameter` function with a [MagicMock](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.MagicMock)
574+
object named `get_parameter_mock`.
575+
576+
=== "tests.py"
577+
```python
578+
from unittest.mock import patch
579+
from src import index
580+
581+
# Replaces "aws_lambda_powertools.utilities.parameters.get_parameter" with a Mock object
582+
@patch("aws_lambda_powertools.utilities.parameters.get_parameter")
583+
def test_handler(get_parameter_mock):
584+
get_parameter_mock.return_value = 'mock_value'
585+
586+
return_val = index.handler({}, {})
587+
get_parameter_mock.assert_called_with("my-parameter-name")
588+
assert return_val.get('message') == 'mock_value'
589+
590+
```

0 commit comments

Comments
 (0)