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

Chapter 3 Testing a Simple Home Page with Unit Tests

ใช้ คำสั่ง $ python3 manage.py startapp lists
เพื่อสร้าง Folder ขึ้นมาใหม่ชื่อ lists และไฟล์ db.sqlite3

superlists/
     - db.sqlite3
     - functional_tests.py
     - lists
          • admin.py
          • __init__.py
          • migrations
               ->__init__.py
          • models.py
          • tests.py
          • views.py
     - manage.py
     - superlists
          • __init__.py
          • __pycache__
          • settings.py
          • urls.py
          • wsgi.py


Unit Testing in Django

ในไฟล์ lists/tests.py. ให้ใส่ Code

from django.test import TestCase


class SmokeTest(TestCase):


    def test_bad_maths(self):


        self.assertEqual(1 + 1, 3)


คำสั่ง $ python3 manage.py test




จากการทดสอบ code จะฟ้องว่า 2 ไม่เท่ากับ 3 ซึ่งทำงานได้ถูกต้อง


คำสั่ง

$ git status

$ git add lists



$ git diff  --staged



$ git commit -m "Add app for lists, with deliberately failing unit test"






Django’s MVC, URLs, and View Functions


     - ที่ root("/") จะ resolve ไปได้ functions ที่ต้องการ หรือไม่
     - เราทำให้ฟังก์ชั่น view สามารถส่งค่า HTML คืน ได้หรือไม่

ไฟล์ lists/tests.py. 

from django.core.urlresolvers import resolve

from django.test import TestCase
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) #เช็คว่า found.func เท่ากับ home_page หรือไม่

คำสั่ง $ python3 manage.py test




จากผลลัพธืที่ได้ คือ ไม่สามารถ import ชื่อ home_page ได้

ไฟล์ lists/views.py. 


from django.shortcuts import render



# Create your views here.

home_page = None



คำสั่ง
$ python3 manage.py test


จากผลลัพธ์ที่ได้ โปรแกรมได้ฟ้องว่าหา path ไม่เจอ

ไฟล์ superlists/urls.py.
จาก

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)),
)
แก้ไขเป็น

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

หลังจากนั้นให้ทดสอบรันอีกครั้ง


ผลคือ import Error เพราะ module ไม่มีชื่อ superlists.views

ไฟล์ superlists/urls.py. จาก
urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'superlists.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    # url(r'^admin/', include(admin.site.urls)),
)

แก้เป็น
urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'lists.views.home_page', name='home'),
    # url(r'^blog/', include('blog.urls')),
    # url(r'^admin/', include(admin.site.urls)),
)

จากนั้นรันอีกครั้งด้วยคำสั่ง $ python3 manage.py test


จะเห็นว่า import Error ได้หายไปแล้วแต่ตรง view ยังผิดอยู่

ไฟล์ lists/views.py.

from django.shortcuts import render

# Create your views here.
def home_page():
    pass

ใช้คำสั่ง $ python3 manage.py test


คำสั่ง
$ git diff 
$ 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>'))

คำสั่ง
$ python3 manage.py test


ผลจากการรันคือ home_page() ไม่มี arguments แต่ต้องการ 1 arguments

ไฟล์ lists/views.py

แก้ไขเป็น
def home_page(request):
    pass


ผลคือ Object ไม่มี attribute content

ไฟล์ lists/views.py
แก้ไขเป็น

from django.http import HttpResponse

# Create your views here.
def home_page(request):
    return HttpResponse()


โปรแกรมฟ้องกลับมาว่า code ไม่ถูกต้อง

ไฟล์ lists/views.py
แก้ไขเป็น

def home_page(request):
    return HttpResponse('<html>')



โปรแกรมฟ้องกลับมาว่าต้องเพิ่ม <title>To-Do lists</title> เข้าไป

ไฟล์ lists/views.py
แก้ไขเป็น

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


โปรแกรมห้องกลับมาว่าต้องปิดท้ายด้วย </html>

ไฟล์ lists/views.py
แก้ไขเป็น

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


โปรแกรมถูกต้องเสร็จสมบูรณ์

คำสั่ง $ python3 functional_tests.py


$ git diff 
$ git commit -am "Basic view now returns minimal HTML"
$ git log --oneline


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

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