Python Nosetest - 如何模拟MySQLdb

时间:2016-06-04 19:55:05

标签: python mysql tdd nose

我正在尝试从我的项目中模拟MySQLdb lib,但我真的不明白该怎么做:

这是我的测试代码:

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()

这是我的班级:

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: 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)

任何人都知道测试第三方库时的正确方法吗?

谢谢!

0 个答案:

没有答案