Running a Single Test File
As a side bonus, we’re now able to run an individual test file, like this:
$
python3 manage.py test functional_tests.test_list_item_validation
[...]
AssertionError: write me!
Brilliant, no need to sit around waiting for all the FTs when we’re only interested in a
single one. Although we need to remember to run all of them now and again, to check
for regressions. Later in the book we’ll see how to give that task over to an automated
Continuous Integration loop. For now let’s commit!
$
git status
$
git add functional_tests
$
git commit -m"Moved Fts into their own individual files"
Fleshing Out the FT
Now let’s start implementing the test, or at least the beginning of it:
functional_tests/test_list_item_validation.py (ch10l008).
def
test_cannot_add_empty_list_items
(
self
):
# Edith goes to the home page and accidentally tries to submit
# an empty list item. She hits Enter on the empty input box
self
.
browser
.
get
(
self
.
server_url
)
self
.
browser
.
find_element_by_id
(
'id_new_item'
)
.
send_keys
(
'
\n
'
)
# The home page refreshes, and there is an error message saying
# that list items cannot be blank
error
=
self
.
browser
.
find_element_by_css_selector
(
'.has-error'
)
#
self
.
assertEqual
(
error
.
text
,
"You can't have an empty list item"
)
# She tries again with some text for the item, which now works
self
.
browser
.
find_element_by_id
(
'id_new_item'
)
.
send_keys
(
'Buy milk
\n
'
)
self
.
check_for_row_in_list_table
(
'1: Buy milk'
)
#
# Perversely, she now decides to submit a second blank list item
self
.
browser
.
find_element_by_id
(
'id_new_item'
)
.
send_keys
(
'
\n
'
)
# She receives a similar warning on the list page
self
.
check_for_row_in_list_table
(
'1: Buy milk'
)
error
=
self
.
browser
.
find_element_by_css_selector
(
'.has-error'
)
self
.
assertEqual
(
error
.
text
,
"You can't have an empty list item"
)
# And she can correct it by filling some text in
self
.
browser
.
find_element_by_id
(
'id_new_item'
)
.
send_keys
(
'Make tea
\n
'
)
self
.
check_for_row_in_list_table
(
'1: Buy milk'
)
self
.
check_for_row_in_list_table
(
'2: Make tea'
)
A couple of things to note about this test:
174
|
Chapter 10: Input Validation and Test Organisation