Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用python模拟dbconnection fetchall_Python 3.x_Python Unittest_Python Mock_Python Unittest.mock - Fatal编程技术网

Python 3.x 使用python模拟dbconnection fetchall

Python 3.x 使用python模拟dbconnection fetchall,python-3.x,python-unittest,python-mock,python-unittest.mock,Python 3.x,Python Unittest,Python Mock,Python Unittest.mock,我试图模拟dbconnectioncursor对象中的fetchall()。我正在使用预期的返回值尝试以下代码。但是,它没有返回值。我现在有了答案,并编辑了单元测试,将答案也包括在内 db.py 测试用例: def test_get_client_object(self): dbconnection = Mock(name="dbconnection") mycursor = Mock(name="mycursor") mycursor.f

我试图模拟
dbconnection
cursor对象中的
fetchall()
。我正在使用预期的返回值尝试以下代码。但是,它没有返回值。我现在有了答案,并编辑了单元测试,将答案也包括在内 db.py

测试用例:

    def test_get_client_object(self):
        dbconnection = Mock(name="dbconnection")
        mycursor = Mock(name="mycursor")
        mycursor.fetchall.return_value = "testing_return_value"
        dbconnection.cursor.return_value = mycursor  # I was doing dbconnection.cursor = mycursor .  ... which caused the error

        self.assertEqual("testing_return_value", utils.query_db(dbconnection, 12345))
我得到了以下断言错误。它返回一个模拟对象,而不是预期的返回值

<Mock name='mycursor().fetchall()' id='4443879760'> != testing_return_value

Expected :testing_return_value
Actual   :<Mock name='mycursor().fetchall()' id='4443879760'>
<Click to see difference>
!=测试返回值
预期:测试返回值
实际:

以下是单元测试解决方案:

utils.py

def query\u数据库(数据库连接,查询):
cur=db_connection.cursor()
尝试:
打印(f“要执行的查询:{Query}”)
当前执行(查询)
结果=cur.fetchall()
打印(f“查询结果:{Results}”)
除例外情况外:
打印(“执行“查询数据库”功能时出现异常”)
引发异常(
f“执行查询时出错:{query}。有关详细信息,请检查日志”)
返回结果
test_utils.py

导入单元测试
从unittest.mock导入mock
导入UTIL
类TestUtils(unittest.TestCase):
def测试获取客户端对象(自身):
dbconnection=Mock(name=“dbconnection”)
mycursor=Mock(name=“mycursor”)
mycursor.fetchall.return\u value=“测试返回值”
dbconnection.cursor.return\u value=mycursor
self.assertEqual(“测试返回值”,
utils.query_数据库(数据库连接,12345))
dbconnection.cursor.assert_调用了_once()
mycursor.execute.assert_调用一次__(12345)
mycursor.fetchall.assert_调用了_once()
def测试查询数据库异常(自身):
dbconnection=Mock(name=“dbconnection”)
mycursor=Mock(name=“mycursor”)
mycursor.fetchall.side_effect=异常
dbconnection.cursor.return\u value=mycursor
使用self.assertRaises(异常)作为cm:
utils.query\u数据库(数据库连接,12345)
自我评估资格(str)(
cm.exception),“执行查询时出错:12345。请检查日志以了解详细信息”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
unittest.main(详细度=2)
100覆盖率报告的单元测试结果:

test\u get\u client\u对象(\uuu main\uuu.TestUtils)。。。待执行查询:12345
查询结果:测试返回值
好啊
测试\u查询\u数据库\u异常(\uuuu main\uuu.TestUtils)。。。待执行查询:12345
执行“查询数据库”功能时出现异常
好啊
----------------------------------------------------------------------
在0.002s中运行了2次测试
好啊
Name Stmts未命中封面丢失
------------------------------------------------------------------------
src/stackoverflow/59226762/test_utils.py 22 0 100%
src/stackoverflow/59226762/utils.py 11 0 100%
------------------------------------------------------------------------
总数33 0 100%

源代码:

接受的答案对我不起作用,我仍然收到对fetchall()的链接调用。我模仿了包括fetchall在内的所有三个级别,然后成功了,也许它对某人有帮助:

测试:

功能:

cursor = connection.cursor()
return cursor.execute(sqlstatement).fetchall()

试着阅读这篇文章来更好地理解mocks。这是一个小错误,在做了这个修改之后它就起作用了。如果你能提供更正,那就太好了。谢谢修改了起源问题
expected = ["{\"parameter\":\"1337\"}"]
myconnection = mocker.Mock(name="dbconnection")
mycursor = mocker.Mock(name="mycursor")
myfetchall = mocker.Mock(name="myfetchall")  # I needed to mock this as well
myfetchall.fetchall.return_value = expected
mycursor.execute.return_value = myfetchall
myconnection.cursor.return_value = mycursor
cursor = connection.cursor()
return cursor.execute(sqlstatement).fetchall()