You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/parameters.md
+73Lines changed: 73 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -515,3 +515,76 @@ The **`config`** and **`boto3_session`** parameters enable you to pass in a cust
515
515
value = ssm_provider.get("/my/parameter")
516
516
...
517
517
```
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)
# 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
0 commit comments