0

I'm using Django 1.3 and need to check the output and number of interactions in my logging system. For logging I'm using Django-Sentry though it appears that it's working just like the regular Python logger.

I'm using python-mockito for mocking and if possible I would like to check the number of times different methods have been called and the messages they return.

I'm trying to achieve a check that does something like:

from foo import views
logger = mock()
views.logger = logger
do_method()
verify(logger).error(any, any)
do_method()
verifyZeroInteractions(logger)

Also being able to check the parameters would be nice.

models.py:

from django.db import models
import logging
from sentry.client.handlers import SentryHandler

logger = logging.getLogger(__name__)

try:
    is_logging_setup = True
except NameError:
    is_logging_setup = True
    logger.setLevel(settings.LOGGING_LEVEL)
    logger.addHandler(SentryHandler())

class Foo(models.Model):
    def bar(self):
        logger.warning("Rawr", 'extra': { 'data': 'foo' })

tests.py:

class TestModelFoo(TestCase):
    def setUp(self):
        self.foo = Foo()

    def test_getting_logged(self):
        self.foo.bar()
        # Check the log output.

Any suggestions on how I can catch the output?

Kit Sunde
  • 35,972
  • 25
  • 125
  • 179

1 Answers1

0

Here's some code that does just that with the standard mock python library.

    with mock.patch('infra.manifest.fake_put') as fake_patch:
        infra.manifest.copy_files(root, files, folder, True)

    args, kwargs = fake_patch.call_args

    self.assertEqual((u'/etc/a.tmp', u'/tmp/a.tmp'), args)
    self.assertEqual({'use_sudo': True}, kwargs)

It's the mock.patch method you're interested in. I think there are other frameworks where you can specify a passthrough keyword which will also call the original method, this one will turn the patched method into a mock call and not call the original method.

mjallday
  • 9,796
  • 9
  • 51
  • 71