excluding unit test code

Remco Gerlich scarblac at pino.selwerd.nl
Tue Apr 3 03:01:49 EDT 2001


Steven Haryanto <steven at haryan.to> wrote in comp.lang.python:
> Since an XP-faithful coder can write lots of test suites,
> is there a simple way to exclude these code in a module
> from the production code?

A simple idiom is 

if __name__ == '__main__':
  # tests here
  
That way the scripts are run when the module is run by itself, but not when
it is imported.

Also, __debug__ is usually 1, but 0 when the -O (optimize) command line
switch is used. -O also removes assert commands. -O -O discards doc strings
as well. So you could put your tests under

if __debug__:
  # tests here

And use your production code with the -O command line option.

-- 
Remco Gerlich



More information about the Python-list mailing list