
Unittest Mocking
For the course ‘Real World Test Automation with Pytest (Django app)’ by Eden Marco, in the section mocking, I decided to take additional four tutorials on this subject to get in-depth understanding of this subject. Firstly, “Intro to Python Mocks | Python tutorial #1” by Red Eyed Coder Club on Youtube. The length of tutorial is 0:18 total hours. o In this tutorial, I learned: • ideal testable function, and a unit test for it • example of a function with a dependency on another function • a test for the function with a dependency on another function. Problems that Mocks will solve. • patch() function from unittest.mock • an example of unit test of a function that makes a request to external API and returns a different result each time it's been called. A mock returns another mock. How to test requests. Secondly, “Intro to Python Mocks #2: Mocking Exceptions | Python tutorial” by Red Eyed Coder Club on Youtube. The length of tutorial is 0:15 total hours. o In this tutorial, I learned: • Adding try/except block to the function under test • How to raise an exception from a test, and how to mock an exception using the .side_effect property. • Fixing the 'TypeError: catching classes that are not inherit from BaseException is not allowed' when mocking an exception. • Using the .raise_for_status() function from the requests library • Writing a test to test raise_for_status(), and mocking raise_for_status • Checking status codes within an except clause of the try/except block Thirdly, “Intro to Python Mocks #3: Mocking Python Requests with Responses | Python tutorial” by Red Eyed Coder Club on Youtube. The length of tutorial is 0:09 total hours. o In this tutorial, I learned: • Writing a test for a function that should return value if JSON object with a certain structure is provided. • Testing the raise_for_status() function with Python Responses library • Testing a logic when Exception is raised. You can find above three tutorials at Part 1: https://www.youtube.com/watch?v=xT4SV7AH3G8&t=4s&ab_channel=RedEyedCoderClub Part 2: https://www.youtube.com/watch?v=RqR0AvEujrU&t=2s&ab_channel=RedEyedCoderClub Part 3: https://www.youtube.com/watch?v=RNVspDHVIA0&t=1s&ab_channel=RedEyedCoderClub Fourthly, “How to use Python's unittest.mock.patch” by redshiftzero on Youtube. The length of tutorial is 0:08 total hours. o In this tutorial, I learned: • Three ways to use `mock.patch` as decorator, context_manager, and as inline • Patch keyword agruments - `return_value` and `side_effect` You can find this tutorial at https://www.youtube.com/watch?v=WFRljVPHrkE&t=303s&ab_channel=redshiftzero