Intro to Python Mocks | Python tutorial

Опубликовано: 01 Октябрь 2024
на канале: Red Eyed Coder Club
84,815
2k

This video is an Intro to Python Mocks from unittest.mock library: what are Python mocks, why we have to use mocks, and when to do it.
How to write Python tests for functions that make a request to external API.
How to use the patch function to test Requests based functions with Python unittest.mock library.
It's a Python unittest mock tutorial for beginners.

Follow me @:
Telegram: https://t.me/red_eyed_coder_club
Twitter:   / codereyed  
Facebook: https://fb.me/redeyedcoderclub

Timecodes:

00:00 - ideal testable function, and a unit test for it.
02:29 - example of a function with a dependency on another function.
06:02 - a test for the function with a dependency on another function. Problems that Mocks will solve.
07:48 - patch() function from unittest.mock
11:28 - 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.

Why Python mocks are important:
- Mocks eliminate dependency on network, database calls, calls to OS (it will speed testing)
- we get isolated unit tests,
- we can test methods that have no return value
- reduce test complexity. We don't have to write complex logic to handle behavior of methods under tests.
- don't have to wait to implement other methods.

When should you mock?
When you don't want to actually call an object


Intro to Python Mocks | Python tutorial
   • Intro to Python Mocks | Python tutorial  

#python #testing #redeyedcoderclub