【问题标题】:How to write test case for sending email如何编写发送电子邮件的测试用例
【发布时间】:2019-10-16 07:34:27
【问题描述】:

这是创建用户和员工模型的简单视图。创建用户和员工后,它将html电子邮件发送到用户的电子邮件以填写详细信息,视图工作正常。 现在我想为此视图编写测试用例并在下面尝试这样,但我陷入了如何编写测试以检查在保存人员模型后是否将电子邮件发送给用户的问题。 模型.py

class Staff(models.Model):
    user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE, related_name='staff')
    name = models.CharField(max_length=255, blank=True, null=True)
    organization = models.ForeignKey(Organization, on_delete=models.SET_NULL,related_name='staff')
    position = models.ForeignKey(Position, on_delete=models.SET_NULL,related_name='staff')
    .......

views.py

def register_staff(request):
    form = RegisterStaffForm()
    if request.method == 'POST':
        form = RegisterStaffForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            organization = form.cleaned_data['organization']
            position = form.cleaned_data['position']
            ......
            user = form.save(commit=False)
            user.is_staff = True
            user.is_active = True
            user.save()

            # creating staff model with user data

            Staff.objects.create(user=user, name=name, organization=organization, position=position,....)

            # sending html_email to the user
            config = EmailConfiguration.objects.order_by('-date').first()
            backend = EmailBackend(host=config.email_host, port=config.email_port, username=config.email_host_user,
                                   password=config.email_host_password, use_tls=config.email_use_tls)

            subject, from_email, to = "Staff Details", config.email_host_user, user.email
            text_content = "Staff Details "
            site = get_current_site(request)
            html_content = render_to_string('send_email.html',
                                            {'user': user, 'site_domain': site,})
            msg = EmailMultiAlternatives(subject, text_content, from_email, [to],connection=backend)
            msg.attach_alternative(html_content, "text/html")
            msg.send()

tests.py

class StaffTestCase(TestCase):

    def setUp(self):
        self.position = Position.objects.create(title='Developer')
        self.org = Organization.objects.create(name='name')
        self.user = get_user_model().objects.create_user(username='username01',password='Admin@321',email='abc@xyz.com',is_staff=True)
        self.staff = Staff.objects.create(user=self.user,position=self.position,organization=self.org,name='Julia')

        self.client = Client()

    def test_view_staffs(self):
        self.client.login(username='username01', password='Admin@321')
        response = self.client.get(reverse('app:view_staff_users'))
        self.assertEqual(response.status_code, 200)

    def add_staff(self):
        self.client.login(username='username01', password='Admin@321')
        url = reverse('app:register_staff')
        response = self.client.post(url, {'user': self.user,'organization':'name1','position':'Designer','name':'Mark'})
        self.assertEqual(response.status_code, 302)

    def check_email_will_sent_or_not(self):
     ??

【问题讨论】:

标签: django unit-testing django-views


【解决方案1】:

Django 提供tools to test the sending of emails,但据我了解,这些仅适用于您的设置文件中配置的默认电子邮件后端。

这让您有四个选择:

  1. 确定是否可以修改视图以在测试中使用 locmem backend
  2. register_staff 视图中模拟EmailBackend 并检查其send_messages 函数是否被调用。
  3. 启动一个虚拟 SMTP 服务器并检查它是否接收到消息
  4. 使用有效凭据将电子邮件实际发送到您控制的地址并检查是否收到电子邮件

选项 3 和 4 让您最自信地相信您的电子邮件发送确实有效,但它们可能会变得缓慢而脆弱,并实际测试 Django 本身的电子邮件发送代码。我会选择选项 2。

【讨论】:

  • 如何在我的 register_staff 中模拟 EmailBacken 任何示例?
猜你喜欢
  • 1970-01-01
  • 2012-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多