【发布时间】:2015-04-30 11:02:38
【问题描述】:
我正在尝试测试管理员更改列表视图是否正常工作。这是我使用的代码:
from django.test import Client
from django.test import TestCase
class TestAdminViews(TestCase):
def test_admin(self, user = None, name = None):
client=Client()
if user is None:
password = "test"
user = User.objects.create_superuser('testadmin', 'test@test.com', password)
success=client.login(username = user.username, password = password)
self.assertTrue(success, "Login Failed!")
app="messwertdb"
mymodels=["messrun","messung"]
for model in mymodels:
response = client.get(reverse('admin:%s_%s_changelist'% (app, model)))
print reverse('admin:%s_%s_changelist'% (app, model)), response.status_code, response.content.count("exception")#test line self.assertEqual(response.status_code,200,"Response not ok!")
我通过尝试显示不存在的属性破坏了其中一个视图。因此,如果我尝试在浏览器中获取它,我会得到一个 AttributeError(如果 DEBUG=False,则会导致 500 响应)。但是,在我的测试中,我总是得到 200 响应——这意味着没有测试失败。如果我在 shell 中输入完全相同的代码,我会得到正确的错误。在测试用例中,它似乎返回一个空但其他正确的更改列表。
破碎的 admin.py:
class MessRunAdmin(admin.ModelAdmin):
list_display=('type','date','path','failure')
def failure(self, obj):
return obj.nonexisting #this does not exist
【问题讨论】:
标签: python django django-testing