Embedding unit tests in source files?

R

Roy Smith

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
BrentonMcc
Top