1

I'm trying to mock the MySQLdb lib from my project but I don't really understood how to do it:

Here's my test code:

import unittest
from mock import MagicMock
from core.distrib.main.CentralDistDraw import CentralDistDraw
from connector.mysql  import MySQL

class CentralDistDraw(unittest.TestCase):
    def test_hello(self):
        draw = CentralDistDraw()
        msql = MySQL()
        msql.getConnection = MagicMock()

Here's my class:

from datetime import datetime
import logging
from core.distrib.config.PromoConstants import PromoConstants
from dateutil.parser import parse
from google.appengine.api import memcache
from connector.mysql import MySQL
from core.distrib.ndb.userpromo import UserPromo
from core.distrib.ndb.drawinfo import DrawInfo
import traceback

class CentralDistDraw(object):
    def getNextNumber(self, idUser, amountDefault=1, amountSpecial=1):

        numbersDelivered = []
        # DB CONTROLL....
        db = MySQL().getConnection()
        db.autocommit(False)
        currentUser = None

Error thrown:

ERROR: Failure: ImportError (/home/dyego/Documents/code-stuff/mod/pdbcontest2/src/lib/_mysql.so: invalid ELF header)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose-1.3.7-py2.7.egg/nose/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python2.7/dist-packages/nose-1.3.7-py2.7.egg/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python2.7/dist-packages/nose-1.3.7-py2.7.egg/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/dyego/Documents/code-stuff/mod/pdbcontest2/src/tests/core/test_central_dist_draw.py", line 4, in <module>
    from core.distrib.main.CentralDistDraw import CentralDistDraw
  File "/home/dyego/Documents/code-stuff/mod/pdbcontest2/src/core/distrib/main/CentralDistDraw.py", line 11, in <module>
    from connector.mysql import MySQL
  File "/home/dyego/Documents/code-stuff/mod/pdbcontest2/src/connector/mysql.py", line 8, in <module>
    import MySQLdb
  File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: /home/dyego/Documents/code-stuff/mod/pdbcontest2/src/lib/_mysql.so: invalid ELF header

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

Anyone knows the correct way to go when comes to testing third party libraries?

Thanks!

SupimpaAllTheWay
  • 1,308
  • 3
  • 16
  • 22
  • You aren't actually injecting the mocks into the code under test (or, for that matter, testing anything in your tests). – jonrsharpe Jun 04 '16 at 20:02
  • Thanks @jonrsharpe! But could you please demonstrate to me how to do it, or link me to a tutorial? I found out how to mock regular methods but I'm not able to mock the mysql lib (or any other 3rd library) – SupimpaAllTheWay Jun 04 '16 at 20:16
  • Start with e.g. http://stackoverflow.com/questions/23112199/python-mocking-third-party-modules – jonrsharpe Jun 04 '16 at 20:18
  • or https://docs.python.org/3/library/unittest.mock-examples.html#mocking-classes – studioj Jun 06 '16 at 11:27

0 Answers0