询问;使用python进行测试驱动开发;第五章
第63~66页 我运行代码询问;使用python进行测试驱动开发;第五章,python,django,testing,tdd,Python,Django,Testing,Tdd,第63~66页 我运行代码 python manage.py test 错误消息是 Creating test database for alias 'default'... FF. ====================================================================== FAIL: test_home_page_can_save_a_POST_request (lists.tests.HomePageTest) ----------
python manage.py test
错误消息是
Creating test database for alias 'default'...
FF.
======================================================================
FAIL: test_home_page_can_save_a_POST_request (lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/hanminsoo/Documents/TDD_test/TDD_Test/superlists/lists/tests.py", line 33, in test_home_page_can_save_a_POST_request
self.assertEqual(response.content.strip().decode(), expected_html)
AssertionError: '<htm[381 chars] 신규 작업 아이템\n </td>\n </tr>\n [28 chars]tml>' != '<htm[381 chars] \n </td>\n </tr>\n </table[21 chars]l>\n'
======================================================================
FAIL: test_home_page_returns_correct_html (lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/hanminsoo/Documents/TDD_test/TDD_Test/superlists/lists/tests.py", line 18, in test_home_page_returns_correct_html
self.assertEqual(response.content.decode(), expected_html)
AssertionError: '<htm[381 chars] None\n </td>\n </tr>\n </t[25 chars]l>\n' != '<htm[381 chars] \n </td>\n </tr>\n </table[21 chars]l>\n'
----------------------------------------------------------------------
Ran 3 tests in 0.011s
FAILED (failures=2)
Destroying test database for alias 'default'...
test.py
from django.core.urlresolvers import resolve
from django.test import TestCase
from django.http import HttpRequest
from django.template.loader import render_to_string
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)
expected_html = render_to_string('home.html', request=request)
self.assertEqual(response.content.decode(), expected_html)
self.assertIn(b'<title>To-Do lists</title>', response.content.strip())
def test_home_page_can_save_a_POST_request(self):
request = HttpRequest()
request.method = 'POST'
request.POST['item_text'] = '신규 작업 아이템'
response = home_page(request)
self.assertIn('신규 작업 아이템', response.content.strip().decode())
expected_html = render_to_string(
'home.html',
request=request
)
self.assertEqual(response.content.strip().decode(), expected_html)
改变
'new_item_text': request.POST.get('item_text', '')
所以,我解决了一个问题
'测试\主页\返回\正确\ html'
但我找不到第二个问题
'<htm[381 chars] 신규 작업 아이템\n </td>\n </tr>\n [28 chars]tml>' != '<htm[381 chars] \n </td>\n </tr>\n </table[21 chars]l>\n'
“”!='您使用的是哪个Django版本?我使用了Django 1.9.5,python 3.5.1@AlasdairTry-lineself.assertEqual(response.content.strip().decode(),expected_html)而没有.strip()哦,我解决了我的问题。我尝试添加代码“{'new\u item\u text”:”신규 작업 아이템'}' 在“test_home_page_can_save_a_POST_request(self)”中,谢谢大家!您使用的是哪个Django版本?我使用了Django 1.9.5,python 3.5.1@AlasdairTry,lineself.assertEqual(response.content.strip().decode(),预期是html),没有.strip()哦,我解决了我的问题。我尝试添加代码“{'new_item_text:”신규 작업 아이템'}' 在“测试主页”中,您可以保存帖子请求(self),谢谢大家!
'<htm[381 chars] None\n </td>\n </tr>\n</t[25 chars]l>\n' != '<htm[381 chars] \n </td>\n </tr>\n </table[21 chars]l>\n
'new_item_text': request.POST.get('item_text')
'new_item_text': request.POST.get('item_text', '')
'<htm[381 chars] 신규 작업 아이템\n </td>\n </tr>\n [28 chars]tml>' != '<htm[381 chars] \n </td>\n </tr>\n </table[21 chars]l>\n'