Python unit testing - pytest parameters
Skills:
AI-Assisted Code Review60%
Key Takeaways
Using pytest parameters for unit testing in Python
Full Transcript
hello friends welcome to core basics coding tutorial today we are going to talk about how you can combine multiple test cases into one single test case using p/y test parameters here I have a math lab dot PDF file which has a single function which is just calculating a square of a number so you can see that this is a very straightforward function typically when you write a unit test for this function you will end up writing multiple test cases to taste that functionality so you will have your first test case which will be testing the script square underscore one and you will say result is equal to MATLAB dot calculate square let's say you are calculating square of 5 so you will assert that result should be 25 because the square of Phi is 25 and this way you will write multiple test cases right so 9 squared is 81 and this is my third unit test let's say I'm calculating square of 10 which should be hundred okay this is what you do you supply multiple inputs and you check if those inputs are producing the expected output or not ok when you run this these test cases here they're going to work ok it says these three test cases pass now the problem with this is that you are essentially writing same code you can see that you are capturing a result in two result variable by calling calculate square and you are asserting against the output so I could say there is a repetition or duplication of code here it will be nice if I can somehow combine these three test cases into one and somehow let's say if I have just one test where I can past test input and capture expect their output and here I pass test input and here I can say right so you you will agree that if we had a way of doing this then it would be nice and then in the test input an expected output you can just pass those things your expected values and test inputs as parameters so py test allows you to do that so for this you have to do the import through a test and then use this special decorator called pure test dot mark dot parametrize so this is a py taste decorator that you need to use whenever you want to parameterize your taste cases okay and now the first argument to parameterize is a string argument where you will pass the name of the variables or the arguments that you are going to pass to a function so I just copy pasted these two from here the second argument will be the expected output and the taste input and you will be passing these things as a topple so let's say we had like three test cases okay so I'm going to pass three topples so my first test case was five was a test input and 25 was expected output second one was 981 and the third one was ten hundred here what this means is in this top of this file is test input in this twenty five is expected output so when I'm doing this let's see let's first run it and see what happens just clear the prom run it again and you can see that it essentially did the same thing and here you can see the test input output and past so this is pretty powerful it essentially generated three test cases out of these three sample inputs you should use this whenever you see similarity between unit test cases and just try to combine them that way you have less maintenance and whenever you want to add a new test you just add a new parameter here and you're done okay that was all about pure taste parameterization thank you for watching
Original Description
Learn how you can combine multiple python unit tests into one by using pytest parameters. We will discuss what is pytest parameters and showing how to implement it through examples.
Topics that are covered in this Python Video:
0:00 Overview
3:04 create pytest decorator parameterized()
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
Link for code in this tutorial: https://github.com/codebasics/py/blob/master/unittesting_pytest/parametrize/
Next Video:
numpy tutorial - introduction: https://www.youtube.com/watch?v=rN0TREj8G7U&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=39
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 · 0 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
60
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
More on: AI-Assisted Code Review
View skill →
🎓
Tutor Explanation
DeepCamp AI