title: To Mock or Not to Mock when testing code that relies on third party libraries
body: <p>I have written a function which basically hides the complication of using the pySnmp library to send an SNMP message and it looks like:</p>
  
  <pre><code>snmpWalk(host,oid):
      from pysnmp.entity.rfc3413.oneliner import cmdgen
      cg = cmdgen.CommandGenerator()
      comm_data = cmdgen.CommunityData('my-manager','public')
      transport = cmdgen.UdpTransportTarget((host,161))
      variables = oid
      return cg.nextCmd(comm_data,transport,variables)
  </code></pre>
  
  <p>I am new to unit testing, and I was trying to write a unit test for this code as a doctest using minimock and this is what I came up with:</p>
  
  <pre><code>#Setup the mocks
  &gt;&gt;&gt; from minimock import mock, Mock
  &gt;&gt;&gt; cmdgen.CommandGenerator = Mock('cmdgen.CommandGenerator')
  &gt;&gt;&gt; cmdgen.CommunityData = Mock('cmdgen.CommunityData')
  &gt;&gt;&gt; cmdgen.UdpTransportTarget = Mock('cmdgen.UdpTransportTarget')
  &gt;&gt;&gt; cgMock = Mock('cgMock')
  &gt;&gt;&gt; cmdgen.CommandGenerator.mock_returns = cgMock
  &gt;&gt;&gt; cmdgen.CommunityData.mock_returns = 1
  &gt;&gt;&gt; cmdgen.UdpTransportTarget.mock_returns = 2
  &gt;&gt;&gt; cgMock.nextCmd.mock_returns = (1,2,3,4)
  #run the test
  &gt;&gt;&gt; PrinterMonitor.SnmpWalk('192.0.0.1','1.1.1.1.1.1.1') 
  Called cmdgen.CommandGenerator()
  Called cmdgen.CommunityData('my-manager', 'public')
  Called cmdgen.UdpTransportTarget(('192.0.0.1', 161))
  Called cgMock.nextCmd(1, 2, (1, 1, 1, 1, 1, 1, 1))
  (1, 2, 3, 4)
  </code></pre>
  
  <p>So I run this test and it works out fine, but my question is, <strong>do I really need to go to all this effort in mocking every aspect of the 3rd party library</strong>? In my mind the minimum I would need to mock is the final line <code>cg.nextCmd(comm_data,transport,variables)</code> because I don't actually want to start firing off messages to printers in my unit tests, but the other calls to the library code should give me consistent results, depending on what I pass to them. I can make sure that my code interacts appropriately with the library (i.e. make sure that it passes the host argument to the transport variable correctly, by asserting this on the object passed to the mocked <code>cg.nextCmd(...)</code> call?</p>