

In this Python Programming Tutorial, we will be learning how to unit-test our code using the unittest module. Unit testing will allow you to be more comfortable with refactoring and knowing whether or not your updates broke any of your existing code. Unit testing is a must on any large projects and is used by all major companies. Not only that, but it will greatly improve your personal code as well. Let’s get started.
The code from this video can be found at:
Unittest assert methods:
if __name__ == ‘__main__’ video:
OOP Series:
✅ Support My Channel Through Patreon:
✅ Become a Channel Member:
✅ One-Time Contribution Through PayPal:
✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey’s Public Amazon Wishlist
✅ Equipment I Use and Books I Recommend:
▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –
#Python
source
11:06 How do you do that?
How did he do that? (Shortcut pls) 11:03
"You don't have to be an expert at tests." True. I find that the most valuable test is the first one, because it gets the ball rolling and lets you have some idea that automated builds are running, not just "compi9ling."
Very helpful video, big thank you. I wasn't able to figure it out from documentation alone.
self.assertEqual(calc.add(0.1, 0.2), 0.3)
Very informative and helpful. Thank you, Sir
really cool
Thanks Corey, this video was super helpful and right on time as I am starting with Python 👍🏻
am 17 and i think if somehow i manged to make a good career in this field, its all goes upto u sir…. and i wnna say that , cuz ain't able to give u any fund but i always watch the ad till the end..even if its 2 min long..
GOD BLESS U SIR !!!!!!
17:52 with my own code, it seems like you can only run one function in the "with self.assertRaises():" context manager. when i add another function call in the same context manager (without changing the inputs of the funcs tested) my tests always pass even when they should fail. can anybody confirm or explain why this is so?
Could you do a more comprehensive video on Mocking? I'd love to see it.
Everything about these videos are so polished, be it the title, thumbnail, or clear scripts
Hey Corey. Hope you're doing ok. Please consider doing a tutorial on advanced testing, with more in depth material on mocks, and other stuff like skipping tests and the like. Thanks.
Hi Corey, thanks for for this good job. I would like to see a tutorial like this on Pytest
23:13 Don't do that! Don't move test logic/given to setUp(). Yea, it might be shorter, and that might "feel" as if this method was created for it, but please don't do that. That creates coupling between tests (so the tests can influence each other, they can pass when run together and fail when run separately, they can silence bugs, etc.), and it introduces shared state. If you want to lower the duplication, please use a private method and create the test fixtures each time for each test (not once in SetUp, and re-use it).
Nice touch having a female employee name w/ higher employee salary than a male name in a coding video: a subtle/healthy way of breaking down stereotypes & expectations in the tech industry I think
Hi Corey, just a quick question about the mock tests… so in essense the mocked_get is only checking if the url we're calling is the correct url? and then the <self.assertEqual(schedule, 'success') is checking if we get a success response? how can that be when schedule = self.employee1.monthly_schedule('may')?
Hi, Corey I would like to request a video on python "line-profiling" the code
it works in the python terminal but not in the mac terminal
whenever I try running the test in mac terminal I get an error message saying that no module is named test_calc, or if I try run it with pip3 it says it cannot be found, can someone help?
Thank you so much. This is such a brilliant tutorial. As a beginner to programming struggling to understand what tests are and why they even matter, your video really helped me get more than just a conceptual understanding. Really appreciate you taking the time for such a detailed video.
Hey Corey,
I have one doubt. In below line and others in test_email method, shouldn't be 'self.emp_1.email()' instead on 'self.emp_1.email'. I am confused a bit. Please help me.
self.assertEqual(self.emp_1.email, 'Corey.Schafer@email.com')
Great 101 tutorial. I'd love to see a 201 version testing state-changing functions.
At 36:26, in my output, it says, "No module named 'requests' ". Any solution?
Very Usefull vedio cory……Thankyou!
This video finally helped me understand how to properly test my code thank you.
Thank you for your efforts in showing this interesting stuff. At 11:03 you are replacing several "add" with "substract". How can I make it? Can I also do it in VSCode?
I am getting attribute error: module ' unittest' has no attribute 'Testcase'
Just what I needed – especially the part on Mock tests. Very well put together
Hi Corey, I keep getting a traceback error right at the end saying "mocked_get.return_value.ok.text = 'Success' AttributeError: 'bool' object has no attribute 'text'" and the monthly_schedule function fails testing because of this. What exactly is going on?
Why am I not able to run all the 4 tests, but only test_email() test ?? I have copied Corey's code. Still why this is happening ? Can somebody please help me with this ?
Correy, can you please do a video on how to do unittest for flask app in your flask tutorial
38:22
I am still waiting for the pytest tutorial….
Really great video! I have been using unit test a little, but introducing mocking and proper use of teardowns and class methods really gave me a whole new perspective. Really informative stuff.
Please how do I run just one test? I know I want to use all as the ultimate validation, but in building a test, it seems like I might want to run the one under focus alone until I get it right. Is that possible?
think you
Your videos are really good! Great explanation. Please do one with Pytest and mocking
18:20 setting myself a timestamp because im sleeping rn its late will learn tomorrow, thanks Corey!
Will you be doing a pytest tutorial/series any time soon as the pandas series has come to an end? Also, love your channel.
If anyone has a problem with only one test being run when you run the test_employee.py file here's a fix. Replace this
if _name_ == '__main__':
unittest.main()
with this
def main():
TestEmployee()
if _name_ == '__main__':
main()
Fixed the problem for me.
Corey, please do a series on pytest. Thanks!
Hey Corey and friends,
Do we have to call _del_ on every object we create in setUp or our object is destroyed after each test, so we should not need to delete attributes ?
Like:-
~~~
class TestPlayer(unittest.TestCase):
def setUp(self):
self.player1 = Player('x')
self.player2 = Player('y')
def tearDown(self):
del self.player1
del self.player2
~~~
HOW TO IMPORT MODULES FROM OTHER FOLDERS
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
are you able to do @18:02 when you are using dictionary values ? if so how ?
thank you so much
hello, we are a Malaysia-China band, please check out our original music.
Hi I have coded up examples as in video. However in the test function below :
def test_monthly_schedule(self) :
with patch('employee.requests.get') as mocked_get :
I get error … Module not found error : no module named ‘employee’
all my code is same as example. so I cannot find why I get the error……… Would appreciate help !! Many thanks.