Python单元测试如何在我的函数上使用db_session.query时进行测试?

时间:2017-12-12 07:29:02

标签: python-2.7 python-unittest

我是python的新手,我正在为我的python项目尝试单元测试功能

def getFundPosition(self, enterprise_id=None, since=None, till=datetime.datetime.today()):
    """

    :param enterprise_id:
    :param since:
    :param till:
    :return:
    """
bank = self.db_session.query(AccountGroup).filter(
        AccountGroup.enterprise_id == enterprise_id, AccountGroup.name == AccountGroup.BANK_GROUP_NAME).first()
    cash = self.db_session.query(AccountGroup).filter(
        AccountGroup.enterprise_id == enterprise_id, AccountGroup.name == AccountGroup.CASH_GROUP_NAME).first()
    fund = float(
        bank.getBalance(since, till, approved_only=True) + cash.getBalance(since, till, approved_only=True))

另一方面测试代码如下

class AccountsTestCase(TestCase):
    def setUp(self):
        #every test needs a client
    def test_dashboard(self):
        response = AccountsDAO().getFundPosition(121,'2016-01-01','2017-12-01')

它会抛出错误,如下所示

AttributeError: 'NoneType' object has no attribute 'getBalance'

我想知道的是,我正在做的是测试的正确方法,或者它不会在单元测试案例场景下执行self.db_session.query

请帮我解决这个问题

0 个答案:

没有答案