我的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
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
答案 0 :(得分:2)
使用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)
答案 1 :(得分:0)
由于我已经导入了需要修补的方法的模块。我不需要使用完整路径,包括软件包名称。
已更改
patch('projectxapp.billing.Billing.create_invoice_pdf')
对此
patch('billing.Billing.create_invoice_pdf')
来自Django文档:
target应该是“ package.module.ClassName”形式的字符串。导入目标并将指定的对象替换为新对象,因此目标必须可从您从其调用patch()的环境中导入。在执行装饰功能时(而不是在装饰时)导入目标。