diff --git a/Programming_Languages/Python/Concepts/Testing_Python_code.md b/Programming_Languages/Python/Concepts/Testing_Python_code.md index ee8c53f..4df7b42 100644 --- a/Programming_Languages/Python/Concepts/Testing_Python_code.md +++ b/Programming_Languages/Python/Concepts/Testing_Python_code.md @@ -104,7 +104,7 @@ When we mock a function with `patch`, we are replacing the function with a Mock This is useful when you want to track or intercept calls without completely stubbing out or replacing the behavior of the function or method. This can be helpful when you to spy on the internal processes of the given function you are mocking - for instance, ensuring that it calls other functions. -## Testing exceptions with `raises` and `excinfo` +## Testing exceptions with `raises` Testing exceptions is quite straightforward. You can use the `raises` helper provided by pytest, and combine this with `excinfo` ("exception info") to inspect the exception message. @@ -126,6 +126,15 @@ Then to test this, we would use pytest's `excinfo` fixture along with `raises`: ) ``` +We could actually simplify the above test by using the `match` parameter with `raise`. This way we do not need the separate assertion: + +```py +with pytest.raises(ValueError, match="Error: POCKET_LAMBDA_ENDPOINT environment variable is not set"): + get_articles("some_type") +``` + +Note that `excinfo` is best used for testing the exception text that you the developer explicitly `raise`. For exceptions tha may occur naturaly in the code you are testing, you should use `caplog` or `capsys` (see below). + ## Before-each and after-each When testing functions, we achieve this in Python using `setup_function` and `teardown_function` methods. These methods are called before and after each test method respectively.