title: Conditional mocking: Call original function if condition does match
body: <p>How can I conditionally call the original method in a mock?</p>
  <p>In this example I only want to fake a return value if <code>bar=='x'</code>. Otherwise I want to call the original method.</p>
  <pre><code>def mocked_some_method(bar):
      if bar=='x':
          return 'fake'
      return some_how_call_original_method(bar)
  
  with mock.patch('mylib.foo.some_method', mocked_some_method):
      do_some_stuff()
      
  </code></pre>
  <p>I know that it is a bit strange. If I want to fake <code>mylib.foo.some_method</code> in side <code>do_some_stuff()</code> it should be condition-less. All (not some) calls to <code>some_method</code> should be mocked.</p>
  <p>In my case it is an integration test, not a s tiny unittest and <code>mylib.foo.some_method</code> is a kind of dispatcher which gets used very often. And in one case I need to fake the result.</p>
  <h1>Update</h1>
  <p>I wrote this question four years ago. Today, it feels very strange to do conditional mocking. Mocks should only get used in tests. Tests (and production code) should be simple and small. Tests should be conditionless. As I wrote this question, we still used huge production methods and long test. Today, I follow these rules (simple methods, conditionless tests ...). I wrote my findings down: <a href="https://github.com/guettli/programming-guidelines/" rel="noreferrer">my programming guidelines</a></p>