Python Tutorial: Unit Testing Your Code with the unittest Module

///Python Tutorial: Unit Testing Your Code with the unittest Module

Python Tutorial: Unit Testing Your Code with the unittest Module

FavoriteLoadingAdd to favorites

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

By |2020-10-12T10:02:49+00:00October 12th, 2020|Python Video Tutorials|47 Comments

47 Comments

  1. feliponte October 12, 2020 at 10:02 am - Reply

    11:06 How do you do that?

  2. spacejihad October 12, 2020 at 10:02 am - Reply

    How did he do that? (Shortcut pls) 11:03

  3. Dennis Gentry October 12, 2020 at 10:02 am - Reply

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

  4. TTŠ October 12, 2020 at 10:02 am - Reply

    Very helpful video, big thank you. I wasn't able to figure it out from documentation alone.

  5. arth October 12, 2020 at 10:02 am - Reply

    self.assertEqual(calc.add(0.1, 0.2), 0.3)

  6. Joydeep Chakraborty October 12, 2020 at 10:02 am - Reply

    Very informative and helpful. Thank you, Sir

  7. Jagadeesh October 12, 2020 at 10:02 am - Reply

    really cool

  8. Felipe Quintero October 12, 2020 at 10:02 am - Reply

    Thanks Corey, this video was super helpful and right on time as I am starting with Python 👍🏻

  9. SWAYAM October 12, 2020 at 10:02 am - Reply

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

  10. spongechameleon October 12, 2020 at 10:02 am - Reply

    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?

  11. f00 dog October 12, 2020 at 10:02 am - Reply

    Could you do a more comprehensive video on Mocking? I'd love to see it.

  12. abhinav chavali October 12, 2020 at 10:02 am - Reply

    Everything about these videos are so polished, be it the title, thumbnail, or clear scripts

  13. Simbarashe Makahamadze October 12, 2020 at 10:02 am - Reply

    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.

  14. New Hac October 12, 2020 at 10:02 am - Reply

    Hi Corey, thanks for for this good job. I would like to see a tutorial like this on Pytest

  15. Daniel Wilkowski October 12, 2020 at 10:02 am - Reply

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

  16. Sean October 12, 2020 at 10:02 am - Reply

    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

  17. Nehme gadaoun October 12, 2020 at 10:02 am - Reply

    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')?

  18. ML Life October 12, 2020 at 10:02 am - Reply

    Hi, Corey I would like to request a video on python "line-profiling" the code

  19. mason marcobello October 12, 2020 at 10:02 am - Reply

    it works in the python terminal but not in the mac terminal

  20. mason marcobello October 12, 2020 at 10:02 am - Reply

    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?

  21. Burner918 October 12, 2020 at 10:02 am - Reply

    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.

  22. Aman Saxena October 12, 2020 at 10:02 am - Reply

    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')

  23. Brett Monroe October 12, 2020 at 10:02 am - Reply

    Great 101 tutorial. I'd love to see a 201 version testing state-changing functions.

  24. Mashuk Ahmed October 12, 2020 at 10:02 am - Reply

    At 36:26, in my output, it says, "No module named 'requests' ". Any solution?

  25. Pranav Gandhi October 12, 2020 at 10:02 am - Reply

    Very Usefull vedio cory……Thankyou!

  26. Skyler Brown October 12, 2020 at 10:02 am - Reply

    This video finally helped me understand how to properly test my code thank you.

  27. nickl October 12, 2020 at 10:02 am - Reply

    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?

  28. Vrushabh Borhade October 12, 2020 at 10:02 am - Reply

    I am getting attribute error: module ' unittest' has no attribute 'Testcase'

  29. Edward Milne October 12, 2020 at 10:02 am - Reply

    Just what I needed – especially the part on Mock tests. Very well put together

  30. Daniel Hazell October 12, 2020 at 10:02 am - Reply

    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?

  31. Ketan Kulkarni October 12, 2020 at 10:02 am - Reply

    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 ?

  32. Mootje October 12, 2020 at 10:02 am - Reply

    Correy, can you please do a video on how to do unittest for flask app in your flask tutorial

  33. Kudo AMV October 12, 2020 at 10:02 am - Reply

    38:22
    I am still waiting for the pytest tutorial….

  34. Christian Wilgaard October 12, 2020 at 10:02 am - Reply

    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.

  35. Xeno Campanoli October 12, 2020 at 10:02 am - Reply

    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?

  36. Malik Abu Shabab October 12, 2020 at 10:02 am - Reply

    think you

  37. Yuri Uehara October 12, 2020 at 10:02 am - Reply

    Your videos are really good! Great explanation. Please do one with Pytest and mocking

  38. Osama Akhtar October 12, 2020 at 10:02 am - Reply

    18:20 setting myself a timestamp because im sleeping rn its late will learn tomorrow, thanks Corey!

  39. Friedrich Nietzsche October 12, 2020 at 10:02 am - Reply

    Will you be doing a pytest tutorial/series any time soon as the pandas series has come to an end? Also, love your channel.

  40. Friedrich Nietzsche October 12, 2020 at 10:02 am - Reply

    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.

  41. Kishore Patil October 12, 2020 at 10:02 am - Reply

    Corey, please do a series on pytest. Thanks!

  42. Sandeep Yadav October 12, 2020 at 10:02 am - Reply

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

  43. 17ucs018 khilari hemanshu October 12, 2020 at 10:02 am - Reply

    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

  44. Manuel Guzman October 12, 2020 at 10:02 am - Reply

    are you able to do @18:02 when you are using dictionary values ? if so how ?

  45. Jae Oppa October 12, 2020 at 10:02 am - Reply

    thank you so much

  46. The Zoul Band_奏乐队 October 12, 2020 at 10:02 am - Reply

    hello, we are a Malaysia-China band, please check out our original music.

  47. Mohammed Arshad October 12, 2020 at 10:02 am - Reply

    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.

Leave A Comment

*