Python unit testing - pytest introduction

codebasics · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

Introduces pytest for Python unit testing, including installation and assert statements

Full Transcript

you certainly don't want to be like this guy when it comes to testing your code a good programmer will always test his score before moving it out to production a very good programmer will always write automated tests in order to perform the testing Python offers three basic frameworks for writing automated test unit tests News and py test among three py test is the best we are not going to cover why it is the best you can simply google it but if you are interested in learning how to write your first py test based unit test in Python then keep watching I have this Python code which has two methods and these are two pretty basic method one function is doing the sum of two numbers and the other one is doing a multiplication and I would like to write the unit test for these two functions so the first thing I'm going to do is install py test so for this go to your command prompt and install B write test using VIP you can just run VIP install py test I'm having some problem in installing this this will happen if your Python installation folder doesn't have write write permissions so you can easily fix that by going to your Python installation so you should know where your Python is install so in my case it is c program files and if I look for Python this is a directory where it is installed and here I will go to properties security go to all users edit the permission and in the edit permission again go to users and give all users a write permission so once you give the write permission is modifying the permission on all these directories and files and after that my installation is going to work so it's now installed py test correctly now what I will do is I will go to the directory where my Python code is so here my python code is in C code py so let me go to that directory so C code py unit-tests you can see it here I am in that directory and now I'm going to create a file for my unit test now always remember that ok so let me create that file from here now always remember that you have to have a prefix of prefix of taste underscore for your unit test file so I'll just call it test underscore math live okay and here I'm going to now write the unit test for these two functions okay so let me first import the matte lip and then my first unit this will be test underscore calculate total so I'm tasting this method calculate total so I'm just calling it taste underscore now py test is using some discovery rules to find out these test cases and at the discovery one of the discovery rule is to look for a prefix which has taste underscore so I always give taste underscore prefix to all my test function and taste under course underscore prefix to my test file as well okay so here I'll say map dot calc total that's the name of my method so you see this math Lib file calc total so calc total is this function which takes two arguments so I'm passing four and five okay you can pass any numbers here this is your unit test so it could be any number and you will capture the output in total and then to verify total you will use a third statement so you'll say assert total should be nine okay now you will write your second taste for multiply and the result here is mad lib dot calc multiply and you expect your result to be 30 because 10 into 3 is 30 so now I have written my unit test now I'm going to execute these tests so go to your directory here so I am in my Python directory here and alright let me go there first and then there are two ways you can execute your unit test using P I test number one is Python - mpy test if you just run this command what it is going to do is it is going to recursively go into all the files and subdirectories and it is gonna look for any file which has taste underscore prefix and that in that file it is going to execute all the tests which has taste underscore prefix in front of the the name of that method okay so let's just execute this cool so here it is saying math Lib has no attribute multiplier okay so maybe I did okay multi okay I misspelled that function name here so I'm going to fix it and if I run it again see it is saying that my to test passed okay second way of running this test is simply simply type in py dot test so when you say okay let me scroll this here okay so P Y dot test when you do that it's gonna say to do test passed in 0.01 second okay if you want to see a detailed output there is minus V command which means verbose when you do that it will actually list down your test name and whether it passed or fail okay now let's say due to some reason some stupid programmer came here and he modified your code and he screwed it up like this so where you are saying calculate total instead of doing summation let's say he put this - okay so what's gonna now happen is when you run your test that taste is gonna fail so it will say taste calc total fail because of whatever reason and you will go there and debug it okay so here your actual result came out to be minus 1 because 4 minus 5 is minus 1 and your expected result was 9 then you will go and fix this code okay so you now see the benefit of writing this unit test so again to just go over test discovery rule it will look for all the files and method which has taste underscore prefix let's say you don't have taste underscore terrific's here and and let's see what happens so I remove taste from here when you run your test now see it's not discovering anything here because it just goes by the prefix if you don't have that prefix it's not gonna find it now taste underscore is not the only rule there are some other rules also I am not aware of all those rules you can just google BY taste taste discovery rules and it will list all the rules there are certain other I think suffixes or file extensions that you can use you don't need to use only taste underscore but I find this very convenient that's why I'm using it okay one of the thing is your file name should also have taste underscore so let's see what if it doesn't have taste underscore so I am just going to remove so now it's not taste in the school and see what will happen so whenever near my taste again again it's not discovering it because I remove that taste prefix from my file name okay once I have taste here is gonna work okay alright so this was a short introduction on pure taste there are many other interesting aspects that we would like to cover but we will do that in the future video until then thank you and goodbye

Original Description

We are going to discuss python unit testing in this tutorial. We will see python’s testing frameworks for automated test, how to install pytest using pip, unit test coding, assert statement in unit test and what is”-v” command. Topics that are covered in this Python Video: 0:00 Overview 0:19 Python's testing frameworks for automated test 1:12 Install pytest using pip 3:31 Unit test coding 4:50 Assert statement in unit test 7:18 What is "-v" command Code for custom markers: https://github.com/codebasics/py/blob/master/unittesting_pytest/custom_markers/ Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Next Video: Python unit testing - skip/selectively run tests in pytest: https://www.youtube.com/watch?v=nv9zw454bEk&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=36 Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 60 of 60

← Previous Next →
1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related Reads

📰
Aplikasi AI Harian Untuk Produktivitas, Bye Burnout!
Boost productivity with daily AI applications and say goodbye to burnout by leveraging tools like ChatGPT, Notion AI, and Canva AI
Medium · AI
📰
From stethoscopes to source code:Why I Switched from Pre Med to BS AI
Learn how to transition from a pre-med background to a career in AI, and why making this switch can be a great opportunity for growth and development
Medium · Programming
📰
Ilios Software Pvt. Ltd.: Building Practical AI Products That Solve Real Problems
Learn how Ilios Software Pvt. Ltd. builds practical AI products that solve real problems, and why this matters for businesses today
Medium · AI
📰
Stratagems #10: Lena Watched a Team Adopt Her AI Template. Leo Didn't Know the Knife Was in the Contract.
Learn how to navigate contractual agreements and protect your intellectual property when sharing AI templates with teams, and understand the importance of careful contract review.
Dev.to · xulingfeng

Chapters (6)

Overview
0:19 Python's testing frameworks for automated test
1:12 Install pytest using pip
3:31 Unit test coding
4:50 Assert statement in unit test
7:18 What is "-v" command
Up next
How I use OpenAI to structure ad data and show my team what matters
Josiah Roche — Google Ads + SEO Training
Watch →