title: how to test file write with print statement in python
body: <p>I would like to test the following statement</p>
  
  <pre><code>with open('test.txt', 'w') as f:
      print(['abc', 20], end='', file=f)
  </code></pre>
  
  <p>I tried </p>
  
  <pre><code>  from __future__ import print_function                                              
    from mock import patch, mock_open                                                  
  
  
    def f():                                                                           
        with open('test.txt', 'w') as f:                                               
  
            # f.write(str(['abc', 20]))                                                 
            print(['abc', 20], end='', file=f)                                         
  
  
    @patch('__builtin__.open', new_callable=mock_open)                                 
    def test(mock_f):                                                                  
        f()                                                                            
        mock_f.assert_called_with('test.txt', 'w')                                     
        handle = mock_f()                                                              
        handle.write.assert_called_once_with(str(['abc', 20])) 
  </code></pre>
  
  <p>It complains that <code>write</code> is never called, which makes sense. In this case, what is the proper way to check the content for writing?</p>
  
  <p>I also tried to use <code>f.write(str(['abc', 20]))</code> instead of the print statement, which passes the test. Is it just a bad idea to use <code>print</code>?</p>