我正在尝试测试一个在初始化期间创建一些自定义权限的应用程序。这些是在创建数据库后立即创建的:
custom_permissions = getattr(settings,'SOMEAPP_PERMISSIONS',() )
def create_custom_permissions(sender, **kwargs):
ct, created = ContentType.objects.get_or_create(model = '',
app_label = 'some_app',
defaults = {'name':'some_app'})
for codename, name in custom_permissions:
perm = Permission.objects.get_or_create(codename = codename,
content_type__pk = ct.id,
name = name)
post_save.connect(create_custom_permissions, Permission)
我能找到的所有关于覆盖设置的讨论都与override_settings
,with self.settings
等等有关。但是当我在 TestCase 期间覆盖设置时,我的函数已经运行,并且设置的更改当然没有效果:
@override_settings(SOMEAPP_PERMISSIONS = (('some_custom_permission','No Name'))
class TestCustomPermissions(TestCase):
fixtures = ['some_app_user_fixture.json']
urls = 'some_app.tests.test_urls'
def test_create_point_privileges(self):
""" check if all necessary privileges have been created """
Permission.objects.get(codename = 'some_custom_permission')
因此我所有的测试都失败了。现在我如何使用设置文件来测试这个特殊功能并且它与数据库的创建有关?