Skip to content
Advertisement

Python unittest request session when “with” statement is used

I want to mock requests.session() in unit test. It works when I don’t use with statement, however it fails when I start using it. This is the code I execute:

main.py:

JavaScript

test_main.py:

JavaScript

Advertisement

Answer

Your test setup is ignoring the fact that s comes from the __enter__ method of the context manager class.

You need to mock that call as well

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement