Fix Python – Python – doctest vs. unittest [closed]

Question

Asked By – Sean

I’m trying to get started with unit testing in Python and I was wondering if someone could explain the advantages and disadvantages of doctest and unittest.

What conditions would you use each for?

Now we will see solution for issue: Python – doctest vs. unittest [closed]


Answer

Both are valuable. I use both doctest and nose taking the place of unittest. I use doctest for cases where the test is giving an example of usage that is actually useful as documentation. Generally I don’t make these tests comprehensive, aiming solely for informative. I’m effectively using doctest in reverse: not to test my code is correct based on my doctest, but to check that my documentation is correct based on the code.

The reason is that I find comprehensive doctests will clutter your documentation far too much, so you will either end up with either unusable docstrings, or incomplete testing.

For actually testing the code, the goal is to thoroughly test every case, rather than illustrate what is does by example, which is a different goal which I think is better met by other frameworks.

This question is answered By – Brian

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0