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