python - 模拟在 Python 单元测试中没有被击中

标签 python unit-testing mocking

我是 Python 的新手,但我已经用 C# 和 JavaScript 进行了大量的单元测试。我无法弄清楚 Python 中的模拟框架。这是我所拥有的(修剪过的):

invoice_business.py

import ims.repository.invoice_repository as invoiceRepository
import logging

logger = logging.getLogger(__name__)

def update_invoice_statuses(invoices):
    for invoice in invoices:
        dbInvoice = invoiceRepository.get(invoice.invoice_id)
        print("dbInvoice is %s" % dbInvoice) #prints <MagicMock etc.>

        if dbInvoice is None:
            logger.error("Unable to update status for invoice %d" % invoice.invoice_id)
            continue;

test_invoice_business.py

from unittest import TestCase, mock
import logging
import ims.business.invoice_business as business

class UpdateInvoiceTests(TestCase):
    @mock.patch("ims.business.invoice_business.invoiceRepository")
    @mock.patch("ims.business.invoice_business.logger")
    def test_invoiceDoesNotExist_logsErrorAndContinues(self, invoiceRepoMock, loggerMock):
        #Arrange
        invoice = Invoice(123)
        invoice.set_status(InvoiceStatus.Filed, None)
        invoiceRepoMock.get.return_value(33)

        #Act
        business.update_invoice_statuses([invoice])

        #Assert
        invoiceRepoMock.get.assert_called_once_with(123)
        loggerMock.error.assert_called_once_with("Unable to update status for invoice 123")

测试失败

AssertionError: Expected 'get' to be called once. Called 0 times.

不过,update_invoice_statuses 中的 print 语句被命中,因为我看到了

的输出
dbInvoice is <MagicMock name='invoiceRepository.get()' id='xxxx'>

知道我在这里做错了什么吗?

编辑:在@chepner 的帮助下,我遇到了另一个断言错误并意识到这是因为我应该使用 invoiceRepoMock.get.return_value = None 而不是 .return_value(无)

最佳答案

测试函数的模拟参数被交换。内部装饰器(用于记录器)首先应用,因此模拟记录器应该是您的方法的第一个参数。

@mock.patch("ims.business.invoice_business.invoiceRepository")
@mock.patch("ims.business.invoice_business.logger")
def test_invoiceDoesNotExist_logsErrorAndContinues(self, loggerMock, invoiceRepoMock):
    ...

关于python - 模拟在 Python 单元测试中没有被击中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37615354/

相关文章:

python - 从 `%timeit` ipython magic 获取平均运行时间

python - 写入 key :value pairs to a tab-delimited file with variable #s of values

Python 代理检查器,更改为线程版本

android - 如何对 Retrofit api 调用进行单元测试?

ruby - 在 RSpec 中捕获 STDOUT

python - 如何模拟对 __next__ 的调用

python - Gamma 回归仅截距

c# - 用 rhino mocks 对一个属性进行 stub 两次

ruby - Webmock 没有正确注册我的请求 stub

c# - 如何对抽象类进行单元测试