覆盖率测试django admin自定义函数
Coverage test django admin custom functions
我在 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)
但没有任何效果....请帮忙?
提前致谢!!
您将必须使用 django 客户端并在 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....
实际上我找到了它,如果有人需要它,这很简单:
def test_first_name_admin(self):
rsf = ResponsibleStateFlow.objects.get(id = 1)
ResponsibleStateFlowAdmin.User_Name(self, rsf)
ResponsibleStateFlowAdmin.User_Last_Name(self, rsf)
完整、快速、简单:
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
无需使用管理员登录。
将普通函数作为普通函数进行测试。
自定义函数需要一个派生自视图查询集的对象。这应该有效。
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)
我在 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)
但没有任何效果....请帮忙?
提前致谢!!
您将必须使用 django 客户端并在 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....
实际上我找到了它,如果有人需要它,这很简单:
def test_first_name_admin(self):
rsf = ResponsibleStateFlow.objects.get(id = 1)
ResponsibleStateFlowAdmin.User_Name(self, rsf)
ResponsibleStateFlowAdmin.User_Last_Name(self, rsf)
完整、快速、简单:
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
无需使用管理员登录。 将普通函数作为普通函数进行测试。
自定义函数需要一个派生自视图查询集的对象。这应该有效。
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)