Pytest Tutorial - How to Test Python Code

  Рет қаралды 217,632

freeCodeCamp.org

freeCodeCamp.org

Күн бұрын

Пікірлер: 112
@iamrithmic
@iamrithmic Жыл бұрын
Thank you again for the opportunity to collaborate! I hope you folks found this insightful :)
@diana_str
@diana_str 11 ай бұрын
How did you make it show in terminal just (venv) -> file location name?
@valorien1
@valorien1 8 ай бұрын
Thanks for the course. I think you're wrong about `setup_method()` and `@pytest.fixture`: The setup flows or objects they define will be executed/recreated again for each and every test case. This doesn't actually save time or computation resources.
@goodman4093
@goodman4093 7 ай бұрын
PLS STOP USING BLACK BACKGROUND FOR TUTORIAL!!!
@belindazhang426
@belindazhang426 Ай бұрын
thx, intuitive enough
@xsamueljr
@xsamueljr Жыл бұрын
I was literally studying unit testing in Python, and suddenly this video pops up in my feed! Amazing, high quality, free content, as always. Thanks! ❤
@akashmudgal3718
@akashmudgal3718 28 күн бұрын
This is THE course on youtube if you want to learn pytest quickly. Simple, and on point explanations of every important concept in pytest.
@deeksha6514
@deeksha6514 9 ай бұрын
Amazing tutorial on Pytest over the whole KZbin !!
@parthib_deb23
@parthib_deb23 9 ай бұрын
Straight to the Topic !! Top-Notch Descriptions of Every part of Pytest. Love this Video . Will recommend it surely 😇😇😇🤗🤗.
@iamrithmic
@iamrithmic 8 ай бұрын
This comment means a lot, thank you :)
@Sane_Circle
@Sane_Circle 9 ай бұрын
Great tutorial This tutorial made me fall in love with pycharm really. great explanation
@Alex43198
@Alex43198 15 күн бұрын
This is the best explanation I've found so far. I think the AI defeats the purpose a little, but it's good to know
@briannguyen5057
@briannguyen5057 2 ай бұрын
Thank you, I especially found the mocking section really helpful!
@skafiend4319
@skafiend4319 8 ай бұрын
awesome tutorial, reading the documentation was quite excruciating :D this format is much much better. thank you
@iamrithmic
@iamrithmic 8 ай бұрын
You are more than welcome :)
@syinx2301
@syinx2301 4 ай бұрын
Thanks i have know to use fixture, mark, paramatizer, and mock
@Melkii377
@Melkii377 11 ай бұрын
Thank you for helping to understand how fixtures work 😊
@GameWorldRS
@GameWorldRS 9 ай бұрын
Great tutorial! Also I think Harry was the "teacher" for the defense against the dark arts class in like book 6 (?) in that underground class that they made to prepare
@itsmemani9
@itsmemani9 Жыл бұрын
Best tutorial for the pytest - crisp and clear Thanks for posting this
@nitkarshchourasia2406
@nitkarshchourasia2406 10 ай бұрын
🎯 Key Takeaways for quick navigation: 00:43 🧪 *Pytest is a testing framework for Python, catering to both beginners and professionals.* 01:10 🕵️ *Pytest's autodiscovery of tests allows easy identification based on naming conventions, requiring no explicit registration.* 02:34 📋 *Rich assertion introspection in Pytest provides detailed reports on test failures, aiding in debugging.* 03:13 ⚙️ *Pytest supports parameterized and fixture-based testing, allowing efficient test execution with various arguments.* 03:39 🤔 *Reasons to use Pytest include simplified syntax, rich assertion introspection, a powerful fixture system, compatibility with other testing libraries, and extensibility through plugins.* 06:12 🛠️ *Installation of Pytest involves using the command `pip install pytest`, with version 7.4.2 demonstrated in this example.* 08:55 🧪 *Writing basic Pytest tests involves creating functions with assertions to validate expected outcomes.* 16:17 🚨 *Pytest can test for specific exceptions using `with pytest.raises(ExceptionType)` to ensure the expected errors occur.* 19:32 🔄 *Transitioning to class-based tests involves creating classes, and Pytest provides setup and teardown methods for pre/post-test actions.* 23:16 🔄 *Pytest class-based tests can utilize setup and teardown methods to perform actions before and after each test method within the class.* 25:22 🐍 *Pytest command in terminal requires `-s` flag to print statements in setup and teardown methods.* 26:19 🛠️ *`setup` method in Pytest can be useful for creating common objects used in multiple test methods.* 28:20 🔄 *`teardown` method in Pytest can be used for cleanup tasks, like deleting objects, especially in more complicated test scenarios.* 29:43 🚀 *Overview of class-based tests in Pytest, demonstrating how to set up and use class-based tests with Pytest.* 35:14 🛠️ *Pytest fixtures allow creating reusable objects for tests, reducing redundancy and improving maintainability.* 39:31 🌍 *`conftest.py` file can be used to store and make fixtures globally accessible across different test files.* 41:12 🏷️ *Pytest marking allows adding metadata to tests, such as labeling them as slow, skipped, or expected to fail.* 47:47 🔄 *Pytest marks, including `skip`, `xfail`, and `slow`, provide additional information about test behavior and status.* 52:23 📊 *Parameterizing tests in Pytest allows running the same test logic with different sets of input values, improving test coverage and readability.* 54:58 🧪 *Parameterized testing in Pytest allows testing with different values without messy loops. Useful for testing the same scenario with different inputs.* 55:27 🎭 *Mocking is a powerful testing technique to isolate systems by replacing external dependencies with controlled implementations called mocks.* 56:51 🧱 *Mocking example: Creating a dummy database in Pytest, mocking a function using `patch`, and verifying expected behavior.* 01:12:19 🤖 *When to use mocking: Use it when tests depend on external programs with unpredictable results, like APIs, to ensure consistent test outcomes.* 01:13:27 🤖 *Exploring AI for writing tests: Brief mention of using artificial intelligence for test automation and an invitation to try ChatGPT on OpenAI's platform.* 01:22:05 🎭 *Themed Testing: Using fixtures and parameterized tests, the code is tested with a Harry Potter theme, including students like Ron Weasley and a class named "Defense Against the Dark Arts."* 01:23:16 🐞 *Debugging: When a test fails, it's essential to review and understand the code and test conditions. In this case, an oversight in the code logic was identified and corrected.* 01:24:47 🤖 *ChatGPT 4: The tutorial briefly mentions using ChatGPT 4 for enhanced AI capabilities in generating test cases, suggesting it's available for "Plus" users.* 01:25:14 🤯 *AI Understanding: ChatGPT 4 demonstrates improved comprehension by providing a more detailed and context-aware set of test cases with a Harry Potter theme.* 01:26:38 🔄 *Consistency: When updating the test prompt, ensure all relevant details are modified to avoid inconsistencies and obtain accurate results.*
@osasomoregbee4878
@osasomoregbee4878 10 ай бұрын
Not all hero wear capes. Some are just computer programmers 😭😭🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡🫡
@bpospanov
@bpospanov 5 ай бұрын
Great job brother
@FxAndrej
@FxAndrej 3 ай бұрын
@43:45 - You get a warning for the "slow" mark because, unlike other marks, it is NOT a built-in type. So Pytest doesn't recognize it. You may have been misled by the "Getting Started" guide of the official documentation that it is. "Skip" is built-in, "xfail" is built-in, but not "slow". You must register "slow", along with any other "custom_marks" in a pytest.ini file
@sandie_jr
@sandie_jr 5 ай бұрын
Thanks! Great tutorial! This has motivated me to venture into pytest more...
@ThundaAaAaAaAaAh
@ThundaAaAaAaAaAh Жыл бұрын
exactly what I wanted to get better at, thanks again
@inteligenciaartificiuau
@inteligenciaartificiuau 2 ай бұрын
Wow! Amazing content! Thanks a lot!
@sriharsha580
@sriharsha580 Жыл бұрын
Thank you, Can we have one such session on A/B testing too and sanity checks using statistical tests for ML models?
@jansukemmanuel2717
@jansukemmanuel2717 3 ай бұрын
This was really really helpful... Much appreciate.
@riyazahmed7907
@riyazahmed7907 10 ай бұрын
Great lecture! Keep up the good work!
@ranever-hadani3603
@ranever-hadani3603 6 ай бұрын
This video is friggin amazing, thanks, I leant a ton!
@rohitn6333
@rohitn6333 2 ай бұрын
thanks a lot, this tutorial helped me a lot
@polaw7204
@polaw7204 Жыл бұрын
Thank you for this course.
@AryanK1511
@AryanK1511 13 күн бұрын
Loved it!
@laodrofotic7713
@laodrofotic7713 7 ай бұрын
to me it shows some parts of this you have memorized without properly understanding it, and this is so clear once you try to explain it. either that or you are lacking proper communication skills. Either way it amazes me to see how many people want to make videos about things they dont even understand themselves while people that do dont make videos at all.
@coderanger7708
@coderanger7708 3 ай бұрын
Elaborate please
@vuarnet0318
@vuarnet0318 4 ай бұрын
Thank you! Nice tutorial👍🏼
@dextron_py
@dextron_py 3 ай бұрын
Muy buen video, lo esperamos en español.
@AlexBerkk
@AlexBerkk Жыл бұрын
great stuff, but one thing is bugging me - why he doesn't use "from" in his imports? from source import service instead of import source.service as service ? Is there something I don't know about imports?
@1deesa
@1deesa 11 ай бұрын
Some find it useful for readability. Imagine a long code, and in the middle of it you see some function. In this way you see immediately that it is external. If author of code used "from .. import .." construction you will need to jump (or scroll) to the import part of a file. upd: reread your question and it occurs to me that I answered not to it. But let stay, I guess.
@yjc5805
@yjc5805 8 ай бұрын
谢谢作者!
@tadastadux
@tadastadux Жыл бұрын
great stuff, is source code available please?
@FxAndrej
@FxAndrej 3 ай бұрын
Re: Mocking - it's odd that you demo unittest mocking in a Pytest course. Pytest has its own module called MonkeyPatch, and I think that's what should've been demoed instead of mixing two different libraries.
@Learner_Shubham
@Learner_Shubham 26 күн бұрын
Amazing👌🏻
@manavmalhotra8513
@manavmalhotra8513 5 ай бұрын
how does python gets to know confest has fixtures? like no need of importing and all?
@deeksha6514
@deeksha6514 9 ай бұрын
Why are not we using MonkeyPatching built inside Pytest and instead using unittest ?
@edgarlip2
@edgarlip2 Жыл бұрын
Hi , awesome pytest intro !!! do you have the code in txt as well ?
@mathewsjose1990
@mathewsjose1990 5 ай бұрын
Great video!
@mostafaboras
@mostafaboras 10 ай бұрын
i don't why but i think that my vs code is crazy or when you imported source.my_functions as my_functions it telling me that there is no such module called my_functions pretty crazy right!!!!
@rudranarayansamal4168
@rudranarayansamal4168 9 ай бұрын
I got the same issue, it was solved when i created empty __init__.py files inside the package
@AntonioMartinez-sq2gp
@AntonioMartinez-sq2gp Жыл бұрын
I would like to learn coding. I would need to start with step 1. Can anyone point me in the right direction. What to learn first?
@StaticBlaster
@StaticBlaster Жыл бұрын
start with learning some discrete math and set theory. that will train your brain to think discretely and logically - the way computers "think" and process information. Then I would recommend learning JavaScript, HTML, and CSS. and finally learn python from variables to OOP concepts like classes, objects, inheritance, polymorphism, etc.
@AntonioMartinez-sq2gp
@AntonioMartinez-sq2gp Жыл бұрын
@StaticBlaster .... Thank You Very Much!! I hope you have a blessed day!!
@StaticBlaster
@StaticBlaster Жыл бұрын
@@AntonioMartinez-sq2gp You're very welcome, and you too, bro.
@ccc_ccc789
@ccc_ccc789 Жыл бұрын
The best way to learn is for you to identify what is it you wanted to create or solve. Anything that will make you wake up at night because you wanted to finish it. Next, break down the components of the problem in to small parts. Next, write a software function to solve each small parts of the problem. Then, use chatgpt to give you a sample code that is close enough to the software function that you are trying create. It is similar to building a Lego. It is only after you tried programming yourself that will start to appreciate the books or documentations. In other words, just do it. A good analogy is when a small child is trying learn to speak for the first time. They don't start with learning the abcs, they just do it. They try to mimic what they here again and again. It's only later in life that children learn the abcs to polish they're language.
@AntonioMartinez-sq2gp
@AntonioMartinez-sq2gp Жыл бұрын
@@ccc_ccc789 .... Thank You
@paulopontovaz
@paulopontovaz Жыл бұрын
"We were expecting a raise but instead we got a test fail" - I feel ya
@iamrithmic
@iamrithmic Жыл бұрын
😂😂
@tibiavram
@tibiavram 3 ай бұрын
thanks a lot.
@luisrnandezc
@luisrnandezc 10 ай бұрын
Good tutorial except the Mocking section which was extremely shallow. How the mock test is ever going to fail if you are the one defining the return value of the function/method and then are comparing it with the same exact value? Obviously is always going to pass.
@devsuniversity
@devsuniversity 10 ай бұрын
Who is the guy speaking at the first 30 seconds?
@relojsaturno
@relojsaturno 10 ай бұрын
But what is the point of testing a function with results that we know in advance?
@wabschall
@wabschall 10 ай бұрын
Thought experiment: Can I test a use-case that I haven't thought of yet? I would argue no. The first step to testing anything is to identify parameters I want to test. Think about it like this. Part of good coding is Error Handling; how to handle when things aren't correct in a calculation, input, or something else. Testing is a way to do this without actually having to run the code each time. I, as the programmer, know what my inputs should look like, and what my result should look like. The test just makes sure my code is doing what I think.
@relojsaturno
@relojsaturno 10 ай бұрын
@@wabschallBut my friend, if when you are writing the test it occurs to you that the function can receive an elephant as an argument, you modify the function so that it handles an elephant. It is conceptually like the impossibility of deceiving oneself. I still do not understand. Thanks for answering
@cjr-en4wr
@cjr-en4wr 10 ай бұрын
@@relojsaturno a block of code that works today might not work next week as code bases are forever being updated by the team - when pushing out an update to your website you can run all the tests to make sure everything is still ok.
@relojsaturno
@relojsaturno 10 ай бұрын
@@cjr-en4wr It would be good if in the video they simulated a slightly more real case, where a test discovered an error
@diana_str
@diana_str 11 ай бұрын
How did you make it show in terminal just (venv) -> file location name?
@lucasgabas3965
@lucasgabas3965 8 ай бұрын
What is the plugin that autocompletes the function keys?
@ugwuokwuchukwu1001
@ugwuokwuchukwu1001 Жыл бұрын
Thanks to free code camp for this wonderful video. Please I would like you to make a video on how to create animation, comics and stuffs like that. ❤❤❤
@Shubham__Saroj
@Shubham__Saroj 26 күн бұрын
Awesome
@amolsv
@amolsv Жыл бұрын
Thanks!
@lokeshwaran7671
@lokeshwaran7671 Жыл бұрын
Thank you!
@RZOLTANM
@RZOLTANM 4 ай бұрын
Please share a Github repository that accompanies this video.
@nicesong67
@nicesong67 Жыл бұрын
Great tutorial ! @iamrithmic how do you refactor function positional arguments to named parameters automatically ? I can't seems to find the information in the pycharm forum...
@davidolonisakin9028
@davidolonisakin9028 Жыл бұрын
🔥 lovely
@AdityaSharma-em8vz
@AdityaSharma-em8vz Жыл бұрын
Worth the time
@hotspot8688
@hotspot8688 Жыл бұрын
Why?
@hotspot8688
@hotspot8688 Жыл бұрын
Now i see why, it is really basic but might be useful for beginners.
@corybilyeu4490
@corybilyeu4490 6 ай бұрын
where's the code repo ?
@thiyagua3188
@thiyagua3188 7 ай бұрын
Nice
@TheFutureAwareness
@TheFutureAwareness 10 ай бұрын
so we need to install python, pytest, and pycharm?
@wabschall
@wabschall 10 ай бұрын
If on Windows: Yes & No. - You will need to install Python, then PyTest. - You do not specifically need to install Pycharm, but it's a decent IDE. Personally I use Visual Studio Code for all of my work. Pycharm is specifically made for python so it will have the most "out-of-the-box" compatibility with Python. If on Linux: - Python is installed on most Linux Distros by default. Go to your terminal and type in "python --version". If you get an error: It's not installed. If not, it is. - After that you do the same thing as with Windows. Install PyTest, get an IDE of choice.
@ak.murodov
@ak.murodov Жыл бұрын
Can you create a Dynamic Programming course with Python. Thanks beforehand
@tharulinsara7136
@tharulinsara7136 Жыл бұрын
GREAT😍
@SteveAcomb
@SteveAcomb Жыл бұрын
always bizarre to see you post these sorts of videos right as their topics are most relevant to me 😂 guess you’re pretty tuned into the zeitgeist lol
@exiviuz2428
@exiviuz2428 Жыл бұрын
❤❤❤
@silanoktay559
@silanoktay559 Жыл бұрын
Neden Türkçe altyazılı yok lütfen
@paulthomas1052
@paulthomas1052 Жыл бұрын
Thanks for this video - very useful content with really great code / examples.
@julianzurawski4944
@julianzurawski4944 Жыл бұрын
wow 😊
@Shriramkrishnhari
@Shriramkrishnhari Жыл бұрын
👍
@tarunikasundarraj4848
@tarunikasundarraj4848 8 ай бұрын
13:12:00
@viathorr
@viathorr 6 ай бұрын
55:45
@BlackTeenMonkey
@BlackTeenMonkey Жыл бұрын
Guys, I think we all have a similar resume xD
@Wineblood
@Wineblood Жыл бұрын
pytest makes me want to write my own test framework.
@vcool
@vcool Жыл бұрын
There is nothing that pytest does that Python's native unittest itself doesn't also do (except support plugins that painfully keep breaking).
@tomasohara
@tomasohara 9 ай бұрын
unittest is comatose!
@tarunikasundarraj4848
@tarunikasundarraj4848 8 ай бұрын
5:31
@march1956
@march1956 Жыл бұрын
1
@machinimaaquinix3178
@machinimaaquinix3178 10 ай бұрын
A much better (and shorter and more realistic) intro to Pytest: kzbin.info/www/bejne/j5PTfIB_is-pgZo
@irabor18
@irabor18 9 ай бұрын
Thank you, this tutorial was just okay, wasn't it?ha
@BCOG123
@BCOG123 2 ай бұрын
Because you are named your source directory not src and still using requirements.txt, I can't watch this. Neither are standard anymore so I can't trust your pytest stuff is current.
@manoharrathod6541
@manoharrathod6541 7 ай бұрын
I just suggest you, please leave teaching to someone and always teach yourself only as now in this video, you are teaching yourself. Useless teaching, it is just for who muggs the code. Waste of time😡
@СергейПопадев
@СергейПопадев 4 ай бұрын
all this crap
@sebastiangalvan6231
@sebastiangalvan6231 Жыл бұрын
❤‍🔥
@Maxible
@Maxible Жыл бұрын
Thanks!
@Maxible
@Maxible Жыл бұрын
This is literally the best pytest intro I've ever seen! Thank you for making it.
@iamrithmic
@iamrithmic Жыл бұрын
@@Maxible thank you so much. Glad it was useful to you
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,2 МЛН
How To Write Unit Tests For Existing Python Code // Part 1 of 2
25:07
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 1,8 МЛН
HELP!!!
00:46
Natan por Aí
Рет қаралды 76 МЛН
Кто круче, как думаешь?
00:44
МЯТНАЯ ФАНТА
Рет қаралды 5 МЛН
getting started with pytest (beginner - intermediate) anthony explains #518
13:19
Data Analysis with Python for Excel Users - Full Course
3:57:46
freeCodeCamp.org
Рет қаралды 3,2 МЛН
How To Write Unit Tests in Python • Pytest Tutorial
35:34
pixegami
Рет қаралды 149 М.
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,6 МЛН
Intro to Python Mocks | Python tutorial
18:42
Red Eyed Coder Club
Рет қаралды 87 М.
How to Get a Developer Job - Even in This Economy [Full Course]
3:59:46
freeCodeCamp.org
Рет қаралды 3,1 МЛН