英文:
python unittest : mock cursor.fetchall() to return a dummy value inside a function
问题
#file utils.py
def update_configuration(configuration, mysql_client):
query = "SELECT * from some database"
cursor = mysql_client.execute_query(query)
function_mapping = cursor.fetchall()
cursor.close()
configuration["mapping"] = {}
for (display_name, formula_name) in function_mapping:
configuration["formula_mapping"][formula_name] = display_name
#main.py
def main_func(configuration):
mysql_client = get_mysql_connection()
ut.update_configuration(configuration, mysql_client) #ut means utils.py
#test.py
@mock.patch("src.utils.cursor.fetchall", return_value=[1, 2, 3, 4])
@mock.patch("src.main.get_sql_connection", return_value=mock.Mock())
def test_initiate_calc(self, dummy1):
# perform integration testing on "main_func"
The project structure is as shown below
--project
--src
--tests
--test_main.py
--main.py
--utils.py
When I try to mock the cursor.fetchall()
to return some value, I get an error saying "ModuleNotFoundError: No module named 'src.utils.cursor'; 'src.utils' is not a package."
Need help in finding a way to get the function_mapping = cursor.fetchall()
value to return some value.
英文:
#file utils.py
def update_configuration(configuration, mysql_client):
query = "SELECT * from some database"
cursor = mysql_client.execute_query(query)
function_mapping = cursor.fetchall()
cursor.close()
configuration["mapping"] = {}
for (display_name, formula_name) in function_mapping:
configuration["formula_mapping"][formula_name] = display_name
#main.py
def main_func(configuration):
mysql_client = get_mysql_connection()
ut.update_configuration(configuration, mysql_client) #ut means utils.py
#test.py
@mock.patch("src.utils.cursor.fetchall",return_value = [1,2,3,4])
@mock.patch("src.main.get_sql_connection", return_value = mock.Mock())
def test_initiate_calc(self, dummy1):
# perform integration testing on "main_func"
The project structure is as shown below
--project
--src
--tests
--test_main.py
--main.py
--utils.py
When I try to mock the cursor.fetchall()
to return some value I get an error saying "ModuleNotFoundError: No module named 'src.utils.cursor'; 'src.utils' is not a package"
Need help in finding a way to get the function_mapping = cursor.fetchall() value some return value
答案1
得分: 1
你也可以尝试 cursor.fetchall.side_effect = "some-value"
类似这样的:
@patch('connector')
def test_create_table(self, connector):
connection = Mock()
cursor = Mock()
connector.connect.return_value = connection
connection.cursor.return_value = cursor
cursor.fetchall.side_effect = "List of tuple"
connector.connect.assert_called_with("creds")
cursor.execute.assert_called_with("statements - you - are - trying - to - execute")
对于多个语句,可以使用 assert_has_calls
。
英文:
You can also try cursor.fetchall.side_effect = "some-value"
Something like this:
@patch('connector')
def test_create_table(self, connector):
connection = Mock()
cursor = Mock()
connector.connect.return_value = connection
connection.cursor.return_value = cursor
cursor.fetchall.side_effect = "List of tuple"
connector.connect.assert_called_with("creds")
cursor.execute.assert_called_with("statements - you - are - trying - to - execute")
For multiple statements use assert_has_calls
.
答案2
得分: 0
无法修补本地变量cursor
,也不需要这样做。正确配置get_mysql_connection
模拟函数。
@mock.patch("src.main.get_sql_connection")
def test_initiate_calc(self, mock_conn):
mock_conn.return_value.execute_query.return_value.fetchall.return_value = [1,2,3,4]
# 在"main_func"上执行集成测试
英文:
You can't patch the local variable cursor
, nor do you need to. Configure the get_mysql_connection
mock properly instead.
@mock.patch("src.main.get_sql_connection")
def test_initiate_calc(self, mock_conn):
mock_conn.return_value.execute_query.return_value.fetchall.return_value = [1,2,3,4]
# perform integration testing on "main_func"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论