วันอังคารที่ 9 กุมภาพันธ์ พ.ศ. 2559

Test-Driven Development with Python - Chapter. 3

Testing a Simple Home Page with Unit Tests

      ทำการสร้าง app lists ด้วยคำสั่ง

 
 $ python manage.py startapp lists
   
ภาพ(ไฟล์ทั้งหมดในโฟลเดอร์)
  • Unit Testing in Django

- ทำการแก้ไขไฟล์ lists/tests.py 

 
 from django.test import TestCase
 class SmokeTest(TestCase):
     def test_bad_maths(self):
        self.assertEqual(1 + 1, 3)
 
- ทำการ test ไฟล์ tests.py
ภาพ(การ run tests.py)

     จะพบว่ามีการ Error เนื่องจาก มีการนำค่า 1+1 ซึ่งเท่ากับ 2 มาเทียบกับ 3 ซึ่งมีค่าไม่เท่ากัน
- ใช้คำสั่งต่อไปนี้ 

 
 $ git status  # should show you lists/ is untracked
 $ git add lists
 $ git diff --staged  # will show you the diff that you're about to commit
 $ git commit -m"Add app for lists, with deliberately failing unit test"
 
ภาพ(คำสั่ง git status : เพื่อดูสถานะของไฟล์)  


 ภาพ(git diff --staged : เพื่อดูสถานะการแก้ไขในแต่ละไฟล์)

ภาพ(git commit -m : เพื่อ commit เก็บสิ่งที่ได้ทำไป)

  • Django’s MVC, URLs, and View Functions
 จะทำการทดสอบ 2 อย่างคือ
  1.  ทดสอบว่าสามารถแก้ไข URL root ("/") ของเว็บไซต์ได้หรือไม่ 
  2. ทดสอบว่าสามารถทำให้ฟังก์ชัน view return HTML

 
 from django.core.urlresolvers import resolve  
 from django.test import TestCase  
 from lists.views import home_page #1  
 class HomePageTest(TestCase):  
     def test_root_url_resolves_to_home_page_view(self):  
         found = resolve('/') #2  
         self.assertEqual(found.func, home_page) #3 
    

 #1 : เป็นการ import home_page จาก lists/views.py
 #2 : resolve คือฟังก์ชันของ Django ที่ใช้ในกับ URL
 #3 : ใช้เช็ค URL root ("/") กับ home_page

 ทำการ test ไฟล์ test.py  

 
 $ python manage.py test 
    
ภาพ(การ run ไฟล์ test.py)

 จะพบว่า Error เนื่องจากไม่สามารถ import home_page ได้

  • At Last! We Actually Write Some Application Code!

     - ทำการแก้ไขไฟล์ lists/view.py

 
 from django.shortcuts import render  
 # Create your views here.  
 home_page = None 
    

     - ทำการทดสอบอีกครั้ง

ภาพ(การ run ไฟล์ test.py)
  • urls.py

Django จะเรียกใช้ไฟล์ urls.py เพื่อหา URL ที่นำมาใช้กับฟังก์ชั่น view จากไฟล์ superlists/urls.py

 
 from django.conf.urls import url
 from django.contrib import admin
 urlpatterns = [
     url(r'^admin/', admin.site.urls),
 ]
    

     - แก้เป็น

 
 from django.conf.urls import url
 from django.contrib import admin
 urlpatterns = [
     url(r'^$', 'superlists.views.home', name='home'),
     url(r'^admin/', admin.site.urls),
 ]
    

      - ทำการทดสอบไฟล์ test
ภาพ(การ run ไฟล์ test.py)

จะพบว่าไม่สามารถ import superlists.view.home ได้ เนื่องจากไม่มี module superlists.views

     - ทำการแก้ไขไฟล์ superlists/urls.py

 
 from django.conf.urls import url
 from django.contrib import admin
 urlpatterns = [
     url(r'^$', 'superlists.views.home', name='home'),
     url(r'^admin/', admin.site.urls),
 ]
    

     - แก้เป็น

 
 from django.conf.urls import url
 from django.contrib import admin
 urlpatterns = [
     url(r'^$', 'lists.views.home_page', name='home'),
     url(r'^admin/', admin.site.urls),
 ]
    

     - ทำการทดสอบไฟล์ test อีกครั้ง 

ภาพ(การ run ไฟล์ test.py)

จะพบว่าไม่สามารถ import ไฟล์ lists.vies.home_page ได้

     - แก้ไขไฟล์ lists/views.py โดยกำหนดฟังก์ชั่น home_page ขึ้นมา

 
 from django.shortcuts import render  
 # Create your views here.  
 def home_page():  
    pass 
    
ภาพ(การ run ไฟล์ test.py)

     - ทำการ git commit

   
 $ git diff # should show changes to urls.py, tests.py, and views.py  
 $ git commit -am "First unit test and url mapping, dummy view"  
    
  •  Unit Testing a View

      - แก้ไขไฟล์ lists/tests.py 

   
 from django.core.urlresolvers import resolve  
 from django.test import TestCase  
 from django.http import HttpRequest  
 from lists.views import home_page  
 class HomePageTest(TestCase):  
   def test_root_url_resolves_to_home_page_view(self):  
     found = resolve('/')  
     self.assertEqual(found.func, home_page) 

   def test_home_page_returns_correct_html(self):  
     request = HttpRequest() 
     response = home_page(request)   
     self.assertTrue(response.content.startswith(b'<html>')) 
     self.assertIn(b'<title>To-Do lists</title>', response.content)  
     self.assertTrue(response.content.endswith(b'</html>'))   
    

      - ทำการทดสอบไฟล์ test

   
 $ python manage.py test  
 

ภาพ(การ run ไฟล์ test.py)  
ทำการทดสอบไฟล์ test จะพบว่า Error

  • The Unit-Test/Code Cycle

    - แก้ไขไฟล์ lists/views.py

   
 def home_page(request):  
    pass  
ภาพ(การ run ไฟล์ test.py 
ทำการทดสอบไฟล์ test จะพบว่า Error เนื่องจาก object ไม่มี attribute 'content'

     - แก้ไขไฟล์ lists/views.py

   
 from django.http import HttpResponse  
 # Create your views here.  
 def home_page(request):  
     return HttpResponse() 

ภาพ(การ run ไฟล์ test.py   
ทำการทดสอบไฟล์ test จะพบว่า Error เนื่องจากไม่พบ <title>To-Do lists</title>ใน<html>

     - แก้ไขไฟล์ lists/views.py  โดยเพิ่ม <title>To-Do lists</title>

   
 def home_page(request):  
    return HttpResponse('<html><title>To-Do lists</title>')
      

ภาพ(การ run ไฟล์ test.py  
ทำการทดสอบไฟล์ test จะพบว่า Error เนื่องจากไม่มีการปิด tag </html>
      
      - แก้ไขไฟล์ lists/views.py  โดยเพิ่ม </html>

   
 def home_page(request):  
    return HttpResponse('<html><title>To-Do lists</title></html>') 
      
ภาพ(การ run ไฟล์ test.py)  

     - ทำการทดสอบไฟล์ functional_tests.py เพื่อทดสอบการทำงานของโปรแกรมจะแสดงผล  คือ เสร็จสิ้นการทดสอบ 
ภาพ(การ run ไฟล์ functional_tests.py

     - ใช้คำสั่ง git commit

  
 $ git diff # should show our new test in tests.py, and the view in views.py  
 $ git commit -am"Basic view now returns minimal HTML" 
      
ภาพ(การใช่คำสั่ง git commitgit commit)     




----------

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

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