Skip to content

Commit 4674ba8

Browse files
committed
Decorator to adapt tests for Windows
1 parent 8b6414d commit 4674ba8

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

tests/__init__.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
import inspect
2+
import logging
23
import platform
34
from contextlib import asynccontextmanager
5+
from functools import wraps
46

5-
import pytest
67
from scrapy import Request
78
from scrapy.http.response.html import HtmlResponse
89
from scrapy.utils.test import get_crawler
910

1011

12+
logger = logging.getLogger("scrapy-playwright-tests")
13+
14+
1115
if platform.system() == "Windows":
1216
from scrapy_playwright.handler import _WindowsAdapter
1317

14-
def windows_pytest_mark_asyncio(pytest_mark_asyncio):
15-
def wrapper(*args, **kwargs):
16-
if args and inspect.iscoroutinefunction(args[0]):
18+
def allow_windows(test_method):
19+
if not inspect.iscoroutinefunction(test_method):
20+
raise RuntimeError(f"{test_method} must be an async def method")
1721

18-
async def method_proxy(*x):
19-
await _WindowsAdapter.get_result(args[0](*x))
22+
@wraps(test_method)
23+
async def wrapped(self, *args, **kwargs):
24+
logger.debug("Calling _WindowsAdapter.get_result for %r", self)
25+
await _WindowsAdapter.get_result(test_method(self, *args, **kwargs))
2026

21-
return pytest_mark_asyncio(method_proxy)
22-
return windows_pytest_mark_asyncio(pytest_mark_asyncio(*args, **kwargs))
27+
return wrapped
2328

24-
return wrapper
29+
else:
2530

26-
pytest.mark.asyncio = windows_pytest_mark_asyncio(pytest.mark.asyncio)
31+
def allow_windows(test_method):
32+
return test_method
2733

2834

2935
@asynccontextmanager

tests/tests_asyncio/test_playwright_requests.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from scrapy_playwright.handler import DEFAULT_CONTEXT_NAME
1919
from scrapy_playwright.page import PageMethod
2020

21-
from tests import make_handler, assert_correct_response
21+
from tests import allow_windows, make_handler, assert_correct_response
2222
from tests.mockserver import MockServer, StaticMockServer
2323

2424

@@ -41,6 +41,7 @@ def inject_fixtures(self, caplog):
4141
caplog.set_level(logging.DEBUG)
4242
self._caplog = caplog
4343

44+
@allow_windows
4445
async def test_basic_response(self):
4546
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
4647
with StaticMockServer() as server:

0 commit comments

Comments
 (0)