Skip to content

pytest django SynchronousOnlyOperation #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PyB1l opened this issue Nov 20, 2020 · 3 comments
Closed

pytest django SynchronousOnlyOperation #29

PyB1l opened this issue Nov 20, 2020 · 3 comments

Comments

@PyB1l
Copy link

PyB1l commented Nov 20, 2020

I am trying the simplest scenario without pytest-playwright plugin.

import pytest
from playwright import sync_playwright

@pytest.mark.django_db
def test_p(live_server):
    playwright = sync_playwright().start()
    browser = playwright.chromium.launch()
    page = browser.newPage()
    page.goto(f"{live_server.url}")
    page.close()

And I am getting the following error

django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

@kumaraditya303
Copy link
Contributor

kumaraditya303 commented Nov 21, 2020

@PyB1l, for running Django tests with playwright you need to set environment variable
DJANGO_ALLOW_ASYNC_UNSAFE = "true" docs
This can be done by creating a conftest.py in the tests directory and setting the environment variable using

# conftest.py
import os
os.environ.setdefault("DJANGO_ALLOW_ASYNC_UNSAFE","true")

@mxschmitt
Copy link
Member

Closed because of inactivity. Feel free to reopen if the solution by @kumaraditya303 does not work.

@felipe3dfx
Copy link

I have same problem, i use pytest-asyncio and channels with Django, how i can make this works without set environment variable?
Not found docs about this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants