doctest, unittest, or if __name__='__main__'

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Mar 22 17:20:33 EST 2006


In <1142974528.143287.326360 at e56g2000cwe.googlegroups.com>, john_sips_tea
wrote:

> For writing testcode, it looks like there's three ways that it's
> typically done:
> 
> (1). using the doctest module,
> 
> (2). using the unittest module (i.e. "pyunit"), or else
> 
> (3). just putting an "if __name__ = '__main__':" at the bottom of your
> module containing code to manually run your class through its paces.
> 
> So, which way is preferred? Seems to me that unittest is the way to go,
> at least because it has you separate your test code from the code being
> tested.
> 
> If unittest is the standard way to write test code, why do we still
> have doctest? (I notice there's no mention in PEP 3000 of deprecating
> the doctest module).

I see it this way: `unittest` is for testing code and `doctest` is for
testing examples in docstrings.  If you put all your tests in the
docstrings there will be much code that's quite irrelevant for documenting
the function/method/class.

Testing real examples in doctstrings, or external documentation like
tutorials, is important because it's very frustrating for people reading
the docs if the examples don't work as advertised.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list