title: Mocking __main__
body: <p>I would like to ensure with tests that:
  - the application cannot be imported
  - the application can be started as a real application (i.e: python src.py)</p>
  
  <p>I'm interested about that, why the following is not working:</p>
  
  <p>src.py</p>
  
  <pre><code>class A:
   def x(self):
    print('this is x')
  
  if __name__ == '__main__':
    A().x()
  </code></pre>
  
  <p>test.py (snippet)</p>
  
  <pre><code>class Test(unittest.TestCase):
    @mock.patch('src.A.x')
    def test_main(self, mock_x):
      import src
      mock_x.assert_any_call()
  </code></pre>
  
  <p>This test fails... why?</p>