Skip to content

Commit 2289567

Browse files
committed
Convert test_views to pytest.
Expanded namespace of pytest Reference: pytest-dev/pytest-django#97 pytest-dev/pytest-django#232
1 parent b7fdb40 commit 2289567

File tree

3 files changed

+99
-78
lines changed

3 files changed

+99
-78
lines changed

functional_tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ def chrome_browser():
4444
@pytest.fixture()
4545
def browser(chrome_browser):
4646
return Browser(chrome_browser)
47+

lists/tests/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import inspect
2+
3+
def pytest_namespace():
4+
"""Make unittest assert methods available.
5+
This is useful for things such floating point checks with assertAlmostEqual.
6+
"""
7+
8+
from django.test import TestCase
9+
10+
class Dummy(TestCase):
11+
def dummy(self):
12+
pass
13+
14+
obj = Dummy('dummy')
15+
16+
names = {name: member
17+
for name, member in inspect.getmembers(obj)
18+
if name.startswith('assert') and '_' not in name}
19+
20+
return names

lists/tests/test_views.py

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,152 @@
1-
from django.test import TestCase
21
from django.utils.html import escape
32
from lists.forms import ItemForm, EMPTY_ITEM_ERROR, DUPLICATE_ITEM_ERROR, ExistingListItemForm
43
from lists.models import Item, List
5-
from unittest import skip
64

5+
import pytest
76

8-
class HomePageTest(TestCase):
9-
def test_home_page_renders_home_template(self):
10-
response = self.client.get('/')
11-
self.assertTemplateUsed(response, 'home.html')
7+
pytestmark = pytest.mark.django_db
128

13-
def test_home_page_uses_item_form(self):
14-
response = self.client.get('/')
15-
self.assertIsInstance(response.context['form'], ItemForm)
169

10+
class TestHomePage:
11+
def test_home_page_renders_home_template(self, client):
12+
response = client.get('/')
13+
pytest.assertTemplateUsed(response, 'home.html')
1714

18-
class ListViewTest(TestCase):
15+
def test_home_page_uses_item_form(self, client):
16+
response = client.get('/')
17+
assert isinstance(response.context['form'], ItemForm)
1918

