Intro to Python Mocks: Mocking Python Requests with Responses | Python tutorial

  Рет қаралды 12,877

Red Eyed Coder Club

Red Eyed Coder Club

Күн бұрын

This video is the third part of Python testing using Mocks series (or just Python Mocks). In this video we'll touch the mocking of functions that use Python Requests library with Responses library.
How to use Python Responses library to mock function, raise_for_status, exceptions.
In this Python testing tutorial as an example I use a function that makes a request to external API. And as a testing library I use unittest.
It's a Python unittest mock tutorial for beginners.
Follow me @:
Telegram: t.me/red_eyed_coder_club
Twitter: / codereyed
Facebook: redeyedcoderclub
Links:
github.com/getsentry/responses
Timecodes:
00:00 - Beginning.
01:31 - Writing a test for a function that should return value if JSON object with a certain structure is provided
05:50 - Testing the raise_for_status() function with Python Responses library.
06:49 - Testing a logic when Exception is raised
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
#1 Intro to Python Mocks | Python tutorial
• Intro to Python Mocks ...
#2 Intro to Python Mocks: Mocking Exceptions | Python tutorial
• Intro to Python Mocks ...
#3 Intro to Python Mocks: Mocking Python Requests with Responses | Python tutorial
• Intro to Python Mocks:...
How to mock requests in Python with Responses library.
#python #testing #redeyedcoderclub

