Search code examples
pythondjangocoverage.pytest-coverage

Coverage test django admin custom functions


I created a custom functions in django admin.py and im trying to test the using coverage :

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 has a django user model foreign key...So far i tried a lot of ways to test:

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)

But nothing works....any help please ?

Thanks in advance !!


Solution

  • Actually i found it, and it was quite simple if someone ever need it :

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