How To Write Unit Tests For Existing Python Code // Part 1 of 2

preview_player
Показать описание
In this video, I show you a practical example of adding unit tests to existing code. This first part focuses on adding tests while not changing the original code. In the second part (coming out next week), I'll show how refactoring the code simplifies test writing while also improving the design.

NOTES:

1) There are a few things in the test setup that are not ideal, like how dates are used in the test code, using a real API key, and doing actual card charges. I'll address these things in part 2.

2) *patching* means that you're replacing a dependency in a function or method with something else. The thing that you replace it with is called a *mock* (apologies, my use of the terms in the video is a bit messy).

🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Sybren A. Stüvel
- Dale Hagglund

🔖 Chapters:
0:00 Intro
1:05 Explaining the example
4:10 Analysis & Setup
6:05 Adding LineItem tests
8:40 Adding Order tests
10:56 Adding PaymentProcessor tests
15:50 Analysis of the test setup so far
17:05 Adding pay_order tests

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

btw Arjan just wanted to mention that I literally got a job offer with twice my current salary by remembering things from your videos in the technical interview. most probably wouldn't have gotten it without you. thanks so much for what you do!

grmancool
Автор

I ll be Frank, design patterns, unit tests for existing code. You are my mentor. And you put up videos which are useful in a production environment.

sillytechy
Автор

9:23 there are two functions called `test_order_total`. Python only sees the second one, and the first one doesn't run. This is a great example of how you can have 100% test coverage and still be missing tests!

I have my vscode configured to run flake8 and mypy. I make this particular mistake all the time when writing tests, but I get a helpfully annoying red underline under the name of the first duplicated function name.

mikeciul
Автор

Thank you, this is exactly the nightmare I have to deal with. Instead of payment processors it’s expensive computations on gigabytes of data that need to be tested. Sigh. Can’t wait for part 2

astronemir
Автор

I've been programming for a while using Python, and I'd plateaued quite badly.
Your videos are fantastic, and they're constantly teaching me something new.

Thank you!

chaddrobertson
Автор

I'm so glad you've made this video, even though I don't need it anymore.

I got a bit annoyed that there were so many Unit Test introductions, but nothing about what to do if you already have existing code, especially if said code contains a state, because all introductions have a nice, pure (meaning the function only has input and output - no sideeffects like printing text or saving files), function, which simply doesn't reflect reality.

NostraDavid
Автор

After 3 hours of pain googling and youtubing, finally only your video made it clear how to run test from "tests" folder and make import work. FINALLY! THANK YOU SO MUCH

daymaker_bybit
Автор

Arjan, you always exceed the expectations. Thank you once more for the content. The quality of the tips and the video itself are amazing!

valmirjr
Автор

You're quickly becoming one of my favourite programming tutorial channels. Youtube has so much content which, on one end is all beginner programming concepts, and at the other end it's all frameworks and fancy programming features.

I think you fill that gap in between where we take basic programming features and compose them in ways that make software development scalable and maintainable.

vikingthedude
Автор

Nice timing! I'm currently doing something like an internship where one of the things I'll be doing is to help betterify code written by my supervisor. One of the first things I'm going to be doing will be to set up tests and automate the various "did you write it appropriately".

His code is quite good in general, but he has no tests, and when I asked if he used PEP-8 or another styleguide ... the answer was "I try to make sure to keep consistent tabs".

I might not be able to help make the code itself better/faster (don't know yet!), but i will for sure be able to help make sure it's easier to refactor it and show that it's *working as it should*

Melindrea
Автор

In my experience, old code that doesn't have tests is often nearly untestable. I recently rewrote a (very old) program that dealt with databases. The UI and the rest of the code are so interwoven that you can't even let it generate an SQL string without first creating a UI component that's responsible for it. That's why I didn't even bother with it. I just rewrote it from scratch.

Betacak
Автор

Love your intermediate tutorials with examples + refactoring! Would love more TDD and software design patterns guides (for dummies + for non-dummies) as well as I can write code in a pretty straightforward manner but not so much with the mindset of a production setting with best practices. Also really nice setup :D

zactamzhermin
Автор

When writing code with pytest.raises, I recommend only putting the line that you expect to raise in the block, otherwise it might accidentally catch a failure that is happening elsewhere.

jevandezande
Автор

phenomenal editing job. Thanks for creating this. All content, no fluff, right to the point, no hemming/hawing, quick pace but calm delivery.

I tip my hat to y'all. Subscribed and liked. Thank you for a top tier tutorial.

beaumcdermott
Автор

Honestly im so greatful for your videos.
Im learning so much, enjoying my time and gertting alot of motivation to improve as a datascientist/developer.
Thank you so much!!!!
Youre awesome!

eyalbahar
Автор

I learned all these techniques hard way and hours of hard work. I wish I had come across your channel before. If you are a python developer, you can’t appreciate Arjans content enough this content is literally the abstract of years of hard work.

amardeep.sahota
Автор

Great one. Good point that you can realize where the code needs refactoring from writing unit tests. The tests that check raise value error can actually Raise it from different calls (one the init of the object and the other from the call of the method) so those tests are actually not unit and are not testing in the way they are intended.

marcocofano
Автор

I just started delving into unit testing this week, so this video came at a perfect time!! Thank you so much, looking forward for part 2 :)

Bentroen_
Автор

Your videos are fantastic, and each time I am learning something new.

sarkhanmammadli
Автор

Excited for the Part2!! Arjan please post it asap...

rishabhahuja