python Web开发:测试驱动方法---3.3.django中的单元测试,3.4MVC、URL和视图函数:
tests.py测试类中写的第一个单元测试函数,如下:
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
#print "befor exct found"
found = resolve('/')
#print "befor exct found:",found
self.assertEqual(found.func, home_page)
执行后报404错误
Traceback (most recent call last):
File "E:\djangoPreject\HelloWorld\cmdb\tests.py", line 11, in test_root_url_resolves_to_home_page_view
found = resolve('/')
File "C:\Python27\lib\site-packages\django\urls\base.py", line 27, in resolve
return get_resolver(urlconf).resolve(path)
File "C:\Python27\lib\site-packages\django\urls\resolvers.py", line 394, in resolve
raise Resolver404({'tried': tried, 'path': new_path})
Resolver404: {u'path': u'', u'tried': [[<RegexURLPattern None ^home_page/>]]}
问题是

在学习《Python Web开发:测试驱动方法》时遇到3.3.django单元测试部分的问题,测试root_url使用resolve('/')导致404错误。错误源于对resolve函数理解不准确,实际应检查'/home_page/'路径对应视图。修正后,测试通过。
最低0.47元/天 解锁文章
25万+

被折叠的 条评论
为什么被折叠?



