Unit testing is a method of determining the correctness of a single function isolated from a larger codebase. The idea is that if all the atomic units of an application work as intended in isolation, then integrating them together as intended is much easier.
Unit testing is just one form of testing that works in combination with other testing approaches to wring out the bugs from a piece of software being developed. When several functions and classes are put together it's often difficult to determine the source of a problem if several bugs are occurring at the same time. Unit testing helps eliminate as many of the individual bugs as possible so when the application comes together as a whole the separate parts work as correct as possible. Then when issues arise they can often be tracked down as unintended consequences of the disparate pieces not fitting together properly.
There are many tools for creating tests in Python. Some of these tools, such as pytest, replace the built-in unittest framework. Other tools, such as nose, are extensions that ease test case creation. Note that many of these tools are also used for integration testing by writing the test cases to exercise multiple parts of code at once.
unittest is the built-in standard library tool for testing Python code.
pytest is a complete testing tool that emphasizes backwards-compatibility and minimizing boilerplate code.
nose is an extension to unittest that makes it easier to create and execute test cases.
Hypothesis is a unit test-generation tool that assists developers in creating tests that exercise edge cases in code blocks. The best way to get started using Hypothesis is by going through the well-written quickstart.
testify was a testing framework meant to replace the common unittest+nose combination. However, the team behind testify is transitioning to pytest so it's recommended you do not use testify for new projects.
Dive into Python 3's chapter on unit testing has a complete example with code and a detailed explanation for creating unit testing with the unittest module.
Unit Testing Your Twilio App Using Python’s Flask and Nose is a detailed tutorial for using the nose test runner for ensuring a Flask application is working properly.
Understanding unit testing explains why testing is important and shows how to do it effectively in your applications.
Unit testing with Python provides a high-level overview of testing and has diagrams to demonstrate what's going on in the testing cycle.
The Python wiki has a page with a list of Python testing tools and extensions.
Working Effectively with Unit Tests is an interview with the author of a book by the title where he shares some of the insight he's learned on the topic.
Generate your tests
shows how to write a test generator that works with the unittest
framework.
An Extended Introduction to the nose Unit Testing Framework shows how this test runner can be used to write basic test suites. While the article is from 2006, it remains relevant today for learning how to use nose with your projects.
Searching for a complete, step-by-step deployment walkthrough? Learn more about The Full Stack Python Guide to Deployments book.