【问题标题】:Coverage test django admin custom functions覆盖测试 django admin 自定义函数
【发布时间】:2016-11-21 16:11:13
【问题描述】:

我在 django admin.py 中创建了一个自定义函数,并尝试测试使用覆盖率:

class Responsible(admin.ModelAdmin):

   """
    Inherits of admin class
  """

   list_display = ('user', 'Name', 'last_name', 'process',)
   search_fields = ('p__name', 'user__username')

   def User_Name(self, obj):
       return obj.user.first_name

   def User_Last_Name(self, obj):
       return obj.user.last_name

Responsible model 有一个 django user model 外键...到目前为止我尝试了很多方法来测试:

class AdminTestCase(TestCase):

    fixtures = ["initial_data.json"]


    def test_first_name(self):
        rsf = Responsible.objects.get(id = 1)
        User_Name(rsf)

    def test_first_name2(self):
        self.obj = Responsible.objects.get(id = 1)

但是没有任何效果....有什么帮助吗?

提前致谢!!

【问题讨论】:

    标签: python django coverage.py test-coverage


    【解决方案1】:

    您必须使用 django 客户端并在 django admin 中打开 Responsible 模型的列表页面。

    https://docs.djangoproject.com/en/1.10/topics/testing/tools/#overview-and-a-quick-example

    通过打开管理列表页面,您的自定义函数将被调用,因此将被测试覆盖。

    所以基本上需要做如下的事情:

    from django.test import Client, TestCase
    
    class BaseTestCase(TestCase):
        """Base TestCase with utilites to create user and login client."""
    
        def setUp(self):
            """Class setup."""
            self.client = Client()
            self.index_url = '/'
            self.login()
    
        def create_user(self):
            """Create user and returns username, password tuple."""
            username, password = 'admin', 'test'
            user = User.objects.get_or_create(
                username=username,
                email='admin@test.com',
                is_superuser=True
            )[0]
            user.set_password(password)
            user.save()
            self.user = user
            return (username, password)
    
        def login(self):
            """Log in client session."""
            username, password = self.create_user()
            self.client.login(username=username, password=password)
    
    
    class AdminTestCase(BaseTestCase):
    
        def test_responsible_list(self):
            response = self.client.get('/admin/responsilbe_list/')
            # assertions....
    

    【讨论】:

    • 感谢您的回答,从来没有这样做过...您能否在您的回答中为第一个名为“User_Name”的函数举例说明?
    • 更新答案。
    • 感谢您的帮助,但我认为这不是我所需要的,我已经测试了登录和注销...我需要的是测试我发布的那些功能
    • def test_responsible_list(self): 将对其进行测试。通过在管理员中为您的模型Responsible 打开列表页面,由self.client.get('/admin/responsilbe_list/') 完成,最终将调用您的管理员自定义方法。
    • 我找到了另一种方式,无论如何我都投了你的票以寻求你的帮助和指导。谢谢!!
    【解决方案2】:

    其实我找到了,如果有人需要的话,这很简单:

    def test_first_name_admin(self):
        rsf = ResponsibleStateFlow.objects.get(id = 1)
        ResponsibleStateFlowAdmin.User_Name(self, rsf)
        ResponsibleStateFlowAdmin.User_Last_Name(self, rsf)
    

    【讨论】:

      【解决方案3】:

      完整、快速、简单:

      from [your-app].admin.py import Responsible  # better name it ResponsibleAdmin
      
      class AdminTestCase(TestCase):
      
      fixtures = ["initial_data.json"]
      
      
      def test_first_name(self):
          r = Responsible.objects.get(id = 1)
          admin_function_result = Responsible.User_Name(Responsible, obj=r)
          self.assertEquals(admin_function_result, r.user.first_name)  # alternatively, pass first_name as string 
      

      无需使用管理员登录。 将普通函数作为普通函数进行测试。

      【讨论】:

        【解决方案4】:

        自定义函数需要一个从视图的查询集派生的对象。这应该可以。

        from django.contrib import admin
        
        from myapp.admin import ResponsibleAdmin  # renamed to distinguish from model class
        
        class AdminTestCase(TestCase):
        
            fixtures = ["initial_data.json"]  # as in the question
        
            def test_first_name(self):
                r = Responsible.objects.get(id=1)
                r_admin = ResponsibleAdmin(Responsible, admin.site)
                obj = r_admin.get_object(None, r.id)
                admin_first_name = r_admin.User_Name(obj)
                self.assertEquals(admin_first_name, r.user.first_name) 
        

        【讨论】:

          猜你喜欢
          • 2020-05-26
          • 2010-11-20
          • 2011-03-29
          • 2019-05-07
          • 2017-05-26
          • 2020-07-30
          • 2018-10-23
          • 2019-08-15
          • 2017-10-14
          相关资源
          最近更新 更多