20-
def post_invalid_input(self):
19+
20+
class TestListView:
21+
22+
def post_invalid_input(self, client):
2123
list_ = List.objects.create()
22-
return self.client.post(
24+
return client.post(
2325
'/lists/{}/'.format(list_.id),
2426
data={'text': ''}
2527
)
2628

27-
def test_uses_list_templates(self):
29+
def test_uses_list_templates(self, client):
2830
list_ = List.objects.create()
29-
response = self.client.get('/lists/{}/'.format(list_.id))
30-
self.assertTemplateUsed(response, 'list.html')
31+
response = client.get('/lists/{}/'.format(list_.id))
32+
pytest.assertTemplateUsed(response, 'list.html')
3133

32-
def test_displays_only_items_for_that_list(self):
34+
def test_displays_only_items_for_that_list(self, client):
3335
correct_list = List.objects.create()
3436
Item.objects.create(text='itemey 1', list=correct_list)
3537
Item.objects.create(text='itemey 2', list=correct_list)
3638
other_list = List.objects.create()
3739
Item.objects.create(text='other list item 1', list=other_list)
3840
Item.objects.create(text='other list item 2', list=other_list)
3941

40-
response = self.client.get('/lists/{}/'.format(correct_list.id))
42+
response = client.get('/lists/{}/'.format(correct_list.id))
4143

42-
self.assertContains(response, 'itemey 1')
43-
self.assertContains(response, 'itemey 2')
44-
self.assertNotContains(response, 'other list item 1')
45-
self.assertNotContains(response, 'other list item 2')
44+
pytest.assertContains(response, 'itemey 1')
45+
pytest.assertContains(response, 'itemey 2')
46+
pytest.assertNotContains(response, 'other list item 1')
47+
pytest.assertNotContains(response, 'other list item 2')
4648

47-
def test_passes_correct_list_to_template(self):
49+
def test_passes_correct_list_to_template(self, client):
4850
other_list = List.objects.create()
4951
correct_list = List.objects.create()
5052

51-
response = self.client.get('/lists/{}/'.format(correct_list.id))
53+
response = client.get('/lists/{}/'.format(correct_list.id))
5254

53-
self.assertEqual(response.context['list'], correct_list)
55+
assert correct_list == response.context['list']
5456

55-
def test_displays_item_form(self):
57+
def test_displays_item_form(self, client):
5658
list_ = List.objects.create()
57-
response = self.client.get('/lists/{}/'.format(list_.id))
58-
self.assertIsInstance(response.context['form'], ExistingListItemForm)
59-
self.assertContains(response, 'name="text"')
59+
response = client.get('/lists/{}/'.format(list_.id))
60+
assert isinstance(response.context['form'], ExistingListItemForm)
61+
pytest.assertContains(response, 'name="text"')
6062

61-
def test_can_save_a_POST_request_to_an_existing_list(self):
63+
def test_can_save_a_POST_request_to_an_existing_list(self, client):
6264
other_list = List.objects.create()
6365
correct_list = List.objects.create()
6466

65-
self.client.post(
67+
client.post(
6668
'/lists/{}/'.format(correct_list.id),
6769
data={'text': 'A new item for an existing list'}
6870
)
6971

70-
self.assertEqual(Item.objects.count(), 1)
72+
assert 1 == Item.objects.count()
7173
new_item = Item.objects.first()
72-
self.assertEqual(new_item.text, 'A new item for an existing list')
73-
self.assertEqual(new_item.list, correct_list)
74+
assert 'A new item for an existing list' == new_item.text
75+
assert correct_list == new_item.list
7476

75-
def test_POST_redirects_to_list_view(self):
77+
def test_POST_redirects_to_list_view(self, client):
7678
other_list = List.objects.create()
7779
correct_list = List.objects.create()
78-
response = self.client.post(
80+
response = client.post(
7981
'/lists/{}/'.format(correct_list.id),
8082
data={'text': 'A new item for an existing list'}
8183
)
82-
self.assertRedirects(response, '/lists/{}/'.format(correct_list.id))
83-
84-
def test_for_invalid_input_nothing_saved_to_db(self):
85-
self.post_invalid_input()
86-
self.assertEqual(Item.objects.count(), 0)
87-
88-
def test_for_invalid_input_renders_list_template(self):
89-
response = self.post_invalid_input()
90-
self.assertEqual(response.status_code, 200)
91-
self.assertTemplateUsed(response, 'list.html')
84+
pytest.assertRedirects(response, '/lists/{}/'.format(correct_list.id))
9285

93-
def test_for_invalid_input_passes_form_to_template(self):
94-
response = self.post_invalid_input()
95-
self.assertIsInstance(response.context['form'], ExistingListItemForm)
86+
def test_for_invalid_input_nothing_saved_to_db(self, client):
87+
self.post_invalid_input(client)
88+
assert 0 == Item.objects.count()
9689

97-
def test_for_invalid_input_shows_error_on_page(self):
98-
response = self.post_invalid_input()
99-
self.assertContains(response, escape(EMPTY_ITEM_ERROR))
90+
def test_for_invalid_input_renders_list_template(self, client):
91+
response = self.post_invalid_input(client)
92+
assert 200 == response.status_code
93+
pytest.assertTemplateUsed(response, 'list.html')
10094

95+
def test_for_invalid_input_passes_form_to_template(self, client):
96+
response = self.post_invalid_input(client)
97+
assert isinstance(response.context['form'], ExistingListItemForm)
10198

99+
def test_for_invalid_input_shows_error_on_page(self, client):
100+
response = self.post_invalid_input(client)
101+
pytest.assertContains(response, escape(EMPTY_ITEM_ERROR))
102102

103-
def test_duplicate_item_validation_errors_end_up_on_lists_page(self):
103+
def test_duplicate_item_validation_errors_end_up_on_lists_page(self, client):
104104
list1 = List.objects.create()
105105
item1 = Item.objects.create(list=list1, text='textey')
106-
response = self.client.post(
106+
response = client.post(
107107
'/lists/%d/' % (list1.id,),
108108
data={'text': 'textey'}
109109
)
110110

111111
expected_error = escape(DUPLICATE_ITEM_ERROR)
112-
self.assertContains(response, expected_error)
113-
self.assertTemplateUsed(response, 'list.html')
114-
self.assertEqual(Item.objects.all().count(), 1)
112+
pytest.assertContains(response, expected_error)
113+
pytest.assertTemplateUsed(response, 'list.html')
114+
assert 1 == Item.objects.all().count()
115115

116116

117-
class NewListTest(TestCase):
118-
def test_saving_a_POST_request(self):
119-
self.client.post(
117+
class TestNewList:
118+
def test_saving_a_POST_request(self, client):
119+
client.post(
120120
'/lists/new',
121121
data={'text': 'A new list item'}
122122
)
123123

124-
self.assertEqual(Item.objects.count(), 1)
124+
assert 1 == Item.objects.count(), 1
125125
new_item = Item.objects.first()
126-
self.assertEqual(new_item.text, 'A new list item')
126+
assert 'A new list item' == new_item.text
127127

128-
def test_redirects_after_POST(self):
129-
response = self.client.post(
128+
def test_redirects_after_POST(self, client):
129+
response = client.post(
130130
'/lists/new',
131131
data={'text': 'A new list item'}
132132
)
133133
new_list = List.objects.first()
134-
self.assertRedirects(response, '/lists/{}/'.format(new_list.id))
134+
pytest.assertRedirects(response, '/lists/{}/'.format(new_list.id))
135135

136-
def test_for_invalid_input_renders_home_template(self):
137-
response = self.client.post('/lists/new', data={'text': ''})
138-
self.assertEqual(response.status_code, 200)
139-
self.assertTemplateUsed(response, 'home.html')
136+
def test_for_invalid_input_renders_home_template(self, client):
137+
response = client.post('/lists/new', data={'text': ''})
138+
assert 200 == response.status_code
139+
pytest.assertTemplateUsed(response, 'home.html')
140140

141-
def test_validation_errors_are_shown_on_home_page(self):
142-
response = self.client.post('/lists/new', data={'text': ''})
143-
self.assertContains(response, escape(EMPTY_ITEM_ERROR))
141+
def test_validation_errors_are_shown_on_home_page(self, client):
142+
response = client.post('/lists/new', data={'text': ''})
143+
pytest.assertContains(response, escape(EMPTY_ITEM_ERROR))
144144

145-
def test_for_invalid_input_passes_form_to_template(self):
146-
response = self.client.post('/lists/new', data={'text': ''})
147-
self.assertIsInstance(response.context['form'], ItemForm)
145+
def test_for_invalid_input_passes_form_to_template(self, client):
146+
response = client.post('/lists/new', data={'text': ''})
147+
assert isinstance(response.context['form'], ItemForm)
148148

149-
def test_invalid_list_items_arent_saved(self):
150-
self.client.post('/lists/new', data={'text': ''})
151-
self.assertEqual(List.objects.count(), 0)
152-
self.assertEqual(Item.objects.count(), 0)
149+
def test_invalid_list_items_arent_saved(self, client):
150+
client.post('/lists/new', data={'text': ''})
151+
assert 0 == List.objects.count()
152+
assert 0 == Item.objects.count()

0 commit comments

Comments
 (0)