我正在尝试运行 Django 测试,但遇到了此错误 -
django.template.exceptions.TemplateSyntaxError: 'socialaccount' is not a registered tag library.
,但通常它必须工作,因为我在运行测试时使用自定义设置。
我有2种身份验证方法:默认Django的身份验证和allauth的“使用谷歌登录”。我只想测试 Django 的默认身份验证而不是 allauth 登录。为此,我正在使用
tests_settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'widget_tweaks', # I'm using here only needed apps
'apps.userauth',
'apps.spacefy',
]
IS_TEST_CONFIG = True # I just override existing settings.py
这是我的
view
我想测试:
class CustomLoginView(LoginView):
form_class = CustomAuthenticationForm
redirect_authenticated_user = True
template_name = 'apps.userauth/login.html'
success_url = "home"
def get_context_data(self, **kwargs):
context = super(CustomLoginView, self).get_context_data(**kwargs)
context['is_test'] = settings.IS_TEST_CONFIG
def form_invalid(self, form):
messages.error(self.request, message=(
"Please enter a correct email and password.Note that both fields may be "
"case-sensitive. (You can also try to login with google)"))
return self.render_to_response(self.get_context_data(form=form))
其实是我的
tests:
:
class TestCustomLoginView(TestCase):
def setUp(self):
CustomUser.objects.create_user(
email='[email protected]',
password='<PASSWORD>'
)
self.email = "[email protected]"
def test_form_invalid(self):
response = self.client.post(path='/auth/login/', data={
'email': self.email,
'password': 'invalid_password'
})
self.assert ...
我还在模板中使用 if 语句来防止像我遇到的错误(但它不起作用:( ):
{% if not is_test %}{% load socialaccount %}{% endif %}
Finally
我用这个运行我的测试command
-
python3 manage.py test tests.userauth-tests.test_views --settings=tests.tests_settings
你能帮我吗?
如果您正在使用
allauth's "Login with google"
,它应该位于已安装的应用程序设置中。
'allauth',
'allauth.account',
'allauth.socialaccount.providers.google',
参考:- 文档