Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
PMPL
Collection of Practice
2019
1406568923-practice
Commits
4a734003
Commit
4a734003
authored
Oct 15, 2019
by
emil farisan
Browse files
Split out unit tests into two files
parent
a7d5ce48
Changes
4
Hide whitespace changes
Inline
Side-by-side
functional_tests/base.py
View file @
4a734003
...
...
@@ -27,3 +27,13 @@ class FunctionalTest(StaticLiveServerTestCase):
if
time
.
time
()
-
start_time
>
MAX_WAIT
:
raise
e
time
.
sleep
(
0.5
)
def
wait_for
(
self
,
fn
):
start_time
=
time
.
time
()
while
True
:
try
:
return
fn
()
except
(
AssertionError
,
WebDriverException
)
as
e
:
if
time
.
time
()
-
start_time
>
MAX_WAIT
:
raise
e
time
.
sleep
(
0.5
)
functional_tests/test_list_item_validation.py
View file @
4a734003
...
...
@@ -5,19 +5,35 @@ from .base import FunctionalTest
class
ItemValidationTest
(
FunctionalTest
):
@
skip
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
.
live_server_url
)
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
Keys
.
ENTER
)
# The home page refreshes, and there is an error message saying
# that list items cannot be blank
self
.
wait_for
(
lambda
:
self
.
assertEqual
(
self
.
browser
.
find_element_by_css_selector
(
'.has-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'
)
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
Keys
.
ENTER
)
self
.
wait_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
(
Keys
.
ENTER
)
# She receives a similar warning on the list page
self
.
wait_for
(
lambda
:
self
.
assertEqual
(
self
.
browser
.
find_element_by_css_selector
(
'.has-error'
).
text
,
"You can't have an empty list item"
))
# And she can correct it by filling some text in
self
.
fail
(
'write me!'
)
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
'Make tea'
)
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
Keys
.
ENTER
)
self
.
wait_for_row_in_list_table
(
'1: Buy milk'
)
self
.
wait_for_row_in_list_table
(
'2: Make tea'
)
lists/tests/test_
all
.py
→
lists/tests/test_
models
.py
View file @
4a734003
from
django.test
import
TestCase
from
lists.models
import
Item
from
lists.models
import
Item
,
List
class
HomePageTest
(
TestCase
):
...
...
lists/tests/test_views.py
0 → 100644
View file @
4a734003
from
django.test
import
TestCase
from
lists.models
import
Item
class
HomePageTest
(
TestCase
):
def
test_uses_home_template
(
self
):
response
=
self
.
client
.
get
(
'/'
)
self
.
assertTemplateUsed
(
response
,
'home.html'
)
def
test_can_save_a_POST_request
(
self
):
self
.
client
.
post
(
'/'
,
data
=
{
'item_text'
:
'A new list item'
})
self
.
assertEqual
(
Item
.
objects
.
count
(),
1
)
new_item
=
Item
.
objects
.
first
()
self
.
assertEqual
(
new_item
.
text
,
'A new list item'
)
def
test_redirects_after_POST
(
self
):
response
=
self
.
client
.
post
(
'/'
,
data
=
{
'item_text'
:
'A new list item'
})
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
response
[
'location'
],
'/'
)
def
test_home_page_returns_correct_html
(
self
):
response
=
self
.
client
.
get
(
'/'
)
html
=
response
.
content
.
decode
(
'utf8'
)
self
.
assertTrue
(
html
.
startswith
(
'<html>'
))
self
.
assertIn
(
'<title>To-Do lists</title>'
,
html
)
self
.
assertTrue
(
html
.
strip
().
endswith
(
'</html>'
))
self
.
assertTemplateUsed
(
response
,
'home.html'
)
def
test_only_saves_items_when_necessary
(
self
):
self
.
client
.
get
(
'/'
)
self
.
assertEqual
(
Item
.
objects
.
count
(),
0
)
def
test_displays_all_list_items
(
self
):
Item
.
objects
.
create
(
text
=
'itemey 1'
)
Item
.
objects
.
create
(
text
=
'itemey 2'
)
response
=
self
.
client
.
get
(
'/'
)
self
.
assertIn
(
'itemey 1'
,
response
.
content
.
decode
())
self
.
assertIn
(
'itemey 2'
,
response
.
content
.
decode
())
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment