在Python的unittest中,为同一个SQLClient对象在每个函数中获取不同的数值。

huangapple go评论75阅读模式
英文:

fetch different values in each function for the same sqlclient object in python unittest

问题

我正在尝试在项目上执行一个集成测试。我们有一个名为mysqlclient的对象,它被模拟,并且能够在第一次fetchall调用时分配虚拟值。有谁可以帮助我了解如何在不同函数中每次调用fetchall时获取不同的值。代码结构如下:

测试函数

@mock.patch("src.calculate.get_mysql_connection", return_value=mock.Mock())
@mock.patch("src.calculate.get_mongo_connection", return_value=mock.Mock())
def test_initiate_calc(self, mongo_patcher, mysql_patcher):
    mysql_patcher.return_value.\
            execute_query.return_value.\
            fetchall.return_value = [1,2,3,4]
    
    test_main()

主函数

main():
    mongo_client = get_mongo_connection() 
    mysql_client = get_mysql_connection()  # 模拟的mysql对象
    func1(mysql_client)
    func2(mysql_client)
    func3(mysql_client)
    func4(mysql_client)

func1(mysql_client):
    query = "select something"
    params = (some_params, )
    cursor = mysql_client.execute_query(query, params)
    result1 = cursor.fetchall()  # 这按预期工作并返回[1,2,3,4]

func2(mysql_client):
    query = "select something"
    params = (some_params, )
    cursor = mysql_client.execute_query(query, params)
    result2 = cursor.fetchall()  # 如何在这里为相同的情况模拟不同的值

func3(mysql_client):
    query = "select something"
    params = (some_params, )
    cursor = mysql_client.execute_query(query, params)
    result3 = cursor.fetchall()  # 如何在这里为相同的情况模拟不同的值

有谁可以帮助我了解如何在不同函数中每次调用fetchall时获取不同的值?

英文:

I am trying to perform an integration test on the project. We have a mysqlclient object which is mocked and able to assign dummy values on the first fetchall call. Can anyone help me in how to fetch different values each time the fetchall is called in different functions. the code structure is as below.

test function


@mock.patch("src.calculate.get_mysql_connection", return_value = mock.Mock())
@mock.patch("src.calculate.get_mongo_connection", return_value = mock.Mock())
def test_initiate_calc(self, mongo_patcher, mysql_patcher):
    mysql_patcher.return_value.\
            execute_query.return_value.\
            fetchall.return_value = [1,2,3,4]
    
    test_main()

main function


main():
    mongo_client = get_mongo_connection() 
    mysql_client = get_mysql_connection() - - - - - mocked mysql object
    func1(mysql_client)
    func2(mysql_client)
    func3(mysql_client)
    func4(mysql_client)

func1(mysql_client):
    query = ("select something")

    params = (some_params, )

    cursor = mysql_client.execute_query(query, params)

    result1 = cursor.fetchall() - - - - - This works as intended and returns [1,2,3,4]

func2(mysql_client):
    query = ("select something")

    params = (some_params, )

    cursor = mysql_client.execute_query(query, params)

    result2 = cursor.fetchall() - - - - - how to mock different value here for the same 

func3(mysql_client):
    query = ("select something")

    params = (some_params, )

    cursor = mysql_client.execute_query(query, params)

    result3 = cursor.fetchall() - - - - - how to mock different value here for the same


Can anyone help me in how to fetch different values each time the fetchall is called in different functions

答案1

得分: 1

你可以创建一个完整的模拟函数。在那里,你可以轻松地设置在不同条件下想要获取的内容。

from unittest.mock import patch

def mocked_connection():
    class MockedCursor:
        def fetchall(self):
            # 这将使您能够更改每次调用的返回值
            global x
            x += 1
            if x == 1:
                return [1, 2, 3, 4]
            elif x == 2:
                return [2, 3, 4, 5]

    class MockedConnection:
        def execute_query(self, query, params):
            return MockedCursor()

    return MockedConnection()

# 然后使用这个模拟连接
@patch("src.calculate.get_mysql_connection", mocked_connection)
@patch("src.calculate.get_mongo_connection", mocked_connection)
def test_initiate_calc(self, mongo_patcher, mysql_patcher):
    x = 0
    test_main()
英文:

You can create whole mocking function. There you can easily set what you want to get on various conditions.

from unittest.mock import patch
def mocked_connection():
  class MockedCursor:
    def fetchall(self):
      # This will enable you to change return each next call
      global x
      x += 1
      if x == 1:
        return [1,2,3,4]
      elif x == 2:
        return [2,3,4,5]

  class MockedConnection:
    def execute_query(self, query, params):
      return MockedCursor()
  return MockedConnection()


# Then use this mocked connection
@patch("src.calculate.get_mysql_connection", mocked_connection)
@patch("src.calculate.get_mongo_connection", mocked_connection)
def test_initiate_calc(self, mongo_patcher, mysql_patcher):
    x = 0
    test_main()

huangapple
  • 本文由 发表于 2023年2月27日 14:03:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定