0

To make manual testing easy, I want to create users when the login page gets shown.

class LalaLoginView(LoginView):
    def get(self, request, *args, **kwargs):
        self.create_users()
        return super().get(request, *args, **kwargs)

    @classmethod
    def create_users(cls):
        if not settings.DEBUG:
            return
        admin = User.objects.update_or_create(username='admin', defaults=dict(
            is_superuser=True, is_staff=True))[0]
        admin.set_password('admin')
        admin.save()

My Test:

@pytest.mark.django_db
def test_create_users_prod(settings):
    settings.DEBUG=False
    LalaLoginView.create_users()
    assert not User.objects.all().exists()

But the mocking of settings.DEBUG does not work. In create_users() DEBUG is "True".

How to mock settings.DEBUG with pytest-django?

2 Answers 2

2

You need to uses override_settings decorator:

@override_settings(DEBUG=False)
@pytest.mark.django_db
def test_create_users_prod(settings):
    LalaLoginView.create_users()
    assert not User.objects.all().exists()
Sign up to request clarification or add additional context in comments.

Comments

0

The problem was between keyboard and chair :-)

This code does work:

@pytest.mark.django_db
def test_create_users_prod(settings):
    settings.DEBUG=False
    LalaLoginView.create_users()
    assert not User.objects.all().exists()

But in my production code I imported settings from mysite.

After changing this to from django.conf import settings the pytest fixture "settings" worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.