Python unit testing - pytest introduction
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
▶
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
Related Reads
📰
📰
📰
📰
Aplikasi AI Harian Untuk Produktivitas, Bye Burnout!
Medium · AI
From stethoscopes to source code:Why I Switched from Pre Med to BS AI
Medium · Programming
Ilios Software Pvt. Ltd.: Building Practical AI Products That Solve Real Problems
Medium · AI
Stratagems #10: Lena Watched a Team Adopt Her AI Template. Leo Didn't Know the Knife Was in the Contract.
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
🎓
Tutor Explanation
DeepCamp AI