django中註冊功能signup的測試例
阿新 • • 發佈:2018-12-06
這個是套路,
有了這些測試用例,
開發就會更有保障,
且每次重寫,都好麻煩,
不好cp來得快~~:)
from django.test import TestCase from django.urls import reverse, resolve from django.contrib.auth.models import User from accounts.views import signup from accounts.forms import SignUpForm # Create your tests here. class SignUpTests(TestCase):def setUp(self): url = reverse('signup') self.response = self.client.get(url) def test_signup_status_code(self): self.assertEqual(self.response.status_code, 200) def test_signup_url_resolves_signup_view(self): view = resolve('/signup/') self.assertEqual(view.func, signup)def test_csrf(self): self.assertContains(self.response, 'csrfmiddlewaretoken') def test_contains_form(self): form = self.response.context.get('form') self.assertIsInstance(form, SignUpForm) def test_form_inputs(self): self.assertContains(self.response,'<input', 5) self.assertContains(self.response, 'type="text"', 1) self.assertContains(self.response, 'type="email"', 1) self.assertContains(self.response, 'type="password"', 2) class SuccessfulSignUpTests(TestCase): def setUp(self): url = reverse('signup') data = { 'username': 'john', 'email': '[email protected]', 'password1': '123456', 'password2': '123456' } self.response = self.client.post(url, data) self.home_url = reverse('home') def test_redirection(self): self.assertRedirects(self.response, self.home_url) def test_user_creation(self): self.assertTrue(User.objects.exists()) def test_user_authentication(self): response = self.client.get(self.home_url) user = response.context.get('user') self.assertTrue(user.is_authenticated) class InvalidSignUpTests(TestCase): def setUp(self): url = reverse('signup') self.response = self.client.post(url, {}) def test_signup_status_code(self): self.assertEqual(self.response.status_code, 200) def test_form_errors(self): form = self.response.context.get('form') self.assertTrue(form.errors) def test_do_not_create_user(self): self.assertFalse(User.objects.exists())