title: mocking a standard library calls in py.test
body: <p>I'm learning how to write tests with py.test, but I can't get idea how to mock external calls.</p>
  
  <p>Let say I have a code for testing:</p>
  
  <p>app.py:</p>
  
  <pre><code>import random
  
  def trade_robot(stock, price):
      return ' '.join((random.choice(('buy', 'sell', 'keep')), stock))
  </code></pre>
  
  <p>And I want to be sure I check case for random.choice deciding I need to buy (and I want to check it properly join strings for 'buy' case).</p>
  
  <p>my test code should look like this:</p>
  
  <pre><code>import pytest
  from app import trade_robot
  import sys
  
  def test_buy_case():
      # some mock magic here
      assert trade_robot('AAPL', 500) == 'buy AAPL'
  
  if __name__ == "__main__":
      pytest.main("-v %s" % sys.argv[0])
  </code></pre>
  
  <p>What should be in 'some mock magic' to make this test passing every time? Thanks!</p>