title: Log Unittest output to a text file
body: <p>I am trying to log the output of tests to a text file. I am using the unittest module and want to log results into a text file instead of the screen. I have some script here to explain what has been tryied so far. This is the test script.</p>
  
  <pre><code>import unittest, sys
  
  class TestOne(unittest.TestCase):
  
      def setUp(self):
          self.var = 'Tuesday'
      def tearDown(self):
          self.var = None 
  
  
  
  class BasicTestOne(TestOne):
  
      def runTest(self):
  
          TestOne.setUp(self)
          self.assertEqual(self.var, 'Tuesday')
  
  
  
  class AbsoluteMoveTestSuite(unittest.TestSuite):
  
      # Tests to be tested by test suite
      def makeAbsoluteMoveTestSuite():
          suite = unittest.TestSuite()
          suite.addTest(TestOne("BasicTestOne"))
  
          return suite 
  
      def suite():
          return unittest.makeSuite(TestOne)
  
  
  if __name__ == '__main__':
      unittest.main()
  </code></pre>
  
  <p>I have added this to the file but it doesn't seem to work.</p>
  
  <pre><code>log_file = 'log_file.txt'
  sys.stout = sys.sterr = open(log_file, 'w')
  
  return suite &gt;&gt; sys.stout
  </code></pre>
  
  <p>and also:</p>
  
  <pre><code>log_file = 'log_file.txt'
  return suite &gt;&gt; open(log_file, 'w')
  </code></pre>
  
  <p>I have tried several different versions of this command.</p>
  
  <pre><code>if __name__ == '__main__':
      unittest.main() &gt;&gt; open(log_file, 'w')
  </code></pre>
  
  <p>I have tried this. I want the log file to be stored and created inside the python script. I do not want to have to call <code>python tests.py &gt;&gt; log_file.txt</code>.</p>
  
  <p>Thanks for any help</p>