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

Поділитися
Вставка
  • Опубліковано 26 лис 2024

КОМЕНТАРІ • 59

  • @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.

  • @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.

  • @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!

  • @serg_D
    @serg_D 2 роки тому

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

  • @ДанилКочетков-ы1в
    @ДанилКочетков-ы1в 2 роки тому

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

  • @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

  • @ВладимирШалун
    @ВладимирШалун 2 роки тому +1

    Great video. Everything is clear and accessible.

  • @zzygyx9119
    @zzygyx9119 Рік тому

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

  • @ВиталинаСмоленск
    @ВиталинаСмоленск 2 роки тому

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

  • @SayamKumar-ci9sg
    @SayamKumar-ci9sg Рік тому

    Loved the video. Thanks

  • @АлексейЛешов-е7у
    @АлексейЛешов-е7у 2 роки тому

    Excellent instructional video.

  • @ОльгаЛебедева-ц1в
    @ОльгаЛебедева-ц1в 2 роки тому

    Thank you. Your video helped me figure everything out.

  • @МаринаПеткова-у9м
    @МаринаПеткова-у9м 2 роки тому

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

  • @ЛарисаАфаринова
    @ЛарисаАфаринова 2 роки тому

    It's something with something. 👍👍👍

  • @kostya9169
    @kostya9169 2 роки тому

    Thank you very much for the interesting and informative video.

  • @liliaharchikova9365
    @liliaharchikova9365 2 роки тому

    Cool. Everything is clear and understandable.

  • @Инна-х8к
    @Инна-х8к 2 роки тому

    This information is really useful to me.

  • @АнатолийРожков-ю8е

    All clear. Great lesson.

  • @olyfomina9927
    @olyfomina9927 2 роки тому

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

  • @avbolshakov
    @avbolshakov 2 роки тому

    Thank you, Oleg!

  • @АнтонинаХохлова-с5н

    it was interesting, thanks for the video.

  • @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

  • @OlegBosi
    @OlegBosi 2 роки тому

    Very gooooood

  • @RishinderRana
    @RishinderRana Рік тому

    Very helpful!

  • @ygr4943
    @ygr4943 5 місяців тому

    شكرا ❤❤

  • @WilliamPatiño-p7m
    @WilliamPatiño-p7m 11 місяців тому

    Que buen video

  • @andreylagno2372
    @andreylagno2372 2 роки тому

    very good video

  • @dodokwak
    @dodokwak 2 роки тому

    Thank you!

  • @jhosagidpirelapineda7808
    @jhosagidpirelapineda7808 2 роки тому

    thank you.

  • @prateeksarangi9187
    @prateeksarangi9187 2 роки тому

    thanks 👍

  • @dodokwak
    @dodokwak 2 роки тому

    Bedankt

  • @ИванИванов-н9т9ъ
    @ИванИванов-н9т9ъ 2 роки тому

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

    • @RedEyedCoderClub
      @RedEyedCoderClub  2 роки тому +1

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

  • @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

  • @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