测试首页
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))
测试详情页面
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))
测试新建文章页面
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)
测试编辑页面
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)
测试登陆表单的组件
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( )
self.assertEquals('form-control ', input_class(form['username']))
def test_valid_bound_field(self):
form = ExampleForm({'username': 'john', 'password': '123'})
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'})
self.assertEquals('form-control is-invalid', input_class(form['username']))
测试新建和编辑文章组件
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']))