Skip to content

feat: add compatibility check between pytest-playwright and pytest-playwright-asyncio #282

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def _pw_artifacts_folder() -> Generator[tempfile.TemporaryDirectory, None, None]
pass


@pytest.fixture(scope="session", autouse=True)
def _check_sync_async_incompatibility(pytestconfig: Any) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd instead prefer putting it into the pytest_addoption fixture - wdyt? That should work as well and seems very early on when the plugin gets loaded.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your call m8, just LMK and I'll try to update the PR when I can find some time.

if pytestconfig.pluginmanager.hasplugin("pytest-playwright"):
raise RuntimeError(
"pytest-playwright and pytest-playwright-asyncio are not compatible. Please use only one of them."
)


@pytest.fixture(scope="session", autouse=True)
def delete_output_dir(pytestconfig: Any) -> None:
output_dir = pytestconfig.getoption("--output")
Expand Down Expand Up @@ -282,7 +290,7 @@ async def launch(**kwargs: Dict) -> Browser:

@pytest_asyncio.fixture(scope="session")
async def browser(
launch_browser: Callable[[], Awaitable[Browser]]
launch_browser: Callable[[], Awaitable[Browser]],
) -> AsyncGenerator[Browser, None]:
browser = await launch_browser()
yield browser
Expand Down
8 changes: 8 additions & 0 deletions pytest-playwright/pytest_playwright/pytest_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def _pw_artifacts_folder() -> Generator[tempfile.TemporaryDirectory, None, None]
pass


@pytest.fixture(scope="session", autouse=True)
def _check_sync_async_incompatibility(pytestconfig: Any) -> None:
if pytestconfig.pluginmanager.hasplugin("pytest-playwright-asyncio"):
raise RuntimeError(
"pytest-playwright and pytest-playwright-asyncio are not compatible. Please use only one of them."
)


@pytest.fixture(scope="session", autouse=True)
def delete_output_dir(pytestconfig: Any) -> None:
output_dir = pytestconfig.getoption("--output")
Expand Down
34 changes: 32 additions & 2 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def pytester(pytester: pytest.Pytester) -> pytest.Pytester:


@pytest.fixture(autouse=True)
def _add_async_marker(testdir: pytest.Testdir) -> None:
def _add_ini_asyncio(request: pytest.FixtureRequest, testdir: pytest.Testdir) -> None:
if "no_add_ini" in request.keywords:
return
testdir.makefile(
".ini",
pytest="""
Expand All @@ -42,6 +44,34 @@ def _add_async_marker(testdir: pytest.Testdir) -> None:
)


@pytest.mark.no_add_ini
def test_sync_async_incompatibility(testdir: pytest.Testdir) -> None:
# Remove `-p no:playwright` from pytest addopts
testdir.makefile(
".ini",
pytest="""
[pytest]
addopts = --maxfail=1
asyncio_default_test_loop_scope = session
asyncio_default_fixture_loop_scope = session
""",
)
testdir.makepyfile(
"""
import pytest
@pytest.mark.asyncio
async def test_foo():
pass
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=0, errors=1)
assert (
"pytest-playwright and pytest-playwright-asyncio are not compatible. Please use only one of them."
in "\n".join(result.outlines)
)


def test_default(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
Expand Down Expand Up @@ -235,7 +265,7 @@ async def test_is_firefox(page, browser_name, is_chromium, is_firefox, is_webkit
assert is_chromium is False
assert is_firefox
assert is_webkit is False
"""
"""
)
result = testdir.runpytest("--browser", "firefox")
result.assert_outcomes(passed=1)
Expand Down
30 changes: 29 additions & 1 deletion tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def pytester(pytester: pytest.Pytester) -> pytest.Pytester:


@pytest.fixture(autouse=True)
def _add_ini(testdir: pytest.Testdir) -> None:
def _add_ini(request: pytest.FixtureRequest, testdir: pytest.Testdir) -> None:
if "no_add_ini" in request.keywords:
return
testdir.makefile(
".ini",
pytest="""
Expand All @@ -41,6 +43,32 @@ def _add_ini(testdir: pytest.Testdir) -> None:
)


@pytest.mark.no_add_ini
def test_sync_async_incompatibility(testdir: pytest.Testdir) -> None:
# Remove `-p no:playwright-asyncio` from pytest addopts
testdir.makefile(
".ini",
pytest="""
[pytest]
addopts = --maxfail=1
""",
)
testdir.makepyfile(
"""
import pytest

def test_foo():
pass
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=0, errors=1)
assert (
"pytest-playwright and pytest-playwright-asyncio are not compatible. Please use only one of them."
in "\n".join(result.outlines)
)


def test_default(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
Expand Down
Loading