หน้าเว็บ

วันพุธที่ 28 มกราคม พ.ศ. 2558

CHAPTER 3 Testing a Simple Home Page with Unit Tests



" apps are good way to keep your code organised."

Our first Django app and first unit test
Let's start an app for our to-do liste:
  -- ไปที่ directory ที่จัดเก็บ manage.py
  -- run code : python3 manage.py startapp lists เพื่อสร้าง app lists
  -- create a folder at superlists/lists, next to superlists/superlists
[จะสร้าง folder lists ขึ้นมา]
   -- in folder lists : admin.py __init__.py migrations models.py tests.py views.py
   -- in folder superlists :__init__.py  __pycache__  settings.py  urls.py  wsgi.py

Unit Tests, and How to They Differ From Functional Test
"Functional tests should help you build an application with the right functionality, and guarantee you never accidentally break it. Unit tests should help you to write code that’s clean and bug free."

::functional test : ป็นการทดสอบในมุมมองของ user ทดสอบตามเรื่องราว รู้แค่โครงสร้างผลลัพธ์ที่แสดงออกมาแต่ไม่รู้การทำงานข้างในของโค้ด
::Unit test : ทดสอบในมุมมองของ programmer การทดสอบใช้ได้จริงหรือป่าวเห็นการทำงานข้างในของโค้ด

Django workflow
1. request เข้ามาจาก client ต้องการนู้นนี่ (ในที่นี้คือ url) :resolving the url
2. request ที่เข้ามานี้ให้ฟังก์ชันไหนจัดการ ตามกฎที่ระบุไว้ : rules ระบุใน url
3. เรียกใช้ฟังก์ชันจัดการ process จากนั้นส่งค่ากลับ :ฟังก์ชันอยู่ใน view function

Testing
  • Test request ที่เข้ามาจะ resolve ไปยังฟังก์ชันที่ออกแบบไว้หรือไม่ เป็นการทดสอบถูกฟังก์ชัน  ในที่นี้คือ test_root_url_resolve_to_home_page_view.
  • Test ฟังก์ชันทำงานอย่างที่ออกแบบไว้หรือไม่
    ในที่นี้คือ test_home_page_returns_correct_html

Unit Testing in Djiango
git status
git add lists
git diff --staged




เปิด urls.py แล้วแก้ไข
code :
from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
   # Examples:
   url(r'^$', 'superlists.views.home', name='home'),
   # url(r'^blog/', include('blog.urls')),

   # url(r'^admin/', include(admin.site.urls)),
)

run:  python3 manage.py test




ใน urls.py แก้ไข 
code :
urlpatterns = patterns('',
   # Examples:
    url(r'^$', 'lists.views.home_page', name='home'),





ใน view.py : home_page = None แก้ไข
code: def home_page():
            pass
run:  python3 manage.py test


code : 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
code :
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() #1
      response = home_page(request) #2
      self.assertTrue(response.content.startswith(b'')) #5')) #3
self.assertIn(b'To-Do lists', response.content) #4
self.assertTrue(response.content.endswith(b'

The Unit-Test/code cycle
แก้ไขโค้ดที่ lists/view.py ตาม error ที่ขึ้น
code:
def home_page(request):
   return HttpResponse('')To-Do lists


 run:  python3 manage.py test


run:  python3 functional_tests.py test



-----------------------------------------------------------------------------------------------------
learn : http://chimera.labs.oreilly.com/books/1234000000754/ch03.html#_our_first_django_app_and_our_first_unit_test

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

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