Python 2.7 mock library and web2py

64 views
Skip to first unread message

Robin Bryce

unread,
Apr 16, 2017, 10:37:11 AM4/16/17
to web2py-users
Hi,

I'm attempting to use the python mock library with some unittest based tests for a web2py application. It feels like I'm going against the grain. 

General question: Is it generally a pain to use mock with web2py applications or am I missing some trick to get it working ?

Specifically, in models/db.py I have

from gluon.contrib.login_methods.ldap_auth import ldap_auth

in a unittest implementation I have

def mock_ldap_auth(*args,**kw):
    return True

mock_ldap_auth = mock.patch(
        'db.ldap_auth',
        side_effect=mock_ldap_auth)


I'm using web2py_utils and webtest to drive unittest.

class TestSomething(unittest.TestCase):
    # __init__ method which intialises webtest

    def setUp(self):
        p = mock_ldap_auth.start()
        self.addCleanup(p.stop)

The call to 'mock_ldap_auth.start()' produces this traceback::

Traceback (most recent call last):
  File "C:\Projects\web2py\applications\icss/tests\sign_infra.py", line 49, in setUp
    p = mock_ldap_auth.start()
  File "C:\Projects\pyenvs\icss\lib\site-packages\mock\mock.py", line 1500, in start
    result = self.__enter__()
  File "C:\Projects\pyenvs\icss\lib\site-packages\mock\mock.py", line 1353, in __enter__
    self.target = self.getter()
  File "C:\Projects\pyenvs\icss\lib\site-packages\mock\mock.py", line 1523, in <lambda>
    getter = lambda: _importer(target)
  File "C:\Projects\pyenvs\icss\lib\site-packages\mock\mock.py", line 1206, in _importer
    thing = __import__(import_path)
  File "C:\Projects\web2py\gluon\custom_import.py", line 89, in custom_importer
    raise ImportError, 'Cannot import module %s' % str(e)
ImportError: Cannot import module 'applications.icss.modules.db'

Note: My purpose in asking this question is to avoid ugly and unnecessary case specific work arounds.

Thanks,

Robin

Marlysson Silva

unread,
Apr 18, 2017, 6:50:36 AM4/18/17
to web2py-users
Do you have a file called db in your modules folder in your application?

Robin Bryce

unread,
Apr 25, 2017, 7:41:31 AM4/25/17
to web2py-users
Sorry, I missed your reply.

I do not have modules/db.py 

I was attempting to mock the definition of 'ldap_auth' in models/db.py (It's not essential that the definition live there.)

It looks like when the tests/sign_infra.py mock is applied for "db.ldap_auth", the web2py custom import hook gets involved and doesn't resolve models/db.py

I'm not clear on the effective namespace of my web2py app, which I need to understand to define mocks. If there are *some* places where I can apply mocks and not others thats fine, I'd just like a little help working out where.

I'm currently relying on full request testing (functional) which is broader than I would like and forced me to put an "if TEST_APP" predicate in production code. Again, that's ok but I'd like to have more granular tests.

Cheers,

Robin
Reply all
Reply to author
Forward
0 new messages