Embedding unit tests in source files?

Roy Smith roy at panix.com
Sun Jun 17 17:15:46 CEST 2007


I'm starting a new project and am thinking of embedding my unit tests
right in the source files.  I've used unittest before, and I'm happy
with it, but I've always used it with the source code in one file and
the unit tests in another.  I figure if I just put a

if __name__ == '__main__":
    import unittest
    blah, blah, blah

block at the end of each source file, I'll end up with a cleaner
project structure.  I can run the unit tests just by executing each
source file.  Any comments from people who have organized their Python
projects this way?  Did it work out well?

It seems like what you give up is the ability to aggregate tests in a
hierarchy of test suites.  In theory, that sounds like something you
might want to do, but in practice, I've never found it that useful, so
I don't think I'll miss it.

I know about doctest, but I'm happy with unittest and don't see any
reason to change.




More information about the Python-list mailing list