Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Testing Your Code With Python's unittest (Summary)

Now you know how to write unit tests for your Python code using the unittest testing framework. This framework comes with the Python standard library, so you don’t have to install third-party packages to start writing your tests.

The unittest framework supports several useful features that you’ve also learned how to use. These features include test cases, fixtures, test suites, test discovery, and more.

In this video course, you’ve learned how to:

  • Create unittest tests with the TestCase class
  • Explore the assert methods on the TestCase class
  • Run your tests using the command-line interface of unittest
  • Group test cases using the TestSuite class
  • Handle setup and teardown logic using fixtures

With this knowledge, you’re ready to start writing robust unit tests for your Python code without needing to install additional packages.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

00:00 In the previous lesson, I showed you how to set your tests up by using the fixture methods. This is the last lesson of the course. In it, I’ll summarize what you learned and point you at where you can continue your automated testing journey.

00:12 If your projects require more than short one-off scripts, then you should be writing automated tests to go with them. Python’s standard library includes tools for doing automated unit tests that can be found in the appropriately named unittest module.

00:27 Python’s test runner has a discovery process. It looks for tests that are in a Python file whose name begins with test, possibly in a subdirectory. Then inside those files, it looks for classes that inherit from unittest.TestCase.

00:42 Any method in the class that starts with test is what gets executed as a test. Tests fail if the method raises an exception. The TestCase class comes with a wide selection of assertion methods that allow you to compare your expected results with the ones you obtained.

00:58 The assertion methods raise an exception if the comparison fails, thus triggering test failure. In addition to covering the basic structure of tests and the assertion methods, this course showed you a bunch of ways to control your tests, including how to skip them, skip them conditionally, use subtests to augment your errors with extra information, and how to prepare data to use with your tests through the setUp() and tearDown() fixture methods.

01:24 This course is based on a long article by Leodanis Pozo Ramos. He always writes really good, really in-depth content. If you’re looking for more examples, you might want to check the article out.

01:35 It also covers a couple of things I did not, including how to group test cases together in a suite, some hints on dealing with tests that are failing, and how to fake out objects like dates that can make testing tricky.

01:48 That last bit uses the unittest.mock library, and one of the reasons I didn’t handle it in this course is because, well, there’s a whole other course dedicated to it.

01:57 Or, if you’re tired of hearing me drone on — it’s one of mine as well — you can read the tutorial that goes along with it.

02:04 If you’re writing a web application, you need extra tools to test it. For example, it would be handy to be able to launch your browser and click on things.

02:13 One of the more popular libraries for doing that is Selenium, and this tutorial shows you how to do just that. A lot of coders feel that the unittest library is too verbose.

02:24 A popular third-party alternative is pytest. It uses functions instead of classes and decorators to handle fixture features. It also overloads Python’s assert keyword to give better error messages, so you use that instead of the assertion methods I covered in unittest.

02:41 There’s both a course and a tutorial on pytest if you’re interested. Fair warning, that one’s me as well.

02:48 That’s all for now. I hope you enjoyed the content. On a personal note, this is my 80th video course for Real Python. I’d like to thank my fellow podcast host, Christopher Bailey, who’s been my editor throughout this journey and has been just awesome to work with.

03:03 I’d also like to thank Real Python’s chief editor, Dan Bader, and all the rest of the Real Python crew. It’s been quite a journey. Looking forward to 80 more.

Become a Member to join the conversation.