尝试在方法上使用 mock.patch 时出现 ModuleNotFoundError

use*_*284 5 python django pytest pytest-django python-unittest.mock

我的 pytest 单元测试不断返回错误 ModuleNotFoundError: No module name billing

奇怪的send_invoices是,当我删除补丁语句时,计费模块中的方法能够被调用。如果是这种情况,为什么mock.patch 无法找到计费模块并修补方法?

billing.py

import pdfkit
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from projectxapp.models import User

Class Billing:

  #creates a pdf of invoice. Takes an invoice dictionary
  def create_invoice_pdf(self, invoice, user_id):
    #get the html of the invoice
    file_path ='/{}-{}.pdf'.format(user_id, invoice['billing_period'])
    invoice_html = render_to_string('templates/invoice.html', invoice)
    pdf = pdfkit.from_file(invoice_html, file_path)
    return file_path, pdf

  #sends invoice to customer
  def send_invoices(self, invoices):
    for user_id, invoice in invoices.items():
            #get customers email
            email = User.objects.get(id=user_id).email
            billing_period = invoice['billing_period']
            invoice_pdf = self.create_invoice_pdf(invoice, user_id)
            email = EmailMessage(
                'Statement of credit: {}-{}'.format(user_id, billing_period),
                'Attached is your statement of credit.\
                This may also be viewed on your dashboard when you login.',
                'no-reply@trevoe.com',
                [email],
            ).attach(invoice_pdf)

            email.send(fail_silently=False)

        return True
Run Code Online (Sandbox Code Playgroud)

test.py

from mock import patch
from projectxapp import billing

@pytest.mark.django_db
def test_send_invoice():
  invoices = {
    1: {
        'transaction_processing_fee': 2.00,
        'service_fee': 10.00,
        'billing_period': '2020-01-02'
    },
    2: {
        'transaction_processing_fee': 4.00,
        'service_fee': 20.00,
        'billing_period': '2020-01-02'
    }
 }

  with patch('services.billing.Billing().create_invoice_pdf') as p1:
    p1.return_value = '/invoice.pdf'
    test = billing.Billing().send_invoices(invoices)

  assert test == True
Run Code Online (Sandbox Code Playgroud)

blh*_*ing 10

当您使用 时,您应该指定模块的完整路径,包括包名称patch。另外,请勿在路径中使用括号。修改对象return_value的属性Mock来自定义调用对象的返回值:

with patch('projectxapp.billing.Billing.create_invoice_pdf') as p1:
    p1.return_value = '/invoice.pdf'
    test = billing.Billing().send_invoices(invoices)
Run Code Online (Sandbox Code Playgroud)


use*_*284 5

解决方案

由于我已经导入了我需要修补的方法的模块。我不需要使用包括包名称的完整路径。

改变了

patch('projectxapp.billing.Billing.create_invoice_pdf')

对此

patch('billing.Billing.create_invoice_pdf')

来自单元测试文档

target 应该是“package.module.ClassName”形式的字符串。目标被导入,指定的对象被新对象替换,因此目标必须可以从您调用 patch() 的环境中导入。目标是在执行装饰函数时导入的,而不是在装饰时导入。