title: Python: How to run unittest.main() for all source files in a subdirectory?
body: <p>I am developing a Python module with several source files, each with its own test class derived from <a href="http://docs.python.org/library/unittest.html" rel="noreferrer">unittest</a> right in the source.  Consider the directory structure:</p>
  
  <pre><code>dirFoo\
      test.py
      dirBar\
          __init__.py
          Foo.py
          Bar.py
  </code></pre>
  
  <p>To test either Foo.py or Bar.py, I would add this at the end of the Foo.py and Bar.py source files:</p>
  
  <pre><code>if __name__ == "__main__":
      unittest.main()
  </code></pre>
  
  <p>And run Python on either source, i.e.</p>
  
  <pre><code>$ python Foo.py
  ...........
  ----------------------------------------------------------------------
  Ran 11 tests in 2.314s
  
  OK
  </code></pre>
  
  <p>Ideally, I would have "test.py" automagically search dirBar for any unittest derived classes and make one call to "unittest.main()".  What's the best way to do this in practice?  </p>
  
  <p>I tried using Python to call <a href="http://docs.python.org/library/functions.html#execfile" rel="noreferrer">execfile</a> for every *.py file in dirBar, which runs once for the first .py file found &amp; exits the calling test.py, plus then I have to duplicate my code by adding unittest.main() in every source file--which violates DRY principles.  </p>