Skip to content

Commit 5e399b6

Browse files
author
Tom McCarthy
committed
docs: Add testing section to docs for parameters utility
1 parent 6869155 commit 5e399b6

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Diff for: docs/utilities/parameters.md

+73
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.index import handler
528+
529+
def test_handler(monkeypatch):
530+
531+
def mockreturn(name):
532+
return "mock_value"
533+
534+
monkeypatch.setattr(parameters, "get_parameter", mockreturn)
535+
return_val = handler({}, {})
536+
assert return_val.get('message') == 'mock_value'
537+
538+
```
539+
540+
=== "src/index.py"
541+
```python
542+
from aws_lambda_powertools.utilities import parameters
543+
544+
def handler(event, context):
545+
# Retrieve a single parameter
546+
value = parameters.get_parameter("my-parameter-name")
547+
return {"message": value}
548+
```
549+
550+
If we need to use this pattern across multiple tests, we can avoid repetition by refactoring to use our own pytest fixture:
551+
552+
=== "tests.py"
553+
```python
554+
import pytest
555+
556+
from src.index import handler
557+
558+
@pytest.fixture
559+
def mock_parameter_response(monkeypatch):
560+
def mockreturn(name):
561+
return "mock_value"
562+
563+
monkeypatch.setattr(parameters, "get_parameter", mockreturn)
564+
565+
# Pass our fixture as an argument to all tests where we want to mock the get_parameter response
566+
def test_handler(mock_parameter_response):
567+
return_val = handler({}, {})
568+
assert return_val.get('message') == 'mock_value'
569+
570+
```
571+
572+
Alternatively, if we need more fully featured mocking (for example checking the arguments passed to `get_parameter`), we
573+
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
574+
[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)
575+
object named `get_parameter_mock`.
576+
577+
=== "tests.py"
578+
```python
579+
from unittest.mock import patch
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 = 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)