What video should I make next? Any suggestions? Follow me @: Telegram: t.me/red_eyed_coder_club Twitter: twitter.com/CoderEyed Facebook: fb.me/redeyedcoderclub 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!
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.
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
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
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
It is really Beautiful explanation. You helped me a lot. I have written few of my testcases following your video. I was trying to implement the retry logic for http errors (500,502,503,504) and also the unit test cases for the retry logic for the http errors . i have also recently joined your telegram channel. I am giving my test code here below. Could you please have a look and suggest. def func1(url): s = auth_session(scopes=full_url) # auth_session function returns a authenticated session object as s. retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504]) s.mount('', HTTPAdapter(max_retries=retries)) res = s.get(url) res.func1() ==== @patch('app.func1') def test_retry_api_call_for_http_err(self, mock_session): # mock_session.exceptions = Session.exceptions mock_response = MagicMock(status_code=502) mock_response.raise_for_status.side_effect=HTTPError("Invalid response") mock_session.get.return_value=mock_response res=retry_api_call('example.com/') self.assertEqual(mock_session.get.call_count, 2) # it is returning Assertion Error : 0 != 2 self.assertEqual(res.status_code,502) # it is returning Assertion Error : 200 != 502
What video should I make next? Any suggestions?
Follow me @:
Telegram: t.me/red_eyed_coder_club
Twitter: twitter.com/CoderEyed
Facebook: fb.me/redeyedcoderclub
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!
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.
Thank you, Tatjana!
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.
This is an interesting video. For more of these in TH-cam 👍👍👍
Thanks you!
Satisfied with your explanations for the lesson. I have not met a better explanation.
Thanks for comment!
awesome video. Thanks! Just wonder why in the end you used nosetests.
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
Great video. Everything is clear and accessible.
Thanks for comment!
Excellent instructional video.
Thanks for comment!
Everything is smart and understandable. Thank you for the video.
Thanks for comment!
Thank you. Your video helped me figure everything out.
Thanks for comment!
Very gooooood
Thank you!
Loved the video. Thanks
It's something with something. 👍👍👍
Thanks for comment!
Thank you very much for the interesting and informative video.
Thank you!
Great video tutorial and explanation. Thank you for the wonderful material.
Thank you!
Cool. Everything is clear and understandable.
Thank you
This information is really useful to me.
Thanks for comment
Bedankt
Thanks for support!
All clear. Great lesson.
Thanks for comment!
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
Thanks for comment!
it was interesting, thanks for the video.
Thanks for comment!
Thank you, Oleg!
Thanks for watching!
Very helpful!
Thanks for the comment!
Great job thanks for it, your video lesson helped me a lot!! Good luck to you!!!
Thank you!
very good video
Thank you!
شكرا ❤❤
Why keep using MagicMock instead of Mock?
Thank you!
Thanks for comment!
Que buen video
thanks 👍
Thanks for watching!
thank you.
Thanks for watching, and for the comment!
I could understand it buuut..... I feel like I didn't get anything.. at least as the intention of the video was.
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
А можно я не буду выпендриваться и задам вопрос на русском?
Олег, почему Вы не используете pytest?
Я не говорил, что я не использую pytest. Нет пока ни одного видео про pytest - вот это да.
It is really Beautiful explanation. You helped me a lot. I have written few of my testcases following your video. I was trying to implement the retry logic for http errors (500,502,503,504) and also the unit test cases for the retry logic for the http errors . i have also recently joined your telegram channel. I am giving my test code here below. Could you please have a look and suggest.
def func1(url):
s = auth_session(scopes=full_url) # auth_session function returns a authenticated session object as s.
retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
s.mount('', HTTPAdapter(max_retries=retries))
res = s.get(url)
res.func1()
====
@patch('app.func1')
def test_retry_api_call_for_http_err(self, mock_session):
# mock_session.exceptions = Session.exceptions
mock_response = MagicMock(status_code=502)
mock_response.raise_for_status.side_effect=HTTPError("Invalid response")
mock_session.get.return_value=mock_response
res=retry_api_call('example.com/')
self.assertEqual(mock_session.get.call_count, 2) # it is returning Assertion Error : 0 != 2
self.assertEqual(res.status_code,502) # it is returning Assertion Error : 200 != 502