วันพุธที่ 10 กุมภาพันธ์ พ.ศ. 2559

Test-Driven Development with Python Chapter 2

ใช้ Functional Test เพื่อระบุสิ่งที่แอปของเราอย่างน้อยต้องทำได้

Functional Test คือ การทดสอบโดยมุมมองของ user เกี่ยวกับการทำงานของแอปเรา โดยที่ไม่สนใจระบบภายใน ซึ่งเราต้องระบุว่า user น่าจะต้องทำอะไรกับแอปของเรา และแอปจะต้องตอบสนองยังไง

แก้ไขไฟล์ 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()


จากนั้น run server และรัน functional_test.py


จะขึ้น AssertionError มา ซึ่งเป็นสิ่งที่เราคาดไว้ เพราะเรายังไม่ได้เขียนโค้ดเพื่อทำให้เทสผ่าน ถ้าไม่ Error สิแปลก

Library ที่ช่วยในการ test ที่ชื่อว่า unittest

from selenium import webdriver
import unittest


class NewVisitorTest(unittest.TestCase): #


  def setUp(self): #
    self.browser = webdriver.Firefox()
  def tearDown(self): #
    self.browser.quit()
  def test_can_start_a_list_and_retrieve_it_later(self): #
    # 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) #
    self.fail('Finish the test!') #
    # She is invited to enter a to-do item straight away


if __name__ == '__main__': #
    unittest.main(warnings='ignore')


จากนั้นลองรันเทส


และก็อย่าลืม commit

TDD Concept ที่เป็นประโยชน์
- User Story คือการอธิบายว่าแอพลิเคชั่นจะทำงานยังไงในมุมมองของ user ซึ่งใช้เพื่อสร้าง functional test
- Expected Failure คือการเทสไม่ผ่านในทางที่เราคาดหวังจะให้มันเกิด

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

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