from django.test import TestCase
from .models import Entry
class EntryModelTest(TestCase):
def test_string_representation(self):
entry = Entry(title="My entry title")
self.assertEqual(str(entry), entry.title)
def test_verbose_name_plural(self):
self.assertEqual(str(Entry._meta.verbose_name_plural), "entries")
class ProjectTests(TestCase):
def test_homepage(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
def test_judge_list_user_has_none_agency_returns_404(self):
url = reverse('agency:judge_list')
self.user.agency = None
self.user.save()
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
from django.contrib.auth import get_user_model
class HomePageTests(TestCase):
"""Test whether our blog entries show up on the homepage"""
def setUp(self):
self.user = get_user_model().objects.create(username='some_user')
def test_one_entry(self):
Entry.objects.create(title='1-title', body='1-body', author=self.user)
response = self.client.get('/')
self.assertContains(response, '1-title')
self.assertContains(response, '1-body')
def test_two_entries(self):
Entry.objects.create(title='1-title', body='1-body', author=self.user)
Entry.objects.create(title='2-title', body='2-body', author=self.user)
response = self.client.get('/')
self.assertContains(response, '1-title')
self.assertContains(response, '1-body')
self.assertContains(response, '2-title')
def test_no_entries(self):
response = self.client.get('/')
self.assertContains(response, 'No blog entries yet.')
def test_judge_create_displays_form(self):
url = reverse('agency:judge_create')
response = self.client.get(url)
self.assertContains(response, 'name="first_name"')
self.assertContains(response, 'name="last_name"')
self.assertContains(response, 'name="middle_name"')
self.assertContains(response, 'name="status"')
def test_valid_data(self):
form = CommentForm({
'name': "Turanga Leela",
'email': "[email protected]",
'body': "Hi there",
}, entry=self.entry)
self.assertTrue(form.is_valid())
comment = form.save()
self.assertEqual(comment.name, "Turanga Leela")
self.assertEqual(comment.email, "[email protected]")
self.assertEqual(comment.body, "Hi there")
self.assertEqual(comment.entry, self.entry)
def test_blank_data(self):
form = CommentForm({}, entry=self.entry)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors, {
'name': ['required'],
'email': ['required'],
'body': ['required'],
})
The Django test client cannot test form submissions, but WebTest can. We’ll use django-webtest to handle testing the form submission.
from django_webtest import WebTest
class EntryViewTest(WebTest):
def test_view_page(self):
page = self.app.get(self.entry.get_absolute_url())
self.assertEqual(len(page.forms), 1)
def test_form_error(self):
page = self.app.get(self.entry.get_absolute_url())
page = page.form.submit()
self.assertContains(page, "This field is required.")
def test_form_success(self):
page = self.app.get(self.entry.get_absolute_url())
page.form['name'] = "Phillip"
page.form['email'] = "[email protected]"
page.form['body'] = "Test comment body."
page = page.form.submit()
self.assertRedirects(page, self.entry.get_absolute_url())
Coverage, a tool that measures code coverage for Python code, will be used to check what percentage of the tutorial code is being tested.
First install coverage with pip
$ coverage run --include='./*' manage.py test
Prettify the coverage report
coverage html
Branch coverage
coverage run --include='./*' --branch manage.py test
coverage report
Coverage configuration (.coveragerc)
[run]
include = ./*
branch = True
Now we can run -->
coverage run manage.py test
coverage html
def test_gravatar_url(self):
comment = Comment(body="My comment body", email="[email protected]")
expected = "http://www.gravatar.com/avatar/5658ffccee7f0ebfda2b226238b1eb6e"
self.assertEqual(comment.gravatar_url(), expected)
Averiguar sobre BDD (Behavioral-Driven development)