title: How do I run all Python unit tests in a directory?
body: <p>I have a directory that contains my Python unit tests. Each unit test module is of the form <strong>test_*.py</strong>. I am attempting to make a file called <strong>all_test.py</strong> that will, you guessed it, run all files in the aforementioned test form and return the result. I have tried two methods so far; both have failed. I will show the two methods, and I hope someone out there knows how to actually do this correctly.</p>
  
  <p>For my first valiant attempt, I thought "If I just import all my testing modules in the file, and then call this <code>unittest.main()</code> doodad, it will work, right?" Well, turns out I was wrong.</p>
  
  <pre><code>import glob
  import unittest
  
  testSuite = unittest.TestSuite()
  test_file_strings = glob.glob('test_*.py')
  module_strings = [str[0:len(str)-3] for str in test_file_strings]
  
  if __name__ == "__main__":
       unittest.main()
  </code></pre>
  
  <p>This did not work, the result I got was:</p>
  
  <pre><code>$ python all_test.py 
  
  ----------------------------------------------------------------------
  Ran 0 tests in 0.000s
  
  OK
  </code></pre>
  
  <p>For my second try, I though, ok, maybe I will try to do this whole testing thing in a more "manual" fashion. So I attempted to do that below:</p>
  
  <pre><code>import glob
  import unittest
  
  testSuite = unittest.TestSuite()
  test_file_strings = glob.glob('test_*.py')
  module_strings = [str[0:len(str)-3] for str in test_file_strings]
  [__import__(str) for str in module_strings]
  suites = [unittest.TestLoader().loadTestsFromName(str) for str in module_strings]
  [testSuite.addTest(suite) for suite in suites]
  print testSuite 
  
  result = unittest.TestResult()
  testSuite.run(result)
  print result
  
  #Ok, at this point I have a result
  #How do I display it as the normal unit test command line output?
  if __name__ == "__main__":
      unittest.main()
  </code></pre>
  
  <p>This also did not work, but it seems so close!</p>
  
  <pre><code>$ python all_test.py 
  &lt;unittest.TestSuite tests=[&lt;unittest.TestSuite tests=[&lt;unittest.TestSuite tests=[&lt;test_main.TestMain testMethod=test_respondes_to_get&gt;]&gt;]&gt;]&gt;
  &lt;unittest.TestResult run=1 errors=0 failures=0&gt;
  
  ----------------------------------------------------------------------
  Ran 0 tests in 0.000s
  
  OK
  </code></pre>
  
  <p>I seem to have a suite of some sort, and I can execute the result. I am a little concerned about the fact that it says I have only <code>run=1</code>, seems like that should be <code>run=2</code>, but it is progress. But how do I pass and display the result to main? Or how do I basically get it working so I can just run this file, and in doing so, run all the unit tests in this directory?</p>