【发布时间】:2020-08-31 18:45:17
【问题描述】:
我正在尝试为我的代码编写单元测试。但是我得到了没有调用模拟的断言错误。如何解决这个问题?
在我的code.py
from SDK import ManagementClient
def init_client():
MGMT_CLIENT = ManagementClient(credentials)
# ManagementClient is an imported module, MGMT_CLIENT is a global variable
def add_db(ids):
for id in ids:
db_connection = MGMT_CLIENT.databases
db_connection.database_operations.create()
在test.py
@pytest.fixture()
def database_operations_mock():
with mock.patch("code.MGMT_CLIENT.databases") as database_operations_mock:
yield database_operations_mock
@pytest.fixutre
def client_mock():
with mock.patch("code.ManagementClient") as client_mock:
yield client_mock
def test_add():
# prepare test data
ids_mock = ['1','2']
# update global variable for code.py to run add_db()
code.MGMT_CLIENT = client_mock()
# run actual functions
code.add_db(ids_mock)
assert database_operations_mock.call_count == len(ids_mock)
我收到了这个错误:
> assert database_operations_mock.call_count == len(ids_mock)
E assert 0 == 2
E +0
E -2
【问题讨论】:
标签: unit-testing pytest python-unittest