django基础测试

测试首页

# coding=utf-8
# @author   :迷心兔
# time      :2019/11/10
from django.test import TestCase
from django.urls import resolve, reverse
from django.utils import timezone
from ..models import Post
from ..views import post_list
from django.contrib.auth.models import User


# 测试首页
class HomeTests(TestCase):

    def setUp(self):
        self.username = "mi"
        self.password = "123456"
        user = User.objects.create_user(username = self.username, password = self.password)
        self.post = Post.objects.create(
            author = user,
            title = "test title",
            text = "test text",
            created_date = timezone.now( ),
            published_date = timezone.now( ),
        )
        self.home_url = reverse('post_list')


# 没有登陆的测试
class NotLoginHomeTests(HomeTests):

    def setUp(self):
        super( ).setUp( )
        self.response = self.client.get(self.home_url)

    # 测试成功响应码
    def test_view_home_success_status_code(self):
        self.assertEquals(self.response.status_code, 200)

    # 测试链接能正常显示
    def test_url_home_resolves_view(self):
        view = resolve('/blog/')
        self.assertEquals(view.func, post_list)

    # 测试页面中是否包含某些链接
    def test_view_home_contains_navigation_links(self):
        login_url = reverse('login')  # 登陆链接
        board_topics_url = reverse('post_detail', kwargs = {'pk': self.post.pk})  # 详情链接
        self.assertContains(self.response, 'href="{0}"'.format(login_url))
        self.assertContains(self.response, 'href="{0}"'.format(board_topics_url))


# 成功登陆的测试
class LoginSuccessHomeTests(HomeTests):
    def setUp(self):
        super( ).setUp( )
        self.client.login(username = self.username, password = self.password)
        self.response = self.client.get(self.home_url)

    # 测试成功响应码
    def test_view_home_success_status_code(self):
        self.assertEquals(self.response.status_code, 200)

    # 测试链接能正常显示
    def test_url_home_resolves_view(self):
        view = resolve('/blog/')
        self.assertEquals(view.func, post_list)

    # 测试页面中是否包含某些链接
    def test_view_home_contains_navigation_links(self):
        logout_url = reverse('logout')  # 退出链接
        post_new_url = reverse('post_new')  # 新建链接
        board_topics_url = reverse('post_detail', kwargs = {'pk': self.post.pk})  # 详情链接
        self.assertContains(self.response, 'href="{0}"'.format(logout_url))
        self.assertContains(self.response, 'href="{0}"'.format(post_new_url))
        self.assertContains(self.response, 'href="{0}"'.format(board_topics_url))


测试详情页面

# coding=utf-8
# @author   :迷心兔
# time      :2019/11/10
from django.test import TestCase
from django.urls import resolve, reverse
from django.utils import timezone
from ..models import Post
from ..views import post_detail
from django.contrib.auth.models import User


# 测试详情页面
class DetailTests(TestCase):

    def setUp(self):
        self.username = "mi"
        self.password = "123456"
        self.user = User.objects.create_user(username = self.username, password = self.password)
        self.post = Post.objects.create(
            author = self.user,
            title = "test title",
            text = "test text",
            created_date = timezone.now( ),
            published_date = timezone.now( ),
        )
        self.post_detail_url = reverse('post_detail', kwargs = {'pk': self.post.pk})


# 没有登陆的测试
class NotLoginHomeTests(DetailTests):

    def setUp(self):
        super( ).setUp( )
        self.response = self.client.get(self.post_detail_url)

    # 测试成功响应码
    def test_view_post_detail_success_status_code(self):
        response = self.client.get(self.post_detail_url)
        self.assertEquals(response.status_code, 200)

    # 测试失败响应码
    def test_view_post_detail_not_found_status_code(self):
        url = reverse('post_detail', kwargs = {'pk': 99})
        response = self.client.get(url)
        self.assertEquals(response.status_code, 404)

    # 测试链接能正常显示
    def test_url_post_detail_resolves_view(self):
        view = resolve('/blog/post/1/')
        self.assertEquals(view.func, post_detail)

    # 测试页面中是否包含某些链接
    def test_view_post_detail_contains_navigation_links(self):
        login_url = reverse('login')
        response = self.client.get(self.post_detail_url)
        self.assertContains(response, 'href="{0}"'.format(login_url))


