Pytest Tutorial – How to Test Python Code

preview_player
Показать описание
Learn how to use pytest, the powerful testing framework for Python. Throughout this course you'll gain a deep understanding of pytest's features, best practices, and the nuances of writing effective tests. And at the end, you'll learn how you can use ChatGPT to help you write tests quicker.

✏️ Course developed by @iamrithmic

⭐️ Contents ⭐️
(0:00:00) Introduction
(0:06:06) Our First Tests
(0:19:28) Class-based Tests
(0:29:49) Fixtures
(0:41:10) Mark & Parametrize
(0:55:24) Mocking
(1:13:16) Testing with ChatGPT

🎉 Thanks to our Champion and Sponsor supporters:
👾 davthecoder
👾 jedi-or-sith
👾 南宮千影
👾 Agustín Kussrow
👾 Nattira Maneerat
👾 Heather Wcislo
👾 Serhiy Kalinets
👾 Justin Hual
👾 Otis Morgan
👾 Oscar Rahnama

--

Рекомендации по теме
Комментарии
Автор

Thank you again for the opportunity to collaborate! I hope you folks found this insightful :)

iamrithmic
Автор

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! ❤

xsamueljr
Автор

Amazing tutorial on Pytest over the whole YouTube !!

deeksha
Автор

Thank you for helping to understand how fixtures work 😊

Melkii
Автор

Best tutorial for the pytest - crisp and clear

Thanks for posting this

itsmemani
Автор

Great tutorial
This tutorial made me fall in love with pycharm really.
great explanation

Sane_Circle
Автор

Thanks! Great tutorial! This has motivated me to venture into pytest more...

sandie_jr
Автор

Straight to the Topic !! Top-Notch Descriptions of Every part of Pytest. Love this Video . Will recommend it surely 😇😇😇🤗🤗.

parthib_deb
Автор

This video is friggin amazing, thanks, I leant a ton!

ranever-hadani
Автор

exactly what I wanted to get better at, thanks again

ThundaAaAaAaAaAh
Автор

awesome tutorial, reading the documentation was quite excruciating :D this format is much much better. thank you

skafiend
Автор

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

GameWorldRS
Автор

🎯 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.*

nitkarshchourasia
Автор

Thanks for this video - very useful content with really great code / examples.

paulthomas
Автор

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...

nicesong
Автор

Thank you, Can we have one such session on A/B testing too and sanity checks using statistical tests for ML models?

sriharsha
Автор

How did you make it show in terminal just (venv) -> file location name?

diana_str
Автор

What is the plugin that autocompletes the function keys?

lucasgabas
Автор

how does python gets to know confest has fixtures? like no need of importing and all?

manavmalhotra
Автор

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. ❤❤❤

ugwuokwuchukwu