Intro to Python Mocks #2: Mocking Exceptions | Python tutorial

preview_player
Показать описание
This video is the second part of Python testing using Mocks series (or just Python Mocks). In this video we'll touch the mocking of exceptions topic, how to mock exceptions in Python, how to mock the .raise_for_status() function of the Requests library.
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 @:

Timecodes:

00:00 - Beginning. Adding try/except block to the function under test
02:19 - How to raise an exception from a test, and how to mock an exception using the .side_effect property.
04:23 - Fixing the 'TypeError: catching classes that are not inherit from BaseException is not allowed' when mocking an exception.
07:03 - Using the .raise_for_status() function from the requests library
09:48 - Writing a test to test raise_for_status(), and mocking raise_for_status
13:32 - Checking status codes within an except clause of the try/except block.

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

#2 Intro to Python Mocks: Mocking Exceptions | Python tutorial

#python #testing #redeyedcoderclub
Рекомендации по теме
Комментарии
Автор

What video should I make next? Any suggestions?
Follow me @:

Timecodes:

00:00 - Beginning. Adding try/except block to the function under test
02:19 - How to raise an exception from a test, and how to mock an exception using the .side_effect property.
04:23 - Fixing the 'TypeError: catching classes that are not inherit from BaseException is not allowed' when mocking an exception.
07:03 - Using the .raise_for_status() function from the requests library
09:48 - Writing a test to test raise_for_status(), and mocking raise_for_status
13:32 - Checking status codes within an except clause of the try/except block.

Help the channel grow! Please - like, share, and comment!

RedEyedCoderClub
Автор

Great. Thanks a lot. Very useful and underestimated topic, especially for those who's just found a job as a developer. Looking forward for more.

zakchips
Автор

This is a great start to my journey. Security note: the nose module is no longer being maintained and should not be used. Use nose2 if you must, but the preference is to move to pytest.

charlietodd
Автор

Satisfied with your explanations for the lesson. I have not met a better explanation.

serg_D
Автор

I want to thank you. I was really struggling to grasp the concept of mocking subjects, and your videos have made it so much easier for me to understand

brahimboughanem
Автор

awesome video. Thanks! Just wonder why in the end you used nosetests.

zzygyx
Автор

This is an interesting video. For more of these in YouTube 👍👍👍

ДанилКочетков-ыв
Автор

Great video. Everything is clear and accessible.

ВладимирШалун
Автор

Learn with your help the possibilities of Python Mock, get acquainted with the Requests library, using functions is a good opportunity to broaden your horizons and professionalism

oliveratkinson
Автор

Thank you. Your video helped me figure everything out.

ОльгаЛебедева-цв
Автор

Everything is smart and understandable. Thank you for the video.

ВиталинаСмоленск
Автор

Why keep using MagicMock instead of Mock?

passhabi
Автор

Great video tutorial and explanation. Thank you for the wonderful material.

МаринаПеткова-ум
Автор

It's something with something. 👍👍👍

ЛарисаАфаринова
Автор

This information is really useful to me.

Инна-хк
Автор

Great job thanks for it, your video lesson helped me a lot!! Good luck to you!!!

olyfomina
Автор

Thank you very much for the interesting and informative video.

kostya
Автор

Cool. Everything is clear and understandable.

liliaharchikova
Автор

it was interesting, thanks for the video.

АнтонинаХохлова-сн
Автор

Two comments about the code presented:

1. It's a very bad practice to use structures like "except SomeException: pass". That's how you can make exceptions untraceable and very difficult to debug.
2. If you mock not "main.requests" but "main.requests.get" you can get rid of all the fuzz about mocking raise_for_status. It is so much better to mock actually what you need to mock specifically than the whole requests library

vanderfed
join shbcf.ru