覆盖测试django管理员自定义功能

时间:2016-11-21 16:11:14

标签: python django coverage.py test-coverage

我在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

责任模型有一个django用户模型外键...到目前为止我尝试了很多方法来测试:

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)

但没有任何作用......请帮忙吗?

提前致谢!!

4 个答案:

答案 0 :(得分:1)

您必须在django admin中使用django客户端并打开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....

答案 1 :(得分:0)

实际上我找到了它,如果有人需要它,这很简单:

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

答案 2 :(得分:0)

完整,快速,简单:

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 

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

答案 3 :(得分:0)

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

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)