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

  Рет қаралды 177,856

ArjanCodes

ArjanCodes

Күн бұрын

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).
The code I worked on in this episode is available here: github.com/ArjanCodes/2022-te....
💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
💻 ArjanCodes Blog: www.arjancodes.com/blog
🎓 Courses:
The Software Designer Mindset: www.arjancodes.com/mindset
The Software Designer Mindset Team Packages: www.arjancodes.com/sas
The Software Architect Mindset: Pre-register now! www.arjancodes.com/architect
Next Level Python: Become a Python Expert: www.arjancodes.com/next-level...
The 30-Day Design Challenge: www.arjancodes.com/30ddc
🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
👍 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!
💬 Discord: discord.arjan.codes
🐦Twitter: / arjancodes
🌍LinkedIn: / arjancodes
🕵Facebook: / arjancodes
👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Sybren A. Stüvel
- Dale Hagglund
🎥 Video edited by Mark Bacskai: / bacskaimark
🔖 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!

Пікірлер: 206
@ArjanCodes
@ArjanCodes 8 ай бұрын
💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
@grmancool
@grmancool 2 жыл бұрын
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!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
That’s awesome, congratulations!
@lackofsubtlety6688
@lackofsubtlety6688 2 жыл бұрын
That basically means that you owe him 20 percent of that, right? :p Grats on the job man!
@pranavnyavanandi9710
@pranavnyavanandi9710 2 жыл бұрын
Was that a course you took?
@Yeet_the_code
@Yeet_the_code 2 жыл бұрын
Well done and congratulations!!
@henrikholland1730
@henrikholland1730 2 жыл бұрын
That’s amazing. Congrats.
@sillytechy
@sillytechy 2 жыл бұрын
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.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you so much, glad you like the videos!
@mytelevisionisdead
@mytelevisionisdead 2 жыл бұрын
Hi Frank!
@mikeciul8599
@mikeciul8599 2 жыл бұрын
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.
@transatlant1c
@transatlant1c 2 жыл бұрын
Came here to report the same thing! Nice spot 👏
@plato4ek
@plato4ek 2 жыл бұрын
Actually pytest shows the coverage of 82% for `test_order.py`, which gives a hint. So it's not a 100% coverage.
@chaddrobertson5805
@chaddrobertson5805 2 жыл бұрын
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!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you Chadd, glad you like the videos!
@NostraDavid2
@NostraDavid2 2 жыл бұрын
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.
@vikingthedude
@vikingthedude 2 жыл бұрын
You're quickly becoming one of my favourite programming tutorial channels. KZbin 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.
@astronemir
@astronemir 2 жыл бұрын
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
@12valmirjr
@12valmirjr 2 жыл бұрын
Arjan, you always exceed the expectations. Thank you once more for the content. The quality of the tips and the video itself are amazing!
@Betacak3
@Betacak3 2 жыл бұрын
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.
@zactamzhermin1434
@zactamzhermin1434 2 жыл бұрын
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
@daymaker_bybit
@daymaker_bybit Жыл бұрын
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
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad it helped!
@M_7844
@M_7844 25 күн бұрын
Same here. I find it quite complicated to make import work correctly. I would like to ask, is it right I can't run test_line_item.py as separate file getting import error "No module named 'pay'? So far when I wanted to test my code I just run separate parts of it, but with import in siblings folders it's not possible.
@jevandezande
@jevandezande 8 ай бұрын
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.
@Melindrea
@Melindrea 2 жыл бұрын
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*
@beaumcdermott8742
@beaumcdermott8742 6 ай бұрын
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.
@ArjanCodes
@ArjanCodes 6 ай бұрын
Thank you so much for the kind words, Beau!
@amardeep.sahota
@amardeep.sahota Жыл бұрын
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.
@coldhardwick
@coldhardwick 2 жыл бұрын
you an mCoding are some of the most valuable resources I've seen on youtube in a looong time....
@sarkhanmammadli9673
@sarkhanmammadli9673 2 жыл бұрын
Your videos are fantastic, and each time I am learning something new.
@JacinLowe
@JacinLowe 2 жыл бұрын
Long time watcher. This was the video i was waiting for. I'm learning how to do testing and didn't know where to start.
@Bentroen_
@Bentroen_ 2 жыл бұрын
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 :)
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Coming up on Friday 😉.
@rishabhahuja7413
@rishabhahuja7413 2 жыл бұрын
Excited for the Part2!! Arjan please post it asap...
@marcocofano2545
@marcocofano2545 2 жыл бұрын
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.
@vbaclasses3553
@vbaclasses3553 2 жыл бұрын
When you know the content is going to be worth the watch, so you like the video even before you start watching. Vreeslik baie dankie.
@eyalbahar9291
@eyalbahar9291 2 жыл бұрын
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!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
I'm glad to hear the videos are helpful!
@pericofalcor
@pericofalcor 2 жыл бұрын
Awesome video, I really enjoyed it. It would be very interesting to see mocking applied to a database connection. I work with a 25 year old project in Perl, and a relatively younger Python code, and both have no tests at all. Both are inmense codebases and is usually seen as "imposible" to add unit testing to it. Everything is based on data stored in a mysql database, but I have no clue on how to even start testing when I have to mock so much data
@seanoverton798
@seanoverton798 2 жыл бұрын
Great videos mate. Always learning something new from them.
@samggez
@samggez 2 жыл бұрын
Arjan you are a true gem, hope to learn more about python from you! Keep it going!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Hey Samir thank you, and will do! 😉
@rhernaus
@rhernaus 2 жыл бұрын
Thank you for this valuable content. I have watched nearly all of your videos because I like the way you explain things. I'll start leaving comments on your videos to help the KZbin algorithm even more 🙂
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Glad you like the content! And thanks for liking/commenting under the video. It’s indeed good to help the KZbin algorithm, but I also find the feedback really helpful in improving the videos I post.
@rjgonzalez8108
@rjgonzalez8108 Жыл бұрын
Best python content here on YT. Also really like that shirt you're wearing. I would like to know where I can get one if you don't mind.
@universecode1101
@universecode1101 2 жыл бұрын
Now we're all leveling up guys, great 👏🏻😊
@jorgealarconalvarez
@jorgealarconalvarez 2 жыл бұрын
This tutorial is very complete, thanks man!
@conconmc
@conconmc 2 жыл бұрын
This is fantastic, eagerly waiting for the next video!!
@_ipsissimus_
@_ipsissimus_ Жыл бұрын
Arjan, I always enjoy your videos. You really do a good job of pacing the complexity from simple to advanced that makes learning here so appealing. If I could request that you follow up these videos with a "how to write unit tests for existing python code using unittest and mock/patch" that would be great. my organization recently switched over from the simpler pytest to now using the more complex unittest lib as standard, and the abstractions for mock and mocked classes for testing really make it hard to grasp exactly whats going on sometimes. Great video anyways
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks!
@betterbrained
@betterbrained Жыл бұрын
Thank you, Arjan, for your brilliant work and helping!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad it was helpful!
@pramithijagdish2821
@pramithijagdish2821 2 ай бұрын
This was super helpful! Thanks for sharing.
@ArjanCodes
@ArjanCodes 2 ай бұрын
Glad the video was useful!
@mauisam1
@mauisam1 2 жыл бұрын
#11 here. Can't wait until next week. Great video again!
@abdelghafourfid8216
@abdelghafourfid8216 2 жыл бұрын
very informative ! , I hope you'll cover also the type checking with mypy
@mikeciul8599
@mikeciul8599 2 жыл бұрын
I'd like to know more about your choice of pytest over unittest. Are there particular factors that lead to a preference, or is it just what's more familiar? I've been working on some projects that mix bits from both pytest and unittest, and it's kind of a mess but I don't know which one to go with. There's things that annoy me about both. :D
@user-jt5mc2kr4m
@user-jt5mc2kr4m Жыл бұрын
What a great discovery your channel is! Thanks a lot for all videos you are doing, fabulous job! Many greetings from Ukraine 💛💙
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you, happy you’re enjoying the content!
@jurybulich6324
@jurybulich6324 2 жыл бұрын
Thank you Arjan, really glad that somehow KZbin algorithms showed your channel) your content is very helpful! Nice
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you Jury, glad the content is helpful!
@igorferreira9683
@igorferreira9683 Жыл бұрын
Excellent video, I've been following it for some time and it's adding a lot of knowledge to me. A video on how to apply Mocks in situations involving API requests and database connection would be interesting.
@ArjanCodes
@ArjanCodes Жыл бұрын
Great suggestion Igor, Thank you!
@user-fs5nx7bx3t
@user-fs5nx7bx3t 2 жыл бұрын
Just in ...Thanks Arjan for picking up this topic.
@jandom1988
@jandom1988 2 жыл бұрын
This is a super mega important topic, thanks for the video! Test order total is defined twice, I think, so only one will be exercised
@jandom1988
@jandom1988 2 жыл бұрын
Also monekypatch is a rather exotic path. Why not mock.patch with side-effect for the 3 different return values?
@StellarEclipseStudio
@StellarEclipseStudio 2 жыл бұрын
I hope you bring Mock into the conversation! Easier to use than writing lambdas and mock functions. Mock(side_effect=inputs) would have been a simpler way to monkeypatch the input calls.
@StellarEclipseStudio
@StellarEclipseStudio 2 жыл бұрын
Mock is a class not well understood, I've had to teach other engineers about it many times. Well worth learning about if you're serious about writing test code! It's very powerful to use in conjunction with monkeypatch.
@Victorinoeng
@Victorinoeng 2 жыл бұрын
Hey, @Joshua That is indeed an interesting functionality. Do you have any material to share? I have been using patch and magicMock, but feels so unnatural. Every time I have to Google/read up on some examples again
@AgustinIgnacioGenoves
@AgustinIgnacioGenoves 2 жыл бұрын
Thanks Arjan, thisvideo was super util for me.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Glad to hear it was helpful!
@markasiala6355
@markasiala6355 2 жыл бұрын
The monkeypatch example was particularly interesting. I've only used it some to test a method that called input function. However, I modified sys.stdin to place my input strings rather than replacing the input function. Is there a preferred method or are they considered relatively equal?
@JoshSmeda
@JoshSmeda 2 жыл бұрын
You're a legend, Arjan 👍
@niconeumann2963
@niconeumann2963 2 жыл бұрын
Really great video! I love to see things about testing :) Wouldn't it be better to put only the line which raises an error into pytest.raises (like in try/catch) or why is all the code inside the block?
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks Nico! Yes, good point, it would be better to only put the line that actually raises the error in the with statement, as this focuses the error detection better.
@gayathris2108
@gayathris2108 3 ай бұрын
Found this to be simple and effective guide!!!
@ArjanCodes
@ArjanCodes 3 ай бұрын
I'm glad to hear you enjoyed the video!
@Izzat-bj1le
@Izzat-bj1le Жыл бұрын
Amazing I love it, thanks for doing what u do
@KarlaMuller
@KarlaMuller Жыл бұрын
I'd love to know where you got that cool geometric led lights behind you. Also, big fan of all your content!
@RaghothamRao
@RaghothamRao 7 ай бұрын
Thanks a lot! Very much helpful!
@ArjanCodes
@ArjanCodes 7 ай бұрын
Glad you enjoyed the video :)
2 жыл бұрын
Very interesting video, can't wait for part 2! You mentioned writing tests before the actual code. I wonder, how would this work for functions, that are computationally heavy, for example because they do some complex image processing, or perhaps create a 3D scene? How would you provide the correct result for the assertions? It's hard for me to imagine how this scenario would work. I'm only a beginner when it comes to programming, so I hope my question makes sense. :)
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Hi Dávid, if a function performs a very complex task, you would normally split up the function into several smaller functions that you can then test separately. For parts of your application that produce a complex result like a rendered image, a GUI, etc, a common technique used is snapshot testing. You generate the correct complex representation once and indicate this is the correct ‘snapshot’. When you change things in the code or add new features, the snapshot test verifies that the resulting output is still the same (or if it’s supposed to be different, you generate a new snapshot).
2 жыл бұрын
@@ArjanCodes Got it, thanks for the explanation Arjan!
@dannijt69
@dannijt69 2 жыл бұрын
super useful video, thanks again
@DarknezRocks
@DarknezRocks 2 жыл бұрын
Thanks for the video, wasn't aware of monkeypatch, looks much easier to use than the other patches available to pytest and unittests. Had just one remark, be careful with only checking the exception type, especially if the function being tested has multiple of the same exception types, you had 2x ValueError in the same function, but could've triggered the wrong if statement without knowing. I prefer to also check the actual error message within an assert, to make sure that I'm triggering the correct ValueError.
@mikeciul8599
@mikeciul8599 2 жыл бұрын
That's a great reason to use custom exceptions, and create a different one for each fail case!
@Phaust94
@Phaust94 2 жыл бұрын
Awesome stuff man, as always!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you!
@syspogh1609
@syspogh1609 Жыл бұрын
Thanks Arjan for this good video.
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad you liked it!
@marcgierse1601
@marcgierse1601 2 жыл бұрын
Very good video - as always :-). Just one remark: If you don't want to have some tests suddenly fail on 1/1/2025, you probably want to use either something like the freezegun module or set the card info dynamically :-).
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Hi Marc, I’ll address this issue in part 2 this Friday.
@md.mohiuddin
@md.mohiuddin 11 ай бұрын
Excellent tutorial.
@ArjanCodes
@ArjanCodes 10 ай бұрын
Glad you liked it!
@Klej0aka0Klej
@Klej0aka0Klej 2 жыл бұрын
I hope the code quality of tests in part2 will be much better, for example many many cases in here should use fixtures to not repeat code or create classes itself inside every test.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Yes - I’m covering fixtures in part 2.
@PartScavenger
@PartScavenger 2 жыл бұрын
I must say that I'm a little disappointed that he starts by understanding the code, and the method isn't a thousand lines long. That said I did learn a lot and I love this channel thank you for all the amazing content!!! Thanks for testing videos!! Please more!!!
@FabianSlonimczyk
@FabianSlonimczyk 2 жыл бұрын
Super interesting content. I have a question (open to anyone): I see that in testing with a "valid" card the expiry date is being hard coded. Would this not be a problem in the future, when the hard coded values become a "past date" and the tets are run again (and fail)? Thank you! PS (after watching part 2/2) My question is answered there!
@rodrigo2112-
@rodrigo2112- 2 жыл бұрын
Great video! I'm catching up with your videos, so maybe you've done that already, but it would be a nice follow up to show how to create the same payment processing system and its tests if we were writing it using TDD from scratch, probably using dependency injection for the input and the payment service and therefore not need to monkeypatch
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks and love that suggestion!
@ericandes4288
@ericandes4288 Жыл бұрын
My professor assigned us to make unit test without ever telling us what a unit test is. Thank you for teaching me. I regret paying for college now lol
@kosmonautofficial296
@kosmonautofficial296 2 жыл бұрын
great video thanks!
@sHillChannel
@sHillChannel 2 жыл бұрын
I swear you have a hidden video camera in my office. You keep making videos on exactly what I am working on.
@shaheerzaman620
@shaheerzaman620 2 жыл бұрын
Fantastic stuff!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks, glad you liked it!
@renatocustodio1000
@renatocustodio1000 2 жыл бұрын
It seems that monkeypatch is even more powerful than Java's mockito in some aspects. Nice video.
@jpactor155
@jpactor155 2 жыл бұрын
These videos are great.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you Jacob, glad you like them!
@YagiTheDog
@YagiTheDog Жыл бұрын
Great video!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks!
@Kliamframe
@Kliamframe Жыл бұрын
Sir, You're a genius!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks, happy you’re enjoying the content!
@cordempire4789
@cordempire4789 2 жыл бұрын
Hey man - moved house and I need to redecorate. Where did you get that thing on your wall? I love it!
@philscosta
@philscosta 2 жыл бұрын
Hi Arjan. As always, it was a great video! Thank you for it. I noticed there were 2 test functions with the same name "test_order_total()". Is it ok to do that when writing tests?
@plato4ek
@plato4ek 2 жыл бұрын
No, it's not OK. It's a bug caused by copy-paste, and it prevents one of the tests from running.
@henryw.4953
@henryw.4953 2 жыл бұрын
etymology suggestion for monkey patch: 1) guerillia -> gorilla -> monkey; 2) monkeying about with code (en.wikipedia.org/wiki/Monkey_patch)
@gh00n20
@gh00n20 2 жыл бұрын
really good video. thx!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks, glad you liked it!
@InspektorDreyfus
@InspektorDreyfus 2 жыл бұрын
Will the branch coverage (additional to line coverage) be in part 2? Can the coverage metric also be shown directly in the editor?
@jerepovedamartinez4574
@jerepovedamartinez4574 2 жыл бұрын
Hi Arjan, would be nice to know how to test very complex clases like the ones that usually appears in Deep Learning. For example, pytorch_lightning.LightningDataModule... things like the shape of tensors in several configurations, training loops, etc. I don't really know if it's worth to unit test somehow these kind of things. Currently, I'm placing asserts in my the code itself but not unit testing systematically, would be nice to know at least your point of view about this topic!
@mrswats
@mrswats 2 жыл бұрын
Pytest is easily my favourite testing framework! And my favourite feature are fixtures! So easy to create and you can do all sorts of cool things with them too, parametrize, etc... What I am still struggling with is to do true TDD. I most often find myself having to do some exploration before being able to think what the tests will look like. Any ideas for that?
@veni_vidi_victorian
@veni_vidi_victorian 2 жыл бұрын
I often start by thinking about the program flow and writing tests for the "intended flow" first. Then I think about what can go wrong and write tests for that. It's also important to remember that you can't test for every case, so do not feel too bad for missing something. Also, restricting per module helps a lot. Like in this video, the flow is "You have an item, order it, and get charged for the order.". So you first write a set of tests for the items, then the order, and finally the payment processor. When writing tests, the most essential part is not thinking of the implementation code but instead, "How would I interact with this object/function/...?". At least, those are my opinions/tips.
@TheStickofWar
@TheStickofWar 2 жыл бұрын
@@veni_vidi_victorian good tips, emphasis on the last part. Thinking about how you would interact with it is more important than the implementation, good tests should also be able to tell a story of how you interact with the code base
@alchemication
@alchemication 2 жыл бұрын
This is a great question and often asked. I would suggest exploring an idea completely separately (like for example in a Jupyter Notebook) and learning about API’s or error habdling, and then returning to TDD. There are many ways … 🖖
@andrewallbright658
@andrewallbright658 2 жыл бұрын
Excellent example software -- too often I see horrible example code when talking about testing (which leads me to conclusions I will not say). Question: what are your thoughts about starting with writing acceptance tests for brownfield projects?
@Otakutaru
@Otakutaru 2 жыл бұрын
I'm not doing this professionally, so I really doubt I will do many tests... But knowing how to is nice
@peterkruse8192
@peterkruse8192 2 жыл бұрын
Depending on the size of your growing project tests will help you keep your own code stable while growing and extending it. If your feature set is very volatile tests will feel like a burden and overhead.
@gw032sysint9
@gw032sysint9 2 жыл бұрын
Can you please suggest what is the right way to put constants (like error msgs, log messages, other strings used)? What is the best practice to use constants in python : (1) a separate .py file for storing strings (2) Enums (3) Dictionary (4) Named Tuples (5) Data classes (6) anything else
@oncedidactic
@oncedidactic 2 жыл бұрын
Cool video topic!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks!
@TheMightyOprah
@TheMightyOprah 2 жыл бұрын
Hey Arjan, love your videos :) any way you could comment on writing testable code for data science? I find it hard to write unit tests for, for example, the result of a more complex data processing function or the result of a training job.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks! A general tip to write code that's easier to test: whenever you can, treat data as immutable. So: read data, process it to create new data, but don't directly modify the data. By keeping data immutable, your processing functions are going to be easier to test since they are going to be mostly pure functions. Hope that makes sense. It's an interesting topic, I might do a separate video about this.
@codeaxen
@codeaxen Жыл бұрын
Hello thanks for this video really helped... but is it possible to do a real payout system tutorial with stripe putting all these tests together ?
@user-hk7wh9jm7l
@user-hk7wh9jm7l 2 жыл бұрын
Hi . Would be awesome if you could make a video about agile principles. Thanks in advance :)
@j4g094
@j4g094 2 жыл бұрын
Nice video, thanks. I do have a question, though: 14:17 I don't understand what "test_card_valid_date()" is actually testing, given that it has neither an assert statement nor a "with".
@PeterSeres
@PeterSeres 2 жыл бұрын
In this case it is testing whether an exception will be raised when calling the charge method. Since the date is valid, the method should not throw an exception. The test passes if it doesn't.
@sriramjaisankar9121
@sriramjaisankar9121 2 жыл бұрын
Hi Arjan, Are these tests possible when my application built on streamlit?
@taylormonacelli
@taylormonacelli 2 жыл бұрын
You're all dressed up these days. I need hacker hoodie back once in a while
@ericalbertobernal101
@ericalbertobernal101 2 жыл бұрын
Awesome. I am from mexico Your channel is fucking great. And thanks for sharing your knowledge. Keep the good work.
@hawkzro2455
@hawkzro2455 2 жыл бұрын
Thanks!
@hawkzro2455
@hawkzro2455 2 жыл бұрын
Didn't know I could actually write a comment with the purchase 😅 I Just wanted to give you a huge shout-out! I've been learning and growing in my engineering career for some time now and your videos have been a fantastic resource for me! Thank you for sharing your incredible expertise!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you so much! I'm happy to hear the videos are helpful to you!
@Yeet_the_code
@Yeet_the_code 2 жыл бұрын
I got my first software testing job were we use pytest to automate the API testing. I am thankful that youtube recommended your video because i would rather learn this than watch Netflix :D Thanks for putting this out for free 🙏🏽
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Glad to hear it’s helpful!
@oaklyfoundation
@oaklyfoundation Жыл бұрын
You are quite old, but you are not ancient. You have adapted and conquered so many changes, that's very impressive. Beside that the information and the enterprise ready information is just on another level. I no longer believe that programming has an age limit, you are so skilled at teaching and so skilled at technically coding. FUCKING Brilliant.
@jackanderson9258
@jackanderson9258 2 жыл бұрын
When should I use assert vs self.assertEqual?
@MishechkaA
@MishechkaA 2 жыл бұрын
That's what I need now 🐱 I wrote the code, but not unit test for it: (
@MRBADGUY0027
@MRBADGUY0027 6 ай бұрын
Hello Arjan, how does it look like if you've used "unittest" built-in lib? And why do you prefer pytest? Thnks
@gw032sysint9
@gw032sysint9 2 жыл бұрын
Could you please tell (1) how to do Unit Testing for decorators - both methods and class based? (2) Flask's request module throws lot of context related errors while patching how to resolve those?
@dariuscorvus
@dariuscorvus 2 жыл бұрын
Waiting for it!
@user-sm3qi1ku7r
@user-sm3qi1ku7r 7 ай бұрын
Hi Arjan It is a minor thing, but in 16:22, why do you prefer to use [int(d) for d in card_nr] rather than, e.g., list(map(int, list(d)))? Are there any benefits to this? Thanks
@JustMWest
@JustMWest 2 ай бұрын
Excellent breakdown! I want to try it out but I can't for the life of me figure out how to import "pay" from within "tests". How does this work??
@ArnoDunstatter-po7lv
@ArnoDunstatter-po7lv 2 күн бұрын
Wish you explained the -cov flag and its associated plugin at the beginning of the video.
How To Write Unit Tests For Existing Python Code // Part 2 of 2
17:18
Thoughts About Unit Testing | Prime Reacts
11:21
ThePrimeTime
Рет қаралды 206 М.
World’s Deadliest Obstacle Course!
28:25
MrBeast
Рет қаралды 54 МЛН
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 1,3 МЛН
Китайка и Пчелка 4 серия😂😆
00:19
KITAYKA
Рет қаралды 3,7 МЛН
A pack of chips with a surprise 🤣😍❤️ #demariki
00:14
Demariki
Рет қаралды 35 МЛН
5 Types of Testing Software Every Developer Needs to Know!
6:24
Protocol Or ABC In Python - When to Use Which One?
23:45
ArjanCodes
Рет қаралды 197 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 791 М.
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 95 М.
The Ultimate Guide to Writing Functions
24:31
ArjanCodes
Рет қаралды 177 М.
Every Developer Had This Feeling
9:52
ArjanCodes
Рет қаралды 10 М.
Why I prefer attrs over dataclasses
6:21
mCoding
Рет қаралды 61 М.
Do These 5 Things if You Don’t Want to Write Crappy Code
25:59
Why Vitest Is Better Than Jest
13:13
Web Dev Simplified
Рет қаралды 128 М.
World’s Deadliest Obstacle Course!
28:25
MrBeast
Рет қаралды 54 МЛН