diff --git a/tests/__init__.py b/tests/__init__.py index df8d6b3e..676d3c6c 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,29 +1,35 @@ import inspect +import logging import platform from contextlib import asynccontextmanager +from functools import wraps -import pytest from scrapy import Request from scrapy.http.response.html import HtmlResponse from scrapy.utils.test import get_crawler +logger = logging.getLogger("scrapy-playwright-tests") + + if platform.system() == "Windows": from scrapy_playwright.handler import _WindowsAdapter - def windows_pytest_mark_asyncio(pytest_mark_asyncio): - def wrapper(*args, **kwargs): - if args and inspect.iscoroutinefunction(args[0]): + def allow_windows(test_method): + if not inspect.iscoroutinefunction(test_method): + raise RuntimeError(f"{test_method} must be an async def method") - async def method_proxy(*x): - await _WindowsAdapter.get_result(args[0](*x)) + @wraps(test_method) + async def wrapped(self, *args, **kwargs): + logger.debug("Calling _WindowsAdapter.get_result for %r", self) + await _WindowsAdapter.get_result(test_method(self, *args, **kwargs)) - return pytest_mark_asyncio(method_proxy) - return windows_pytest_mark_asyncio(pytest_mark_asyncio(*args, **kwargs)) + return wrapped - return wrapper +else: - pytest.mark.asyncio = windows_pytest_mark_asyncio(pytest.mark.asyncio) + def allow_windows(test_method): + return test_method @asynccontextmanager diff --git a/tests/tests_asyncio/test_playwright_requests.py b/tests/tests_asyncio/test_playwright_requests.py index 98441280..b1ff8924 100644 --- a/tests/tests_asyncio/test_playwright_requests.py +++ b/tests/tests_asyncio/test_playwright_requests.py @@ -18,7 +18,7 @@ from scrapy_playwright.handler import DEFAULT_CONTEXT_NAME from scrapy_playwright.page import PageMethod -from tests import make_handler, assert_correct_response +from tests import allow_windows, make_handler, assert_correct_response from tests.mockserver import MockServer, StaticMockServer @@ -41,6 +41,7 @@ def inject_fixtures(self, caplog): caplog.set_level(logging.DEBUG) self._caplog = caplog + @allow_windows async def test_basic_response(self): async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler: with StaticMockServer() as server: