pytest, asgiref, Selenium and database access? How to make them work?

45 views
Skip to first unread message

Alfons Eberle

unread,
Sep 6, 2023, 6:08:32 PM9/6/23
to Django users
Hey, has anyone gotten pytest to work with django-channels (or asgiref + Daphne) and Selenium in general when database access is required?

My fixtures for users and other models import just fine, but my browser and Daphne process seem to be unaware of the test database and seem unable to create them. Frontend errors out with the classic pytest django.db.utils.OperationalError: connection to server at "127.0.0.1", port 5432 failed: FATAL: database "test2" does not exist

I am using a Daphne test server implementation I found on SO + GitHub here: https://github.com/pytest-dev/pytest-django/issues/1027

Here's some sample code I have for reference. channels_live_server is from the implementation above. Doing this simply shows the yellow Django error page with the error above.
I have tried explicitly creating the database, which gets rid of the error, trying to e.g. log in via the admin but it still does not cause the database to fill with any data. It's like they are in two different worlds (threads).

All my other pytests run fine.

Any ideas? Thanks


@pytest.fixture
def authenticated_browser_staff(channels_live_server, client):
    """Return a browser instance with logged-in user session."""
    options = webdriver.ChromeOptions()
    browser_ = webdriver.Chrome(options=options)
    user_staff = User.objects.create(
        email='te...@foo.bar', 
        password=make_password('hunter42'),
        is_superuser=True
    ) # usually a fixture, but easier to share like this
    print('staff id', user_staff.id)  # works as expected

    client.force_login(user_staff)  # works and returns the session ID
    print('cookie', client.cookies[settings.SESSION_COOKIE_NAME].value)

    browser.get(channels_live_server.url + "/admin/")
    browser.add_cookie({
        'name': settings.SESSION_COOKIE_NAME,
        'value': client.cookies[settings.SESSION_COOKIE_NAME].value,
        #'expires': None,
        #'secure': False,
        #'path': '/'
    })
    # server responds with setting sessionid to empty
    browser.refresh()

    # trying to log in, since passing the cookie does not 
    # work, but errors out with user does not exist
    browser.find_element('name', 'username').send_keys(user_staff.email)
    browser.find_element('name', 'password').send_keys('hunter42')
    browser.find_element(By.CSS_SELECTOR, "input[type='submit']").click()

    assert any(c for c in browser.get_cookies() if c['name'] == settings.SESSION_COOKIE_NAME)
    return browser



David Nugent

unread,
Sep 7, 2023, 5:01:15 AM9/7/23
to Django users, Alfons Eberle
install pytest-django
use fixture @pytest.mark.db
profit
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7281f73c-dcbb-418f-acf1-dd4f5f065cc9n%40googlegroups.com.

Luciano Martins (luxu)

unread,
Sep 7, 2023, 5:11:46 AM9/7/23
to Django users
Hello, how is your postgres configured, because there is no database called test2. this is the error
Reply all
Reply to author
Forward
0 new messages