[Tutor] testing: doctest and unittest

Kent Johnson kent37 at tds.net
Wed Nov 9 04:16:26 CET 2005


Alex Hunsley wrote:
> Regards testing, I've been playing with both the unittest 
> (http://pyunit.sourceforge.net/pyunit.html) and doctest 
> (http://docs.python.org/lib/module-doctest.html). I was wondering what 
> peoples thoughts were on the effectiveness and convenience of one versus 
> the other. It seems to me that doctest is good for quicky and 
> straightforwards input/output tests for small units, whereas unittest 
> would be good for dynamic or complicated testing.
> 
> Where do you seasoned pythonites see unittest and doctest in relation to 
> each other? Do you only use one or the other?

I think it is mostly personal preference. Doctest is nice where you create examples for others, maybe not so nice where you are creating exhaustive unit tests trying to exercise every corner case. unittest is perhaps easier to aggregate tests from multiple modules. Doctests integrate with unittest in Python 2.4. Personally I use unittest but I come from a Java background and learned TDD with JUnit which is related to unittest.

Another option is py.test which is part of the PyPy project, not part of the standard library. Some people think it is simpler and more Pythonic than unittest. This page has good articles about doctest, unittest and py.test:
http://agiletesting.blogspot.com/2005/11/articles-and-tutorials-page-updated.html

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list