Python unittest 模拟方法在至少被调用一次时被调用 0 次
Python unittest mocked method is called 0 times when it's being called at least once
我试图断言模拟的方法被调用一次并期望它通过。但不知何故,mock_create_pool
、mock_insert
和 mock_close
的调用计数为 0 而不是 1 或更多。我怎样才能至少测试一次 运行 函数?
目录结构
- 来源
- 逻辑
- main.py
- 测试
- unit_test_main.py
- 实用程序
- db_connections.py
db_connections.py
class DbConnections:
. . .
def create_pool(self, pool_name="mypool", pool_size=5):
. . .
def close(self):
. . .
class dailyspecial(DbConnections):
. . .
def dbinsert(self, create_date, data_date, country, confirm_case, confrim_death, test=""):
. . .
logic/main.py
import util.db_connections as daily
def get_wm():
. . .
db = daily.dailyspecial()
db.create_pool(pool_name="wm_pool", pool_size=5)
try:
. . .
for record in res_table:
. . .
db.dbinsert(. . .)
except Exception as e:
print(e)
db.close()
tests/unit_test_main.py
import logic.main as main
class TestWorldMeter(unittest.TestCase):
@mock.patch("util.db_connections.dailyspecial.close")
@mock.patch("util.db_connections.dailyspecial.dbinsert")
@mock.patch("util.db_connections.dailyspecial.create_pool")
@mock.patch("util.db_connections.dailyspecial")
def test_get_wm(self, mock_dailyspecial, mock_create_pool, mock_insert, mock_close):
main.get_wm()
self.assertEqual(mock_dailyspecial.call_count, 1) # pass
self.assertEqual(mock_create_pool.call_count, 1) # fail, expect pass
self.assertTrue(mock_insert.call_count > 0) # fail, expect pass
self.assertEqual(mock_close.call_count, 1) # fail, expect pass
或者,我尝试仅通过模拟 util.db_connections.dailyspecial
class 来测试这些方法。它仍然没有通过断言。知道我的模拟过程出了什么问题吗?
备选方案tests/unit_test_main.py
class TestWorldMeter(unittest.TestCase):
@mock.patch("util.db_connections.dailyspecial")
def test_get_wm(self, mock_dailyspecial):
# mock_dailyspecial().return_value = mock.MagicMock()
mock_dailyspecial.return_value = mock.MagicMock()
main.get_wm()
self.assertEqual(mock_dailyspecial.call_count, 1) # pass
self.assertEqual(mock_dailyspecial.create_pool.call_count, 1) # fail, expect pass
self.assertTrue(mock_dailyspecial.dbinsert.call_count > 0) # fail, expect pass
self.assertEqual(mock_dailyspecial.close.call_count, 1) # fail, expect pass
尝试计算调用模拟方法的次数时缺少 ()
。它应该是这样的:
self.assertEqual(mock_dailyspecial.call_count, 1)
self.assertEqual(mock_dailyspecial().create_pool.call_count, 1)
self.assertTrue(mock_dailyspecial().dbinsert.call_count > 0)
self.assertEqual(mock_dailyspecial().close.call_count, 1)
我试图断言模拟的方法被调用一次并期望它通过。但不知何故,mock_create_pool
、mock_insert
和 mock_close
的调用计数为 0 而不是 1 或更多。我怎样才能至少测试一次 运行 函数?
目录结构
- 来源
- 逻辑
- main.py
- 测试
- unit_test_main.py
- 实用程序
- db_connections.py
- 逻辑
db_connections.py
class DbConnections:
. . .
def create_pool(self, pool_name="mypool", pool_size=5):
. . .
def close(self):
. . .
class dailyspecial(DbConnections):
. . .
def dbinsert(self, create_date, data_date, country, confirm_case, confrim_death, test=""):
. . .
logic/main.py
import util.db_connections as daily
def get_wm():
. . .
db = daily.dailyspecial()
db.create_pool(pool_name="wm_pool", pool_size=5)
try:
. . .
for record in res_table:
. . .
db.dbinsert(. . .)
except Exception as e:
print(e)
db.close()
tests/unit_test_main.py
import logic.main as main
class TestWorldMeter(unittest.TestCase):
@mock.patch("util.db_connections.dailyspecial.close")
@mock.patch("util.db_connections.dailyspecial.dbinsert")
@mock.patch("util.db_connections.dailyspecial.create_pool")
@mock.patch("util.db_connections.dailyspecial")
def test_get_wm(self, mock_dailyspecial, mock_create_pool, mock_insert, mock_close):
main.get_wm()
self.assertEqual(mock_dailyspecial.call_count, 1) # pass
self.assertEqual(mock_create_pool.call_count, 1) # fail, expect pass
self.assertTrue(mock_insert.call_count > 0) # fail, expect pass
self.assertEqual(mock_close.call_count, 1) # fail, expect pass
或者,我尝试仅通过模拟 util.db_connections.dailyspecial
class 来测试这些方法。它仍然没有通过断言。知道我的模拟过程出了什么问题吗?
备选方案tests/unit_test_main.py
class TestWorldMeter(unittest.TestCase):
@mock.patch("util.db_connections.dailyspecial")
def test_get_wm(self, mock_dailyspecial):
# mock_dailyspecial().return_value = mock.MagicMock()
mock_dailyspecial.return_value = mock.MagicMock()
main.get_wm()
self.assertEqual(mock_dailyspecial.call_count, 1) # pass
self.assertEqual(mock_dailyspecial.create_pool.call_count, 1) # fail, expect pass
self.assertTrue(mock_dailyspecial.dbinsert.call_count > 0) # fail, expect pass
self.assertEqual(mock_dailyspecial.close.call_count, 1) # fail, expect pass
尝试计算调用模拟方法的次数时缺少 ()
。它应该是这样的:
self.assertEqual(mock_dailyspecial.call_count, 1)
self.assertEqual(mock_dailyspecial().create_pool.call_count, 1)
self.assertTrue(mock_dailyspecial().dbinsert.call_count > 0)
self.assertEqual(mock_dailyspecial().close.call_count, 1)