python - Django Nose 这个测试怎么写?

标签 python django unit-testing testing nose

我完全不熟悉 Django 中的测试。我已经开始安装 noseselenium,现在我想测试以下代码 (下面) 它发送一条 SMS 消息。

这是实际的代码:

views.py

@login_required
def process_all(request):
    """
    I process the sending for a single or bulk message(s) to a group or single contact.
    :param request:
    """
    #If we had a POST then get the request post values.
    if request.method == 'POST':
        batches = Batch.objects.for_user_pending(request.user)
        for batch in batches:
            ProcessRequests.delay(batch)
            batch.complete_update()

    return HttpResponseRedirect('/reports/messages/')

那么我从哪里开始呢?这是我到目前为止所做的...

1) 创建一个名为tests 的文件夹并添加init.py。

2) 创建了一个名为 test_views.py 的新 python 文件(我假设这是正确的)。

现在,我该如何着手编写这个测试?

谁能给我举个例子,说明我是如何为上面的 View 编写测试的?

谢谢你:)

最佳答案

首先,你不需要selenium用于测试 View 。 Selenium 是一种用于高级浏览器内测试的工具 - 当您编写模拟真实用户的 UI 测试时,它非常有用。

Nose 是一种工具makes testing easier通过提供自动测试发现等功能,提供许多辅助功能等。将 nose 与 django 项目集成的最佳方法是使用 django_nose包裹。您所要做的就是:

  1. 添加django_noseINSTALLED_APPS
  2. 定义TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

然后,每次运行 python manage.py test <project_name> nose 将用于运行您的测试。


因此,谈到测试这个特定 View ,您应该测试:

  • login_required装饰器工作——换句话说,未经身份验证的用户将被重定向到登录页面
  • 如果request.method不是 POST,没有发送消息 + 重定向到 /reports/messages
  • 使用 POST 方法发送短信 + 重定向到 /reports/messages

测试前两个语句非常简单,但为了测试最后一个语句,您需要提供有关 Batch 的更多详细信息, ProcessRequests它是如何工作的。我的意思是,您可能不想在测试期间发送真正的 SMS 消息 - 这就是 mocking 的地方会有所帮助。基本上,您需要模拟(即时替换为您自己的实现)Batch , ProcessRequests对象。这是您应该在 test_views.py 中拥有的示例:

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test.client import Client
from django.test import TestCase


class ProcessAllTestCase(TestCase):
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')

    def test_login_required(self):
        response = self.client.get(reverse('process_all'))
        self.assertRedirects(response, '/login')

    def test_get_method(self):
        self.client.login(username='john', password='johnpassword')
        response = self.client.get(reverse('process_all'))
        self.assertRedirects(response, '/reports/messages')

        # assert no messages were sent

    def test_post_method(self):
        self.client.login(username='john', password='johnpassword')

        # add pending messages, mock sms sending?

        response = self.client.post(reverse('process_all'))
        self.assertRedirects(response, '/reports/messages')

        # assert that sms messages were sent

另见:

希望对您有所帮助。

关于python - Django Nose 这个测试怎么写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17232747/

相关文章:

python - 排序时统计元素比较次数

python 扭曲 : Performing multiple writes at once

django - django 形式的多个复选框

python - 使用 Django 查询集访问不同的外键关系

unit-testing - 单元测试预期和非预期死锁行为的明智策略

python - 如何在 Django 中使用 AJAX 正确从 html 调用函数/url?

python - ValueError : Units 'M' and 'Y' are no longer supported, 因为它们不代表明确的 timedelta 值持续时间

python - Django 脆皮表格 : Set rows for Text Area

java - 将 JUnit 测试列表中的所有错误/失败记录到文本文件中

javascript - 模拟 Javascript 对象(特别是 YUI)