Django入门指南-第8章:第一个测试用例

    测试将是一个反复出现的主题,我们将在整个教程系列中一起探讨不同的概念和策略。

    我们来开始写第一个测试。现在,我们将在boards应用程序内的tests.py文件中操作

    boards/tests.py

    这是一个非常简单但非常有用的测试用例,我们测试的是请求该URL后返回的响应状态码。状态码200意味着成功。

    如果出现未捕获的异常,语法错误或其他任何情况,Django会返回状态代码500,这意味着是内部服务器错误。现在,想象我们的应用程序有100个视图函数。如果我们为所有视图编写这个简单的测试,只需一个命令,我们就能够测试所有视图是否返回成功代码,因此用户在任何地方都看不到任何错误消息。如果没有自动化测试,我们需要逐一检查每个页面是否有错误。

    执行Django的测试套件:

    现在我们可以测试Django是否在请求的URL的时候返回了正确的视图函数。这也是一个有用的测试,因为随着开发的进展,您会发现urls.py模块可能变得非常庞大而复杂。URL conf 全部是关于解析正则表达式的。有些情况下有一个非常宽容的URL(译注:本来不应该匹配的,却因为正则表达式写的过于宽泛而错误的匹配了),所以Django最终可能返回错误的视图函数。

    boards/tests.py

    1. from django.core.urlresolvers import reverse
    2. from django.urls import resolve
    3. from django.test import TestCase
    4. from .views import home
    5. class HomeTests(TestCase):
    6. def test_home_view_status_code(self):
    7. url = reverse('home')
    8. response = self.client.get(url)
    9. self.assertEquals(response.status_code, 200)
    10. def test_home_url_resolves_home_view(self):
    11. view = resolve('/')
    12. self.assertEquals(view.func, home)

    在第二个测试中,我们使用了resolve函数。Django使用它来将浏览器发起请求的URL与urls.py模块中列出的URL进行匹配。该测试用于确定URL / 返回 home 视图。

    再次测试:

    1. ```sh
    2. Creating test database for alias 'default'...
    3. ..
    4. ----------------------------------------------------------------------
    5. Ran 2 tests in 0.027s
    6. OK
    7. Destroying test database for alias 'default'...

    要查看有关测试执行时更详细的信息,可将verbosity的级别设置得更高一点:

    1. Creating test database for alias 'default' ('file:memorydb_default?mode=memory&cache=shared')...
    2. Operations to perform:
    3. Synchronize unmigrated apps: messages, staticfiles
    4. Apply all migrations: admin, auth, boards, contenttypes, sessions
    5. Synchronizing apps without migrations:
    6. Creating tables...
    7. Running deferred SQL...
    8. Running migrations:
    9. Applying contenttypes.0001_initial... OK
    10. Applying auth.0001_initial... OK
    11. Applying admin.0001_initial... OK
    12. Applying admin.0002_logentry_remove_auto_add... OK
    13. Applying auth.0002_alter_permission_name_max_length... OK
    14. Applying auth.0003_alter_user_email_max_length... OK
    15. Applying auth.0004_alter_user_username_opts... OK
    16. Applying auth.0005_alter_user_last_login_null... OK
    17. Applying auth.0006_require_contenttypes_0002... OK
    18. Applying auth.0007_alter_validators_add_error_messages... OK
    19. Applying auth.0008_alter_user_username_max_length... OK
    20. Applying boards.0001_initial... OK
    21. Applying sessions.0001_initial... OK
    22. System check identified no issues (0 silenced).
    23. test_home_url_resolves_home_view (boards.tests.HomeTests) ... ok
    24. test_home_view_status_code (boards.tests.HomeTests) ... ok
    25. ----------------------------------------------------------------------
    26. Ran 2 tests in 0.017s
    27. OK
    28. Destroying test database for alias 'default' ('file:memorydb_default?mode=memory&cache=shared')...