lists/views.py (ch10l026-7).
def
view_list
(
request
,
list_id
):
[
...
]
item
.
save
()
return
redirect
(
list_
)
except
ValidationError
:
error
=
"You can't have an empty list item"
And a full unit test and functional test run to assure ourselves that everything still works:
$
python3 manage.py test lists
OK
$
python3 manage.py test functional_tests
OK
Cross off our to-dos:
•
Remove hardcoded URLs from views.py
•
Remove hardcoded URL from forms in
list.html and home.html
•
Remove duplication of validation logic in
views
Let’s do a commit:
$
git commit -am"Use get_absolute_url on List model to DRY urls in views"
That final to-do item will be the subject of the next chapter…
Tips on Organising Tests and Refactoring
Use a tests folder
Just as you use multiple files to hold your application code, you should split your
tests out into multiple files.
• Use a folder called
tests
, adding a
__init__.py
which imports all test classes.
• For functional tests, group them into tests for a particular feature or user story.
• For unit tests, you want a separate test file for each tested source code file. For
Django, that’s typically
test_models.py
,
test_views.py
, and
test_forms.py
.
• Have at least a placeholder test for
every
function and class.
190
|
Chapter 10: Input Validation and Test Organisation