# 成功登陆的测试
class LoginSuccessNewTests(DetailTests):
    def setUp(self):
        super( ).setUp( )
        self.client.login(username = self.username, password = self.password)
        self.response = self.client.get(self.post_detail_url)

    # 测试成功响应码
    def test_view_post_detail_success_status_code(self):
        response = self.client.get(self.post_detail_url)
        self.assertEquals(response.status_code, 200)

    # 测试失败响应码
    def test_view_post_detail_not_found_status_code(self):
        url = reverse('post_detail', kwargs = {'pk': 99})
        response = self.client.get(url)
        self.assertEquals(response.status_code, 404)

    # 测试链接能正常显示
    def test_url_post_detail_resolves_view(self):
        view = resolve('/blog/post/1/')
        self.assertEquals(view.func, post_detail)

    # 测试页面中是否包含某些链接
    def test_view_post_detail_contains_navigation_links(self):
        logout_url = reverse('logout')  # 退出链接
        post_new_url = reverse('post_new')  # 新建链接
        self.assertContains(self.response, 'href="{0}"'.format(logout_url))
        self.assertContains(self.response, 'href="{0}"'.format(post_new_url))

测试新建文章页面

# coding=utf-8
# @author   :迷心兔
# time      :2019/11/10
from django.test import TestCase
from django.urls import resolve, reverse
from django.utils import timezone
from ..models import Post
from ..views import post_new
from django.contrib.auth.models import User
from django.forms import ModelForm


# 测试新建文章页面
class NewTests(TestCase):
    def setUp(self):
        self.username = "mi"
        self.password = "123456"
        self.user = User.objects.create_user(username = self.username, password = self.password)
        self.post = Post.objects.create(
            author = self.user,
            title = "test title",
            text = "test text",
            created_date = timezone.now( ),
            published_date = timezone.now( ),
        )
        self.post_new_url = reverse('post_new')


# 需要登录后的测试
class LoginRequiredNewViewTests(NewTests):

    # 测试失败响应码
    def test_view_post_new_not_found_status_code(self):
        self.response = self.client.get(self.post_new_url)
        self.assertEquals(self.response.status_code, 302)

    # 测试重定向操作
    def test_redirection(self):
        login_url = reverse('login')
        response = self.client.get(self.post_new_url)
        self.assertRedirects(
            response,
            '{login_url}?next={url}'.format(login_url = login_url, url = self.post_new_url))


# 成功登陆的测试
class LoginSuccessNewTests(NewTests):
    def setUp(self):
        super( ).setUp( )
        self.client.login(username = self.username, password = self.password)
        self.response = self.client.get(self.post_new_url)

    # 测试成功响应码
    def test_view_post_new_success_status_code(self):
        self.assertEquals(self.response.status_code, 200)

    # 测试链接能正常显示
    def test_url_post_new_resolves_view(self):
        view = resolve('/blog/post/new/')
        self.assertEquals(view.func, post_new)

    # 测试页面中是否包含某些链接
    def test_view_post_new_contains_navigation_links(self):
        logout_url = reverse('logout')
        url = reverse('post_new')
        response = self.client.get(url)
        self.assertContains(response, 'href="{0}"'.format(logout_url))

    # 测试表单是否存在
    def test_contains_form(self):
        form = self.response.context.get('form')
        self.assertIsInstance(form, ModelForm)

    # 测试表单是否设置跨站防护
    def test_csrf(self):
        self.assertContains(self.response, 'csrfmiddlewaretoken')

    # 测试表单组件是否存在
    def test_form_inputs(self):
        self.assertContains(self.response, '<input', 2)
        self.assertContains(self.response, '<textarea', 1)

    # 测试表单提交后的重定向链接
    def test_redirection(self):
        response = self.client.post(
            self.post_new_url,
            {
                "title": "test title",
                "text" : "test text",
            }
        )
        post_detail_url = reverse('post_detail', kwargs = {'pk': int(self.post.pk) + 1})
        self.assertRedirects(response, post_detail_url)

测试编辑页面

# coding=utf-8
# @author   :迷心兔
# time      :2019/11/10
from django.forms import ModelForm
from django.test import TestCase
from django.urls import resolve, reverse
from django.utils import timezone
from ..models import Post
from ..views import post_edit
from django.contrib.auth.models import User


# 测试编辑页面
class EditTests(TestCase):

    def setUp(self):
        user = User.objects.create_user(username = "mi", password = "123456")
        self.post = Post.objects.create(
            author = user,
            title = "test title",
            text = "test text",
            created_date = timezone.now( ),
            published_date = timezone.now( ),
        )
        self.post_edit_url = reverse('post_edit', kwargs = {'pk': self.post.pk})


# 需要登录后更新查看测试
class LoginRequiredPostEditViewTests(EditTests):
    # 测试重定向操作
    def test_redirection(self):
        login_url = reverse('login')
        response = self.client.get(self.post_edit_url)
        self.assertRedirects(response, '{login_url}?next={url}'.format(login_url = login_url, url = self.post_edit_url))


