【问题标题】:admin view test not failing管理员视图测试没有失败
【发布时间】: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


    【解决方案1】:

    这是一个简单的错误:我忘记加载夹具,所以测试数据库是空的,因为它没有尝试显示任何实例,所以它没有调用失败的属性。在 shell 中,它使用的是非空的部署数据库。

    所以我把它改成了下面的代码,现在失败了:

    class TestAdminViews(TestCase):
        fixtures= ["messwertdb.yaml"]
        def test_admin(self, user = None, name = None):
            .......
    

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 2014-05-11
      • 2020-02-29
      • 2013-04-05
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      相关资源
      最近更新 更多