【发布时间】:2016-05-28 10:25:18
【问题描述】:
我正在使用 sqlalchemy 来查询我的数据库中的一个项目。并排,我是单元测试的新手,我正在尝试学习如何进行单元测试来测试我的数据库。我尝试使用模拟库进行测试,但到目前为止,这似乎非常困难。
所以我编写了一段代码来创建一个Session 对象。该对象用于连接数据库。
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.exc import OperationalError, ArgumentError
test_db_string = 'postgresql+psycopg2://testdb:hello@localhost/' \
'test_databasetable'
def get_session(database_connection_string):
try:
Base = declarative_base()
engine = create_engine(database_connection_string)
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
connection = session.connection()
return session
except OperationalError:
return None
except ArgumentError:
return None
所以我为这个函数做了一个单元测试用例:
import mock
import unittest
from mock import sentinel
import get_session
class TestUtilMock(unittest.TestCase):
@mock.patch('app.Utilities.util.create_engine') # mention the whole path
@mock.patch('app.Utilities.util.sessionmaker')
@mock.patch('app.Utilities.util.declarative_base')
def test_get_session1(self, mock_delarative_base, mock_sessionmaker,
mock_create_engine):
mock_create_engine.return_value = sentinel.engine
get_session('any_path')
mock_delarative_base.called
mock_create_engine.assert_called_once_with('any_path')
mock_sessionmaker.assert_called_once_with(bind=sentinel.engine)
正如您在我的单元测试中看到的,我无法从session = DBSession() 行开始测试get_session() 中的代码。到目前为止,在谷歌搜索之后,我无法确定返回的模拟值是否也可以用于模拟函数调用 - 例如,我模拟 session 对象并验证我是否调用了 session.connection()
上面写单元测试用例的方法对吗?有没有更好的方法来做到这一点?
【问题讨论】:
标签: python unit-testing sqlalchemy mocking python-mock