# 未经授权的更新后查看测试
class UnauthorizedPostEditViewTests(EditTests):

    def setUp(self):
        super( ).setUp( )
        username = 'who'
        password = '654321'
        User.objects.create_user(username = username, password = password)
        self.client.login(username = username, password = password)

        self.response = self.client.get(self.post_edit_url)

    # 测试不是作者也能显示编辑页面,提示错误信息
    def test_view_post_edit_success_status_code(self):
        self.assertEquals(self.response.status_code, 200)

    # 测试不是作者页面中是否包含某些链接
    def test_view_post_edit_contains_link_to_topics_page(self):
        post_list_url = reverse('post_list')
        self.assertContains(self.response, 'href="{0}"'.format(post_list_url))


class AuthorPostEditTests(EditTests):
    def setUp(self):
        super( ).setUp( )
        self.client.login(username = "mi", password = "123456")
        self.response = self.client.get(self.post_edit_url)

    # 测试成功响应码
    def test_view_post_edit_success_status_code(self):
        self.assertEquals(self.response.status_code, 200)

    # 测试链接能正常显示
    def test_url_edit_resolves_view(self):
        view = resolve('/blog/post/1/edit/')
        self.assertEquals(view.func, post_edit)

    # 测试页面中是否包含某些链接
    def test_view_post_edit_contains_navigation_links(self):
        logout_url = reverse('logout')
        url = reverse('post_new')
        response = self.client.get(url)
        self.assertContains(response, 'href="{0}"'.format(logout_url))

    # 测试表单是否存在
    def test_contains_form(self):
        form = self.response.context.get('form')
        self.assertIsInstance(form, ModelForm)

    # 测试表单是否设置跨站防护
    def test_csrf(self):
        self.assertContains(self.response, 'csrfmiddlewaretoken')

    # 测试表单组件是否存在
    def test_form_inputs(self):
        self.assertContains(self.response, '<input', 2)
        self.assertContains(self.response, '<textarea', 1)

    # 测试表单提交后的重定向链接
    def test_redirection(self):
        response = self.client.post(
            self.post_edit_url,
            {
                "title": "test new title",
                "text" : "test new text",
            }
        )
        post_detail_url = reverse('post_detail', kwargs = {'pk': int(self.post.pk)})
        self.assertRedirects(response, post_detail_url)

测试登陆表单的组件

# coding=utf-8
# @author   :迷心兔
# time      :2019/11/10
from django import forms
from django.test import TestCase

from ..templatetags.form_tags import field_type, input_class


# 模拟登陆表单
class ExampleForm(forms.Form):
    username = forms.CharField( )
    password = forms.CharField(widget = forms.PasswordInput( ))

    class Meta:
        fields = ('username', 'password')


# 测试登陆页面的组件
class FieldTypeTests(TestCase):

    # 测试字段小部件类型
    def test_field_widget_type(self):
        form = ExampleForm( )
        self.assertEquals('TextInput', field_type(form['username']))
        self.assertEquals('PasswordInput', field_type(form['password']))


# 用户名或密码输入测试
class InputClassTests(TestCase):
    # 没有输入数据的测试
    def test_unbound_field_initial_state(self):
        form = ExampleForm( )  # unbound form
        self.assertEquals('form-control ', input_class(form['username']))

    # 输入错误数据的测试
    def test_valid_bound_field(self):
        form = ExampleForm({'username': 'john', 'password': '123'})  # bound form (field + data)
        self.assertEquals('form-control is-valid', input_class(form['username']))
        self.assertEquals('form-control ', input_class(form['password']))  #

    # 输入数据不完整的测试
    def test_invalid_bound_field(self):
        form = ExampleForm({'username': '', 'password': '123'})  # bound form (field + data)
        self.assertEquals('form-control is-invalid', input_class(form['username']))

测试新建和编辑文章组件

# coding=utf-8
# @author   :迷心兔
# time      :2019/11/10
from django import forms
from django.test import TestCase
from ..forms import PostForm  # 文章表单
from ..templatetags.form_tags import field_type, input_class


# 测试新建页面的组件
class FieldTypeTests(TestCase):

    # 测试字段小部件类型
    def test_field_widget_type(self):
        form = PostForm( )
        self.assertEquals('TextInput', field_type(form['title']))
        self.assertEquals('Textarea', field_type(form['text']))


# 用户名或密码输入测试
class InputClassTests(TestCase):
    # 没有输入数据的测试
    def test_unbound_field_initial_state(self):
        form = PostForm( )
        self.assertEquals('form-control ', input_class(form['title']))

    # 输入错误数据的测试
    def test_valid_bound_field(self):
        form = PostForm({'title': 'test_title', 'text': 'test_text'})
        self.assertEquals('form-control is-valid', input_class(form['title']))

    # 输入数据不完整的测试
    def test_invalid_bound_field(self):
        form = PostForm({'title': '', 'text': 'test_text'})
        self.assertEquals('form-control is-invalid', input_class(form['title']))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷心兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值