วันอาทิตย์ที่ 7 กุมภาพันธ์ พ.ศ. 2559

Test-Driven Development with Python - Chapter. 2

Extending Our Functional Test Using the unittest Module

      ทำการเเก้ไขไฟล์ functional_tests.py เพื่อทำการทดสอบ

 
    from selenium import webdriver
    browser = webdriver.Firefox()
    browser.get('http://localhost:8000')
    assert 'To-Do' in browser.title
    browser.quit()
 

     จากนั้นทำการทดสอบไฟล์ functional_tests.py โดยการ runserver เเละ functional_tests.py เพื่อทำการทดสอบ

 
   $ python3 manage.py runserver
  
 
   $ python3 functional_tests.py
 
ภาพ (run functional_tests.py เเละ มีการ AssertionError)
ภาพ (run functional_tests.py เเละมีการเรียกหน้า localhost:8000 ขึ้นมา)

     จากนั้นให้เราทำการเเก้ไข AssertionError โดยการระบุ title ของหน้าที่ Error เข้าเพื่อให้ง่ายต่อการตรวจสอบนั้นเอง ไปโดยการเพื่ม "Browser title was " + browser.title เข้าไป
 
    from selenium import webdriver
    browser = webdriver.Firefox()
    browser.get('http://localhost:8000')
    assert 'To-Do' in browser.title, "Browser title was " + browser.title
    browser.quit()
 
ภาพ (run functional_tests.py เเละ มีการ AssertionError เเละ บอกหน้าที่ทำการทดสอบ)
ภาพ (run functional_tests.py เเละมีการเรียกหน้า localhost:8000 ขึ้นมา)

     ทำการเเก้ไขไฟล์ functional_tests.py จากนั้นทำการ run functional_tests.py เพื่อทำการทดสอบอีกที

 
    from 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__':  
        unittest.main()  
  
  1. จัดระเบียบการ test เป็น class
  2. เป็นการเริ่มการทำงานของ browser  
  3. เป็นการหยุดการทำงาน browser  
  4. เป็นส่วน main ที่เอาไว้ใช้ test การทำงาน โดยจะเปิด browser ไปที่ http://localhost:8000
  5. ตรวจสอบว่า 'To-Do' ตรงกับ title ของ browser หรือไม่ 
  6. ใช้เป็นตัวเตือนว่าเสร็จสิ้นการ test


     เกิด การ Error เนื่องจาก 'To-Do' ไม่ตรงกับ title ของ browser และนอกจากนี้จะเห็นได้ว่า browser มีการเปิดและปิดลงอัตโนมัติเนื่องจากการทำงานของฟังก์ชั่น setUp และ tearDown ที่ได้กำหนดไว้


แก้ไขไฟล์ functional_tests.py
 
    [...]
    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(3)

    def tearDown(self):
    [...]
 
     เมื่อทำการรันจะพบว่าหน้าต่าง browser จะเปิดค้างไว้ 3 วินาที ซึ่งเป็นค่าที่ถูกกำหนดไว้ ก่อนจะปิดลงอัตโนมัติ

     Commit
     คำสั่ง  git diff จะแสดงความแตกต่างของสถานะของไฟล์ระหว่างการ commit ครั้งล่าสุด กับสถานะที่เป็นอยู่บนดิส




- - - - - - - - - -

ไม่มีความคิดเห็น:

แสดงความคิดเห็น