Are you mocking me?
Jul 19 2013
3 minutes read

Unit testing is one of the most important parts of any project. TDD (test driven development) is a good habit to cultivate, in case you are not convinced, you don't know about TDD or you have never given it a try. I started using partial TDD recently, yes only partial for what I follow is that first I write a rough outline of the code, then I write tests and then I keep on re-factoring my code till all the tests are passing. Though there is a little flaw in this method according to me -- although writing tests are mostly trivial with those easy assert statements, sometimes you may write wrong tests which make you write wrong code too. So put more thought in writing tests than in writing code. Although many times you will agree with me that TDD is annoying, it does not let you do what you actually want to do (I mean the "way" you want to do it, the result may be same).

So in this post I am going to talk about mocks. What are mocks? There are some magical functions which do some abracadabra on your existing function and make them return anything that you want without actually even thinking what they are returning. Did I personify it a little too much? Let me explain in a little detail with the example of where I used mocking.

Unit tests should be independent of environment and any other module, so that each module can be developed separately without affecting the development of other modules. But many a times one module depends on others and in that case it gets messy to test those functions. In my case it was the "signature" rule which uses "gpg" utility to verify signature of messages. Here is a snippet of the code:

from mailman.utilities.gpg import GPG
class Signature:
    """Look for pgp-signature."""

    description=_('The message has pgp-signature as application/pgp-signature part')
    record = True
    def __init__(self):
        self._gpg = None

    def check(self, mlist, msg, msgdata):
        """See `IRule"""
        gpg_dir = os.path.join(config.VAR_DIR, 'gpg')
        self._gpg = GPG(gpg_dir)

So if you see in line 1 we import the GPG class from gpg utility. But when we write tests we don't want this function to "actually" create an instance of GPG class and "actually" verify the signature because it is not a function of signature rule and any errors or breakpoints in gpg utility may cause our tests for the "working" signature rule to fail. So what do we do? We mock and then we rock ;-). We mock the GPG instance created in signature rule to return "any specific value" irrespective of the input. See the code below:

import mock

class TestSignatureMime(unittest.TestCase):
    """Test the signature handler."""

  class Dummy_GPG_True:    
      def __init__(self, dir):
          self._dir = dir
      def verify_inline_signature(self, data):
          return True

      def verify_detached_signature(self, data):
          return True

  @mock.patch('mailman.rules.signature.GPG', new=Dummy_GPG_True)
  def test_good_signature(self):
      result = self._rule.check(self._mlist, self._msg, {})

Back to posts