μ¬μ©μ κ΄μ μμ μ ν리μΌμ΄μ μΈλΆλ₯Ό ν μ€νΈνλ κ² μμ λ 벨μ κ°λ° μ£Όλ
νλ‘κ·Έλλ¨Έ κ΄μ μμ μ ν리μΌμ΄μ λ΄λΆλ₯Ό ν μ€νΈνλ κ² νμ λ 벨μ κ°λ° μ£Όλ
startapp
μ ν΅ν΄ μ±μ μμ±νκ² λλ©΄ νμμ tests.py
λΌλ νμΌμ΄ μκΈ΄λ€.
ν΄λΉ νμΌμ λ΄μ©μ λ€μκ³Ό κ°λ€.
from django.test import TestCase
# Create your tests here.
Djangoμ TestCase
λ unittest.TestCase
μ νμ₯ λ²μ μΌλ‘ Djangoμ λν νΉν κΈ°λ₯λ€μ΄ μΆκ°λμ΄ μλ€κ³ νλ€.
κ°λ¨ν ν
μ€νΈλ₯Ό λ§λ€μ΄ μ€νν΄λ³΄μ.
from django.test import TestCase
class SmokeTest(TestCase):
def test_bad_maths(self):
self.assertEqual(1 + 1, 3)
μ€ν¨ν μ λ°μ μλ ν μ€νΈμ΄μ§λ§ μ°μ μ€νν΄λ³΄μ.
python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_bad_maths (superlists.lists.tests.SmokeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/yugyeong/Study/TDD4CleanCode/superlists/lists/tests.py", line 6, in test_bad_maths
self.assertEqual(1+1, 3)
AssertionError: 2 != 3
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Destroying test database for alias 'default'...
μ λλ‘ λμνλ€.
System check identified no issues (0 silenced).
EE
======================================================================
ERROR: superlists.lists (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: superlists.lists
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
package = self._get_module_from_name(name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
ModuleNotFoundError: No module named 'superlists.lists'
νΉμ μ°Έκ³ μ€μΈ λμμ λ΄μ©μ κ·Έλλ‘ λ°λΌνλ€κ° ν μμ μμ μμ κ°μ μλ¬κ° λμ λΆμ΄ μλ€λ©΄ νλ‘μ νΈ λ£¨νΈ λλ ν 리 μλμ superlists
ν΄λμ μ΄λ¦μ μμλ‘ λ³κ²½ν΄μΌ νλ€.
λ£¨νΈ λλ ν 리 μ΄λ¦κ³Ό λμΌν΄μ ImportError
κ° λλ κ²μ΄λ€.
νμ§λ§ λ μ΄λΆλΆμ μμ νλ μλ²λ₯Ό μΌ€ λ λ¬Έμ κ° μλ€β¦ μ΄ λΆλΆμ λ κ³ λ―Όν΄λ΄μΌν κ² κ°λ€.
Djangoμ κΈ°λ³Έμ μΈ μ²λ¦¬ νλ¦μ λ€μκ³Ό κ°λ€.
λ°λΌμ ν μ€νΈν΄μΌ ν κ²μ λ κ°μ§μ΄λ€.
/
)λ₯Ό ν΄μν΄μ νΉμ λ·° κΈ°λ₯μ 맀μΉμν¬ μ μλκ°?μμ ν μ€νΈ μ½λλ₯Ό λ€μκ³Ό κ°μ΄ μμ νμ.
from django.test import TestCase
from django.urls import resolve
from .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)
resolve
λ Djangoμ λ΄λΆ ν¨μλ‘ URLμ ν΄μν΄μ μΌμΉνλ λ·° ν¨μλ₯Ό μ°Ύλλ€.
μ¬κΈ°μλ /
κ° νΈμΆλ λ resolve
λ₯Ό μ€νν΄μ home_page
λΌλ ν¨μλ₯Ό νΈμΆνλ€.
home_page
λ 곧 μμ±ν λ·° ν¨μλ‘ HTMLμ λ°ννλ€.
ν μ€νΈλ₯Ό μ€νν΄λ³΄μ.
System check identified no issues (0 silenced).
E
======================================================================
ERROR: superlists.lists.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: superlists.lists.tests
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/Users/yugyeong/Study/TDD4CleanCode/superlists/lists/tests.py", line 3, in <module>
from .views import home_page
ImportError: cannot import name 'home_page'
첫 λ²μ§Έ μλ¬λ‘ home_page
λ₯Ό import ν μ μλ€κ³ λ¬λ€.
κ·Έλ λ€λ©΄ home_page
ν¨μλ₯Ό μμ±ν΄μ£Όμ΄μΌ νλ€.
μ°μ views.py
μ home_page
λ₯Ό μ μΈν΄μ£Όμ.
from django.shortcuts import render
home_page = None
κ·Έλ¦¬κ³ λ€μ ν μ€νΈλ₯Ό μ€νν΄λ³΄μ.
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_root_url_resolves_to_home_page_view (superlists.lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/yugyeong/Study/TDD4CleanCode/superlists/lists/tests.py", line 8, in test_root_url_resolves_to_home_page_view
found = resolve('/')
File "/Users/yugyeong/Study/TDD4CleanCode/venv/lib/python3.6/site-packages/django/urls/base.py", line 24, in resolve
return get_resolver(urlconf).resolve(path)
File "/Users/yugyeong/Study/TDD4CleanCode/venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 566, in resolve
raise Resolver404({'tried': tried, 'path': new_path})
django.urls.exceptions.Resolver404: {'tried': [[<URLResolver <URLPattern list> (admin:admin) 'admin/'>]], 'path': ''}
----------------------------------------------------------------------
Ran 1 test in 0.005s
FAILED (errors=1)
Destroying test database for alias 'default'...
μλ¬ λ΄μ©μ΄ λ°λμλ€.
μ΄λ²μλ URL κ΄λ ¨ μλ¬λ€.
μ°λ¦¬κ° μ°ΎμΌλ €λ /
μ ν΄λΉνλ URL 맀νμ μ°Ύμ μ μμ΄μ 404 μλ¬κ° λ°μν κ±Έλ‘ λ³΄μΈλ€.
urls.py
νμΌμ μ΄μ΄μ URL ν¨ν΄μ μΆκ°ν΄μ£Όμ.
from django.urls import path, include
from superlists.lists.views import *
urlpatterns = [
path('', home_page, name='home'),
]
λ€μ ν μ€νΈν΄λ³΄μ.
(μλ΅)
TypeError: view must be a callable or a list/tuple in the case of include().
view
λ₯Ό νΈμΆν μ μλ€λ λ©μμ§κ° λ¨κ³ μλ€.
home_page
κ° μμ§ ν¨μκ° μλκΈ° λλ¬Έμ΄λ€.
μ΄μ μ€μ ν¨μλ‘ λ³κ²½ν΄λ³΄λλ‘ νμ.
λ€μ views.py
λ‘ λμκ° home_page
λ₯Ό ν¨μμ ννλ‘ λ°κΎΌλ€.
def home_page():
pass
λ€μ ν μ€νΈν΄λ³΄μ.
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Destroying test database for alias 'default'...
λλμ΄ μ²« λ¨μ ν μ€νΈκ° μ±κ³΅νλ€.
λ·°λ₯Ό μν ν
μ€νΈλ₯Ό μμ±ν λλ λΉ ν¨μλ₯Ό μμ±νλ κ²μ΄ μλλΌ HTML νμμ μ€μ μλ΅μ λ°ννλ ν¨μλ₯Ό μμ±ν΄μΌ νλ€.
test.py
λ₯Ό μ΄μ΄ λ€μκ³Ό κ°μ΄ μλ‘μ΄ ν
μ€νΈλ₯Ό μΆκ°νμ.
from django.http import HttpRequest
from django.test import TestCase
from django.urls import resolve
from .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_to_home_page_view(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>'))
HttpResponse
κ°μ²΄λ₯Ό μμ±ν΄μ μ¬μ©μκ° μ΄λ€ μμ²μ λΈλΌμ°μ μ 보λ΄λμ§ νμΈνλ€.
μ΄κ²μ home_page
λ·°μ μ λ¬ν΄μ μλ΅μ μ·¨λνλ€. μ΄ κ°μ²΄λ HttpResponse
λΌλ ν΄λμ€μ μΈμ€ν΄μ€λ€.
μλ΅ λ΄μ©μ΄ νΉμ μμ±μ κ°μ§κ³ μλμ§ νμΈνλ€.
κ·Έ λ€μμ μλ΅ λ΄μ©μ΄ <html>
λ‘ μμνκ³ </html>
λ‘ λλλμ§ νμΈνλ€.
response.content
λ byteν λ°μ΄ν°λ‘ νμ΄μ¬ λ¬Έμμ΄μ΄ μλλ€.
λ°λΌμ b'
ꡬ문μ μ¬μ©ν΄μ λΉκ΅νλ€.
λ§μ§λ§μΌλ‘ λ°ν λ΄μ©μ <title>
νκ·Έμ To-Do listsλΌλ λ¨μ΄κ° μλμ§ νμΈνλ€.
ν μ€νΈλ₯Ό μ€νν΄λ³΄μ.
(μλ΅)
TypeError: home_page() takes 0 positional arguments but 1 was given
μ¬κΈ°μλΆν° TDD λ¨μ ν μ€νΈ - μ½λ μ£ΌκΈ°μ λν΄ μκ°ν΄μΌ νλ€.
κ·Έλ¦¬κ³ μ΄κ²μ λ°λ³΅νλ€.
μΌλ§λ 빨리 μ΄ μ£ΌκΈ°λ₯Ό λ°λΌκ° μ μλμ§ νμΈν΄λ³΄μ.
def home_page(request):
pass
AttributeError: 'NoneType' object has no attribute 'content'
django.http.httpResponse
λ₯Ό μ¬μ©νλ€.from django.http import HttpResponse
def home_page(request):
return HttpResponse
AttributeError: 'property' object has no attribute 'startswith'
def home_page(request):
return HttpResponse('<html><title>To-Do lists</title>')
AssertionError: False is not true
def home_page(request):
return HttpResponse('<html><title>To-Do lists</title></html>')
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 0.007s
OK
Destroying test database for alias 'default'...