How to Write Great Unit Tests in Python
50K views · Aug 15, 2025 · Education
Comments · 63
@samsung40_media87 · 1 year ago
I prefer mock vs monkey patching. Though they're not apples v apples, I like mock's different asserts
15
@francescomarchisotti · 1 year ago
In the video you talked about other kinds of testing and while on YouTube and on your channel I find a lot of videos about unit testing, I feel like these other kinds are always a bit neglected<br>Can you do a video or a series of videos going more in depth about this topic of testing in all its different aspects? I think it would provide great value and even maybe stand out in YouTube’s Python community!
8
@Golgafrincham · 1 year ago
I like to use pytest.Parameter for parametrization. It is a bit more strict on typing and also gives you an ID of your tests where you can describe the current tests. It also integrates good with for example VS Code.
13
@marukrzysz1731 · 1 year ago
Great, more videos about testing would be nice. I feel like it is not covered enough , even in pytest documentation. I`m using Pytest-mock package for mocking so don`t have to import unittest library :p
8
@yehuda100 · 1 year ago (edited)
very important topic and not a lot of videos about it.
4
@_DRMR_ · 1 year ago
One thing we have in our CI is Integration Tests, where things like Database access or messaging behavior (ie. RabbitMQ) can be tested without mocking this functionality.<br>It makes setting up the testing environment a little bit harder, but it helps to confirm edge cases in a more realistic scenario.
7
@argyroustavros · 1 year ago
you should also have mentioned autospec or create_autospec for mocking the real signature of thje object or function. Without it calling a method that doesn't exist would also raise.<br><br>In general I feel that you could go more in depth or production ready for your videos, there are a lot of python channels already that touch on the surface of topics and never go too deep.
10
@chandanjha5022 · 1 month ago
Great thought ❤❤❤❤❤
@ThijsFranck · 1 year ago (edited)
Great stuff as usual, thanks Arjan!<br><br>I'd be interested to hear your take on the other types of testing that you mentioned, such as end-to-end testing. I've found these types of tests especially useful to validate code from a user perspective!<br><br>Some libraries I've experimented with include testcontainers and playwright in combination with pytest.<br><br>Would love to hear your thoughts, keep up the amazing work!
7
@thegimel · 1 year ago
Thank you! Please follow up with videos on other types of testing 🙏
1
@hansdietrich1496 · 1 year ago
About mocking: Imho the best approach would have been, to separate the unreliable data retrieval from the net from the deterministic local data processing. Then you can test the local processing, e.g. of the weather json response separately, without having to mess around with monkey patching.<br><br>While monkey patching is surely powerfull, quite often it just shows inflexible or suboptimal design of the functions, which you want to test. It mainly makes sense for some higher level functions.
1
@thebrigandier · 1 year ago
Great info, looking forward to trying these out.
1
Up next

How To Write Unit Tests For Existing Python Code // Part 1 of 2
ArjanCodes · 233K views

SQLAlchemy vs SQLModel: Which Should You Use?
ArjanCodes · 130K views

Learning Python Feels Easy. Until It Isn’t.
ArjanCodes · 47K views

Things (Almost) No One Thinks About When Designing Functions in Python
ArjanCodes · 110K views

Reinforcement Learning from Scratch: Code a Q-Learning Agent in Python
QuietLoom AI Lab · 47 views

Don’t Use Boolean Flags in Python, Use Policies Instead
ArjanCodes · 165K views

Thoughts About Unit Testing | Prime Reacts
The PrimeTime · 350K views

What Most Python Developers Miss About Generators
ArjanCodes · 30K views

How to Test Python Code with PyTest (Best Practices & Examples)
Code with Josh · 11K views

How to Write Python That Feels Like Python
ArjanCodes · 174K views

Don't learn AI Agents without Learning these Fundamentals
KodeKloud · 1.2M views

How To Write Unit Tests (The Right Way)
Cody Engel · 64K views

The Builder Pattern in Python: Finally Explained!
ArjanCodes · 65K views

10 Python Features You’re Not Using (But Really Should)
ArjanCodes · 76K views

Please Learn How To Write Tests in Python… • Pytest Tutorial
Tech With Tim · 244K views

From Spaghetti Code to Clean Python
ArjanCodes · 126K views

Why You Should Think Twice Before Using Returns in Python
ArjanCodes · 96K views

EU Tells USA To BACK OFF! Dutch BAN Microsoft, Bonds COLLAPSE, Trump PANICS
House of El · 236K views

Software Testing Theory + A Few Less Obvious Testing Techniques
ArjanCodes · 49K views

Mastering API Testing with FastAPI: Databases, Dependencies, and More!
ArjanCodes · 107K views