จากไฟล์ functional_test.py
from
selenium
import
webdriver
browser
=
webdriver
.
Firefox
()
# Edith has heard about a cool new online to-do app. She goes
# to check out its homepage
browser
.
get
(
'http://localhost:8000'
)
# She notices the page title and header mention to-do lists
assert
'To-Do'
in
browser
.
title
# She is invited to enter a to-do item straight away
# She types "Buy peacock feathers" into a text box (Edith's hobby
# is tying fly-fishing lures)
# When she hits enter, the page updates, and now the page lists
# "1: Buy peacock feathers" as an item in a to-do list
# There is still a text box inviting her to add another item. She
# enters "Use peacock feathers to make a fly" (Edith is very methodical)
# The page updates again, and now shows both items on her list
# Edith wonders whether the site will remember her list. Then she sees
# that the site has generated a unique URL for her -- there is some
# explanatory text to that effect.
# She visits that URL - her to-do list is still there.
# Satisfied, she goes back to sleep
browser
.
quit
()
ทำการรัน serverและไฟล์ test
$python manage.py runserver
$python funtional_tests.py
Traceback (most recent call last):
File "Functional_tests.py", line 10, in <module>
assert 'To-Do' in browser.title
AssertionError
เมื่อรันแล้วโปรแกรมจะแสดงว่ามีการ Error คือ ชื่อ title ของบราวเซอร์ไม่ตรงกับคำว่า 'To-Do'
จากนั้นทำการแก้ไขไฟล์ funtional_tests.py
#1from selenium import webdriver import unittest class NewVisitorTest(unittest.TestCase): #1 def setUp(self): #2 self.browser = webdriver.Firefox() def tearDown(self): #3 self.browser.quit() def test_can_start_a_list_and_retrieve_it_later(self): #4 # Edith has heard about a cool new online to-do app. She goes # to check out its homepage self.browser.get('http://localhost:8000') # She notices the page title and header mention to-do lists self.assertIn('To-Do', self.browser.title) #5 self.fail('Finish the test!') #6 # She is invited to enter a to-do item straight away #[...rest of comments as before] if __name__ == '__main__': #7 unittest.main() #8
ทำการรันไฟล์นี้อีกครั้ง
$python funtional_tests.py
F ====================================================================== FAIL: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "functional_tests.py", line 18, in test_can_start_a_list_and_retrieve_it_later self.assertIn('To-Do', self.browser.title) #5 AssertionError: 'To-Do' not found in u'Welcome to Django' ---------------------------------------------------------------------- Ran 1 test in 5.698s FAILED (failures=1)
เมื่อรันแล้วจะพบว่ามีการ Error คือชื่อ title ของbrowserไม่ตรงกับที่เราต้องการคือ 'To-Do'
และ browser จะเปิดขึ้นและปิดไปทันที เนื่องจากในคลาสมีการกำหนดให้มีการ setUp และ tearDown อยู่ด้วย โดยฟังก์ชั่น setUp นั้นเป็นการรันโปรแกรมก่อน และฟังก์ชั่น tearDown เป็นคำสั่งปิด browser จะทำหลังจากที่มีการ test เสร็จแล้ว ดังนั้น browser จึงปิดเร็วมาก
จึงมีการแก้ไขไฟล์โดยการเพิ่มคำสั่งเข้าไปในฟังก์ชั่น setUp
[
...
]
def
setUp
(
self
):
self
.
browser
=
webdriver
.
Firefox
()
self
.
browser
.
implicitly_wait
(
3
)
def
tearDown
(
self
):
[
...
]
เมื่อรันแล้วจะพบว่ามีการ Error เหมือนเดิม แต่ช่วงเวลาในการปิด-เปิด จะนานขึ้นมาเป็น 3 วินาที จากคำสั่ง implicitly_wait(3) เป็นคำสั่งให้ browser รอ 3 วินาที
ต่อไปเป็นคำสั่ง git diff เป็นคำสั่งไว้สำหรับดูว่าได้กระทำกับไฟล์อะไรอย่างไรบ้าง
$ git diff
diff --git a/.gitignore b/.gitignore
index 49ef255..3ba11e5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
db.sqlite3
+__pycache__
+*.pyc
diff --git a/functional_tests.py b/functional_tests.py
index df73c70..b6322b7 100644
--- a/functional_tests.py
+++ b/functional_tests.py
@@ -1,6 +1,26 @@
from selenium import webdriver
+import unittest
-browser = webdriver.Firefox()
-browser.get('http://localhost:8000')
+class NewVisitorTest(unittest.TestCase): #1
-assert 'Django' in browser.title
+ def setUp(self): #2
+ self.browser = webdriver.Firefox()
:
และทำการ commit โดยพิมพ์ไปว่า First FT specced out in comments, and now uses unittest
$git commit -a
$git log
commit d69c2f03402ae2fec3afc49c08d244d5bb6bbd23
Author: jutamas <jutamas@jutamas.(none)>
Date: Wed May 21 11:04:59 2014 +0700
Fist FT specced out in comments, and now uses unittest
commit c7ec8f9cc6c4bf59c8d8dbc51520df2d3c50de2a
Author: jutamas <jutamas@jutamas.(none)>
Date: Tue May 20 11:40:50 2014 +0700
First commit: First FT and basic Django config