Wednesday, July 4, 2018

Commit webapp Food Detail: make functional_tests use LiveServerTestCase ...

 

  • Detail Commit:
      ได้ทำการลบ functional_tests อันเก่าออก แล้วได้สร้าง functional_tests.py อันใหม่ โดยมีเนื้อหาแบบเดิมแต่ว่าใช้ LiveServerTestCase โดยสร้างโฟรเดอร์ functional_tests ซึ่งใช้คำสั่ง  
$ mkdir functional_tests 
$ touch functional_tests/__init__.py

      จากนั้นเราจะย้าย functional test ของเราจากไฟล์แบบ standalone (functional_tests.py) ไปเป็น test.py ในโฟรเดอร์ functional_tests ซึ่งเราใช้ git mv เพื่อให้ Git รู้ว่าเราได้ย้ายไฟล์:

$ git mv functional_tests.py functional_tests/tests.py
$ git status # shows the rename to functional_tests/tests.py and __init__.py

      จากนั้นเข้าไปใน functional_tests.py แล้วลบ unittest.TestCase ที่อยู่ใน class NewVisitorTest ออกจากนั้นใส่ LiveServerTestCase ไปใน class NewVisitorTest และเปลี่ยนจาก
self.browser.get('http://localhost:8000/fooddetail')
เป็น
self.browser.get(self.live_server_url)
สาเหตุที่เราต้องเปลี่ยนเพราะ LiveServerTestCase สร้าง test database โดยอัตโนมัติ
(เช่นเดียวกับใน unit test)

  • File: functional_tests.py

  • File: functional_tests/tests.py
  • File: fooddetail/templates/fooddetail/index.html
 
จากนั้นมาทำ unit test โดยเรา test แค่ การ return template และการแสดงชื่ออาหารบนหน้าเว็บ 

  • fooddetail/tests.py


No comments:

Post a Comment