..
---------------------------------------------------------------------
Ran 2 tests in 12.048s
OK
Destroying test database for alias 'default'...
As a final check, we rerun
all
the FTs:
$
python3 manage.py test functional_tests
Creating test database for alias 'default'...
....
---------------------------------------------------------------------
Ran 4 tests in 19.048s
OK
Destroying test database for alias 'default'...
Hooray! Time for a final commit, and a wrap-up of what we’ve learned about testing
views over the last few chapters.
Recap: What to Test in Views
Partial listing showing all view tests and assertions.
class
ListViewTest
(
TestCase
):
def
test_uses_list_template
(
self
):
response
=
self
.
client
.
get
(
'/lists/
%d
/'
%
(
list_
.
id
,))
#
self
.
assertTemplateUsed
(
response
,
'list.html'
)
#
def
test_passes_correct_list_to_template
(
self
):
self
.
assertEqual
(
response
.
context
[
'list'
],
correct_list
)
#
def
test_displays_item_form
(
self
):
self
.
assertIsInstance
(
response
.
context
[
'form'
],
ExistingListItemForm
)
#
self
.
assertContains
(
response
,
'name="text"'
)
def
test_displays_only_items_for_that_list
(
self
):
self
.
assertContains
(
response
,
'itemey 1'
)
#
self
.
assertContains
(
response
,
'itemey 2'
)
#
self
.
assertNotContains
(
response
,
'other list item 1'
)
#
def
test_can_save_a_POST_request_to_an_existing_list
(
self
):
self
.
assertEqual
(
Item
.
objects
.
count
(),
1
)
#
self
.
assertEqual
(
new_item
.
text
,
'A new item for an existing list'
)
#
def
test_POST_redirects_to_list_view
(
self
):
self
.
assertRedirects
(
response
,
'/lists/
%d
/'
%
(
correct_list
.
id
,))
#
def
test_for_invalid_input_nothing_saved_to_db
(
self
):
self
.
assertEqual
(
Item
.
objects
.
count
(),
0
)
#
def
test_for_invalid_input_renders_list_template
(
self
):
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertTemplateUsed
(
response
,
'list.html'
)
#
def
test_for_invalid_input_passes_form_to_template
(
self
):
self
.
assertIsInstance
(
response
.
context
[
'form'
],
ExistingListItemForm
)
#
def
test_for_invalid_input_shows_error_on_page
(
self
):
self
.
assertContains
(
response
,
escape
(
EMPTY_LIST_ERROR
))
#
def
test_duplicate_item_validation_errors_end_up_on_lists_page
(
self
):
self
.
assertContains
(
response
,
expected_error
)
self
.
assertTemplateUsed
(
response
,
'list.html'
)
self
.
assertEqual
(
Item
.
objects
.
all
()
.
count
(),
1
)
Using the Existing List Item Form in the List View
|
223