Mocking cursor.fetchone()在python中不返回任何值

Mocking cursor.fetchone()在python中不返回任何值,python,mocking,pyodbc,python-unittest,magicmock,Python,Mocking,Pyodbc,Python Unittest,Magicmock,我为我的项目编写了一个函数,它使用Pyodbc从MSSQL服务器获取数据。该函数工作正常。当我使用unittest和mock library编写unittest用例并模拟cursor.fetchone并返回预定义值时,但在运行测试用例时,它不返回值,而是返回None。 这是我的密码 Store.py import os from datetime import date from datetime import timedelta import logging logging.basicConf

我为我的项目编写了一个函数,它使用Pyodbc从MSSQL服务器获取数据。该函数工作正常。当我使用unittest和mock library编写unittest用例并模拟cursor.fetchone并返回预定义值时,但在运行测试用例时,它不返回值,而是返回None。 这是我的密码

Store.py

import os
from datetime import date
from datetime import timedelta
import logging
logging.basicConfig(filename="source_monitor.log", format='%(name)s - %(levelname)s - %(asctime)s %(message)s', filemode='a') 
logger=logging.getLogger() 


class KMExtracter:
    def __init__(self,metric_collected_date):
        self.metric_collected_date = metric_collected_date
    # argument conn is a db connection which will passed in seperate program
    def get_metrics_by_technology(self, conn, technology):
        try:
        
            cursor = conn.cursor()
            cursor.execute(
                "SELECT COUNT(*) FROM URL_STORE WHERE Technology='{0}' firstExtractionDate  BETWEEN '{1} 00:00:00' AND '{1} 23:59:59'".format(self.technology[technology],
                        self.metric_collected_date
                ))
                
            
            count = cursor.fetchone()
            return count[0]
        except Exception as e:
            logging.error("{0} at get_metrics_by_technology()".format(e))
test_store.py

class TestKM(unittest.TestCase):
    def test_get_metrics_by_technology(self):
        mock_data_interface = Mock()
        mock_data_interface.cursor.return_value.execute.return_value.fetchone.return_value(23987,)
        
        km = KMExtracter('2021-04-03')
        print(km.get_metrics_by_technology(mock_data_interface, 'SOME'))
        self.assertEqual(23987,km.get_metrics_by_technology(mock_data_interface, 'SOME'))
我得到的错误是:
断言者错误:23987!=无

最终返回值必须是变量属性,而不是方法调用。例如:
fetchone.return\u value=23987
thankyou@MauroBaraldi。现在它工作了,我已经将mock_data_interface.cursor.return_value.execute.return_value.fetchone.return_value(23987,)行更改为mock_data_interface.cursor.return_value.fetchone.return_value(23987,)
class TestKM(unittest.TestCase):
    def test_get_metrics_by_technology(self):
        mock_data_interface = Mock()
       
        # execute.return_value was removed from the below line.
        mock_data_interface.cursor.return_value.fetchone.return_value(23987,)
        
        km = KMExtracter('2021-04-03')
        print(km.get_metrics_by_technology(mock_data_interface, 'SOME'))
        self.assertEqual(23987,km.get_metrics_by_technology(mock_data_interface, 'SOME'))