Skip to content

Latest commit

 

History

History
37 lines (31 loc) · 1.15 KB

File metadata and controls

37 lines (31 loc) · 1.15 KB

How to log in a test user in a pytest unit test

def test_profile_photo_update_success(tp, user, client):
    """
    POST /users/me/photo

    Confirm that user can update their profile photo 
    """
    client.force_login(user)
    res = tp.get("protected-page")
    tp.response_200(res)
  • client argument is the test client
  • use the force_login method on client with the user fixture
  • follow up with testing that your user can now access whatever protected page you're testing

Example from the pytest-django docs

def test_with_authenticated_client(client, django_user_model):
    username = "user1"
    password = "bar"
    user = django_user_model.objects.create_user(username=username, password=password)
    # Use this:
    client.force_login(user)
    # Or this:
    client.login(username=username, password=password)
    response = client.get('/private')
    assert response.content == 'Protected Area'