Navigate Select ESC Close

Professional Python Testing with Mocks

2023-07-03 Science & Technology
88.8k
1.5k
31
NeuralNine
NeuralNine
470.0k subscribers

Unlock all features

FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.

Description

In this video, we learn how to professionally test Python code with mocks. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 💼 Services 💼 💻 Freelancing & Tutoring: https://www.neuralnine.com/services 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm

Top Comments (10)

@JorgeEscobarMX 2023-07-04

Most people don't use mocks and they just hope that the database or the internet or whatever other service works during the test run phase. A good use case is when there is expected to get a reponse from the user like in the input() function, the test would just wait until someone input some data and press enter. Mocking the input() function allows the test to run uninterrupted.

10
@vitorsouza6604 2023-12-17

That was quite confusing.

8
@joaovitorfrossard7317 2024-02-14

English is my second language and the explainings are so clear that I understood everything in this tutorial. Very well explained and examplified. Thank you so much.

5
@20ted09 2023-09-30

This is now my GO TO channel for python. Absolutely top class tutorials!

2
@AndreCarneiro666 2025-12-25

Nice video! Very didact! I had some problems doing tests in my life as dev and I have one contribution with your video if I may. Mocking tool kits like pytest and unittest obey certain chain order of calls because of test context objects and that can be very tricky for devs, especially if you're trying to test things inside Flask application/session context, for example. The recommendation is mock function/method calls from the module is being tested instead from where it's created. In the case of you example with 'requests.get', the ideal is apply 'patch' in 'module_is_being_tested.requests.get'. That guarantees your patch will mock in the precise moment is needed and will return the mock value as expected. Otherwise, some undesirable values can be returned. Believe me, I loss all my hair being stressed with thing like this.

2
@justmogen7383 2023-10-28

the explanation from beginning is awesome mahn! Very concise

2
@mohitmansi11 2024-01-24

Very well explained. Please come up with more such videos and examples. Thanks .

0
@CristianMolina 2024-02-01

Also, to test error conditions it's great to use mocks on the dependencies

0
@thee_apedo_guy 2024-03-16

That was sensational, feeling 🏋️‍♀️🏋️‍♀️

0
@JorgeEscobarMX 2023-07-04

A little chaotic and the examples were very well done.

0

Unlock the Data Inside
Turn Videos into Knowledge

  • Get FREE 10/day: transcripts, summaries, chats
  • Chat with videos, export text & PDF
  • $1 free API credit for RAG, chatbots & research

Free forever plan • All features unlocked

App screenshot