0
我试图测试管理更改列表视图,如果他们正常工作。这是我使用的代码:管理视图测试未失败
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', '[email protected]', 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