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

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ธ.ค. 2024

ความคิดเห็น • 61

  • @RedEyedCoderClub
    @RedEyedCoderClub  2 ปีที่แล้ว +2

    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!

  • @zakchips
    @zakchips 2 ปีที่แล้ว +1

    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.

  • @charlietodd2867
    @charlietodd2867 ปีที่แล้ว

    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.

  • @ДанилКочетков-ы1в
    @ДанилКочетков-ы1в 2 ปีที่แล้ว

    This is an interesting video. For more of these in TH-cam 👍👍👍

  • @serg_D
    @serg_D 2 ปีที่แล้ว

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

  • @zzygyx9119
    @zzygyx9119 ปีที่แล้ว

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

  • @brahimboughanem208
    @brahimboughanem208 ปีที่แล้ว

    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

  • @ВладимирШалун
    @ВладимирШалун 2 ปีที่แล้ว +1

    Great video. Everything is clear and accessible.

  • @АлексейЛешов-е7у
    @АлексейЛешов-е7у 2 ปีที่แล้ว

    Excellent instructional video.

  • @ВиталинаСмоленск
    @ВиталинаСмоленск 2 ปีที่แล้ว

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

  • @ОльгаЛебедева-ц1в
    @ОльгаЛебедева-ц1в 2 ปีที่แล้ว

    Thank you. Your video helped me figure everything out.

  • @OlegBosi
    @OlegBosi 2 ปีที่แล้ว

    Very gooooood

  • @SayamKumar-ci9sg
    @SayamKumar-ci9sg ปีที่แล้ว

    Loved the video. Thanks

  • @ЛарисаАфаринова
    @ЛарисаАфаринова 2 ปีที่แล้ว

    It's something with something. 👍👍👍

  • @kostya9169
    @kostya9169 2 ปีที่แล้ว

    Thank you very much for the interesting and informative video.

  • @МаринаПеткова-у9м
    @МаринаПеткова-у9м 2 ปีที่แล้ว

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

  • @liliaharchikova9365
    @liliaharchikova9365 2 ปีที่แล้ว

    Cool. Everything is clear and understandable.

  • @Инна-х8к
    @Инна-х8к 2 ปีที่แล้ว

    This information is really useful to me.

  • @dodokwak
    @dodokwak 2 ปีที่แล้ว

    Bedankt

  • @АнатолийРожков-ю8е
    @АнатолийРожков-ю8е 2 ปีที่แล้ว

    All clear. Great lesson.

  • @oliveratkinson913
    @oliveratkinson913 2 ปีที่แล้ว

    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

  • @АнтонинаХохлова-с5н
    @АнтонинаХохлова-с5н 2 ปีที่แล้ว

    it was interesting, thanks for the video.

  • @avbolshakov
    @avbolshakov 2 ปีที่แล้ว

    Thank you, Oleg!

  • @RishinderRana
    @RishinderRana ปีที่แล้ว

    Very helpful!

  • @olyfomina9927
    @olyfomina9927 2 ปีที่แล้ว

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

  • @andreylagno2372
    @andreylagno2372 2 ปีที่แล้ว

    very good video

  • @ygr4943
    @ygr4943 5 หลายเดือนก่อน

    شكرا ❤❤

  • @passhabi
    @passhabi 11 วันที่ผ่านมา

    Why keep using MagicMock instead of Mock?

  • @dodokwak
    @dodokwak 2 ปีที่แล้ว

    Thank you!

  • @WilliamPatiño-p7m
    @WilliamPatiño-p7m 11 หลายเดือนก่อน

    Que buen video

  • @prateeksarangi9187
    @prateeksarangi9187 2 ปีที่แล้ว

    thanks 👍

  • @jhosagidpirelapineda7808
    @jhosagidpirelapineda7808 2 ปีที่แล้ว

    thank you.

    • @RedEyedCoderClub
      @RedEyedCoderClub  2 ปีที่แล้ว

      Thanks for watching, and for the comment!

  • @passhabi
    @passhabi 11 วันที่ผ่านมา

    I could understand it buuut..... I feel like I didn't get anything.. at least as the intention of the video was.

  • @vanderfed
    @vanderfed ปีที่แล้ว

    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

  • @ИванИванов-н9т9ъ
    @ИванИванов-н9т9ъ 2 ปีที่แล้ว

    А можно я не буду выпендриваться и задам вопрос на русском?
    Олег, почему Вы не используете pytest?

    • @RedEyedCoderClub
      @RedEyedCoderClub  2 ปีที่แล้ว +1

      Я не говорил, что я не использую pytest. Нет пока ни одного видео про pytest - вот это да.

  • @krishanunayek785
    @krishanunayek785 ปีที่แล้ว

    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