Chapter 10. Input Validation and Test Organisation
Over the next few chapters we’ll talk about testing and implementing validation of user inputs. We’ll also take the opportunity to do a little tidying up—both in our application code, and also in our tests.
Validation FT: Preventing Blank Items
As our first few users start using the site, we’ve noticed they sometimes make mistakes that mess up their lists, like accidentally submitting blank list items, or accidentally inputting two identical items to a list. Computers are meant to help stop us from making silly mistakes, so let’s see if we can get our site to help.
Here’s the outline of an FT:
functional_tests/tests.py (ch10l001).
deftest_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# The home page refreshes, and there is an error message saying# that list items cannot be blank# She tries again with some text for the item, which now works# Perversely, she now decides to submit a second blank list item# She receives a similar warning on the list page# And she can correct it by filling some text inself.fail('write me!')
That’s all very well, but before we go any further—our functional tests file is beginning to get a little crowded. Let’s split it out into several files, in which each has a single test method.
Remember that functional tests are closely linked to “user stories”. If you were using some sort of project ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access