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
+73
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
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
0 commit comments