【Django入门】——必须要学的Django测试

测试

  • python manage.py test
  • python manage.py test --verbosity=2

查看测试的更详细信息
verbosity=0:无输出
versiboy=1:正常输出
versiboy=2:详细输出
在这里插入图片描述


【1】服务端是否响应成功

  • 客户端client通过GET首部字段发送请求,判断响应状态码是否是200

方法一. 通过reverse得到url

from django.urls import reverse

class HomeTest(TestCase):
    def test_home_view_status_code(self):
        """通过状态码检测请求是否成功"""
        url = reverse('home') # 从路径名得到路径url
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200) # 比较响应状态码和200是否一致

方法二. 直接url

class HomeTest(TestCase):
    def test_home_view_status_code(self):
        """通过状态码检测请求是否成功"""
        response = self.client.get('/home') # 响应为客户端发送'/home'请求
        self.assertEqual(response.status_code, 200) # 比较响应状态码和200是否一致
  • python manage.py test
PS D:\项目管理\Hello_world_django\myproject> python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.215s

OK
Destroying test database for alias 'default'...
PS D:\项目管理\Hello_world_django\myproject> 
  • python manage.py test --veibosity=2
PS D:\项目管理\Hello_world_django\myproject> python manage.py test --verbosity=2 
Creating test database for alias 'default' ('file:memorydb_default?mode=memory&cache=shared')...
Operations to perform:
  Synchronize unmigrated apps: messages, staticfiles
  Apply all migrations: admin, auth, boards, contenttypes, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying boards.0001_initial... OK
  Applying sessions.0001_initial... OK
System check identified no issues (0 silenced).
test_home_view_status_code (boards.tests.HomeTest)
通过状态码检测请求是否成功 ... ok

----------------------------------------------------------------------
Ran 1 test in 0.060s

OK
Destroying test database for alias 'default' ('file:memorydb_default?mode=memory&cache=shared')...

【2】服务端返回的视图是否正确

  • 检测Django是否在请求URL的时候返回了正确的视图函数,因为一个网站肯定有无数个视图函数,urls也会很庞大

1. 通过函数检测

from django.urls import resolve, reverse
from boards.views import home

def test_home_url_resolve_home_view(self):
        """检测请求成功后,django是否返回了正确对应的视图函数"""
        view = resolve('/home')  # 从url路径得到对应的路径名字
        self.assertEqual(view.func, home)  # 对应的函数

2. 通过视图函数名检测

def test_home_url_resolve_home_view(self):
        """检测请求成功后,django是否返回了正确对应的视图函数"""
        view = resolve('/home')  # 从url路径得到对应的路径名字
        self.assertEqual(view.view_name, 'home')  # 对应的函数

【3】完整代码

from django.test import TestCase
# Create your tests here.
from django.urls import resolve, reverse
from boards.views import home


class HomeTest(TestCase):
    def test_home_view_status_code(self):
        """通过状态码检测请求是否成功"""
        url = reverse('home') # 从路径名得到路径url
        response = self.client.get(url)
        # response = self.client.get('/home') # 响应为客户端发送'/home'请求
        self.assertEqual(response.status_code, 200) # 比较响应状态码和200是否一致

    def test_home_url_resolve_home_view(self):
        """检测请求成功后,django是否返回了正确对应的视图函数"""
        view = resolve('/home')  # 从url路径得到对应的路径名字
        self.assertEqual(view.func, home)  # 对应的函数

  • python manage.py test
PS D:\项目管理\Hello_world_django\myproject> python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 0.030s

OK
Destroying test database for alias 'default'...

【4】小结

  • Django URL中 reverseresolve 区别

1. reverse

从url_name(路径名)得到URL具体路径

url = reverse('home') # home是路径的名称

在urls.py中:path('home', views.home, name='home')

2. resolve

从URL具体路径得到url_name(路径名)

view = resolve('/home') # '/home'是客户端请求url

在urls.py中:path('home', views.home, name='home')

猜你喜欢

转载自blog.csdn.net/weixin_44478378/article/details/105689734