Пікірлер: 48
@RoomOfClouds
@RoomOfClouds 2 ай бұрын
Thank you! Responses package is so much easier to test requests package. It was very easy to test urllib3 Retry mechanism.
@akshayas5402
@akshayas5402 Ай бұрын
Thank you. The mocking videos are so informative
@otom8872
@otom8872 Жыл бұрын
Clear and easy to follow. Keep it up :) Thank you very much!
@RedEyedCoderClub
@RedEyedCoderClub Жыл бұрын
Thank you very much for your comment!
@user-ho6ul2vp2d
@user-ho6ul2vp2d 2 жыл бұрын
Excellent work done on your part, I learned a lot of useful things for myself, Thank you!
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thank you!
@amriteshsingh2952
@amriteshsingh2952 Жыл бұрын
Thanks for the beautiful and simple explanation, loved that.
@RedEyedCoderClub
@RedEyedCoderClub Жыл бұрын
Thank you! Glad you liked it!
@abrikosa717
@abrikosa717 2 жыл бұрын
Thank you for your efforts! Very useful video!
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thanks for watching!
@AnnaNEast
@AnnaNEast 2 жыл бұрын
An excellent guide to Python testing
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thank you very much!
@user-ic8qn5id9w
@user-ic8qn5id9w 2 жыл бұрын
Video is great! It was interesting.👍👍👍
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thank you!
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
What video should I make next? Any suggestions? Don't forget to like the video. Thank you! Follow me @: Telegram: t.me/red_eyed_coder_club Twitter: twitter.com/CoderEyed Facebook: fb.me/redeyedcoderclub
@dodokwak
@dodokwak 2 жыл бұрын
Just few suggestions: 1. short video about custom exceptions (requests_mock) with raise customException flow instead of return 2. Type hits 3. creation of custom of django templates with custom views/mixins in django admin. Thank you.
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
@@dodokwak Thanks for your suggestions. Wrote them down!
@nateriver8261
@nateriver8261 2 жыл бұрын
Also would be intresting to know about mock of httpx
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
@@nateriver8261 Thank you for the suggestion! Got it!
@zakchips
@zakchips 2 жыл бұрын
My comment should be probably deleted by YT bot, but I can write it again: my suggesstion is 1. how we can test a third-party application for Django with pytest and tox. 2. debug django app with pdb Thank you very much.
@andreylagno2372
@andreylagno2372 2 жыл бұрын
very good video
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thanks for comment!
@prateeksarangi9187
@prateeksarangi9187 2 жыл бұрын
thanks mate ! more on mocking as your videos are quite promising just a suggestion on request_mock module 👍
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thank you, and thanks for your suggestion. By the way, why request_mock? The Responses library seems to me to be more convenient, and it has better support.
@prateeksarangi9187
@prateeksarangi9187 2 жыл бұрын
@@RedEyedCoderClub yeah agreed !! Just for exploring !! I find responses module really quick and easy ! 👍
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Ok, got it. Definitely will make the video about requests_mock
@duke007x3
@duke007x3 2 жыл бұрын
Thank You for tutorial. Can we use mocks with pytest? Maybe You can make few series about pytest fixtures, parametrization etc? anyway thanks, nice job.
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Of course we can use all these tools with pytest too. Thanks for your suggestions, I'll make videos with pytest.
@OlegBosi
@OlegBosi 2 жыл бұрын
very goood
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thank you!
@sahil_singhai
@sahil_singhai 10 ай бұрын
My python file and my testcaes are in different folders. so when I do ``` @patch("main.post") ``` I get ` ModuleNotFoundError: No module named 'main' ` error. what should I do.
@OmgOrganic
@OmgOrganic 2 жыл бұрын
классная либа, спасибо👌
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Yep, very convenient.
@user-yq4bg1sy7m
@user-yq4bg1sy7m Жыл бұрын
Thanks. Please create something on another file code calling derived class calling parent class method.
@abhishekravoor
@abhishekravoor 2 жыл бұрын
Nice videos on mocking, Please make viddeos on how to mock SubProcess and json data, and also how to mock Power commands of windows such as command = "%s /cs /c:1 /p:%s" % (SOMETHING, sleep_time). pleasy make vidoe on this it would be really helpfull!
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thanks for comments, and thanks for the suggestion. I'll think about it.
@botfly7444
@botfly7444 2 ай бұрын
thx for the tutorial, however i don't understand how to test the response.raise_for_status() instance in the try-block in main.py, can u explain to me?
@zaemiel
@zaemiel 2 ай бұрын
It's the video #3, have you seen the first two?
@botfly7444
@botfly7444 2 ай бұрын
i did it today, how can i test an exception raise ? instesad of return 'some_text' how can i test through the responses methods the reaise HTTPError for example?
@dodokwak
@dodokwak 2 жыл бұрын
May be also include how to debug django project with api calls ( for example in free vscode debug mode). Thank you again.
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Ok, wrote it down too. Thank you for your suggestions.
@dodokwak
@dodokwak 2 жыл бұрын
Bedankt
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thank you very much. I hope that the video was useful for you.
@user-yg6pm4yi5d
@user-yg6pm4yi5d 2 жыл бұрын
very good video
@RedEyedCoderClub
@RedEyedCoderClub 2 жыл бұрын
Thank you!
Intro to Python Mocks | Python tutorial
18:42
Red Eyed Coder Club
Рет қаралды 80 М.
Intro to Python Mocks #2: Mocking Exceptions | Python tutorial
15:54
Red Eyed Coder Club
Рет қаралды 16 М.
ЧУТЬ НЕ УТОНУЛ #shorts
00:27
Паша Осадчий
Рет қаралды 8 МЛН
One moment can change your life ✨🔄
00:32
A4
Рет қаралды 33 МЛН
Best KFC Homemade For My Son #cooking #shorts
00:58
BANKII
Рет қаралды 56 МЛН
Intro to async Python | Writing a Web Crawler
14:23
mCoding
Рет қаралды 76 М.
Professional Python Testing with Mocks
21:19
NeuralNine
Рет қаралды 46 М.
Python Tutorial: Unit Testing Your Code with the unittest Module
39:13
Corey Schafer
Рет қаралды 1,3 МЛН
Requests Library in Python - Beginner Crash Course
20:32
NeuralNine
Рет қаралды 45 М.
How To Write Unit Tests For Existing Python Code // Part 1 of 2
25:07
Getting Started With Testing in Flask
28:31
Pretty Printed
Рет қаралды 24 М.
Requests vs HTTPX vs Aiohttp | Which One to Pick?
15:11
ArjanCodes
Рет қаралды 35 М.