This chapter from the “Hitchhiker’s Guide to Python” provides recommendations on how to perform unit tests in Python and presents the unitest and doctest modules. It also lists the tools that are available to perform unit testing in Python.
Amongst the general rules of testing that are provided for Python, you can find:
* A testing unit should focus on one tiny bit of functionality and prove it correct.
* Each test unit must be fully independent. Each of them must be able to run alone, and also within the test suite, regardless of the order they are called. The implication of this rule is that each test must be loaded with a fresh dataset and may have to do some cleanup afterwards.
* Learn your tools and learn how to run a single test or a test case. Then, when developing a function inside a module, run this function’s tests very often, ideally automatically when you save the code.
* Always run the full test suite before a coding session, and run it again after. This will give you more confidence that you did not break anything in the rest of the code.
* If you are in the middle of a development session and have to interrupt your work, it is a good idea to write a broken unit test about what you want to develop next.
Read the complete resource on https://python-guide.readthedocs.org/en/latest/writing/tests.html