Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Update conftest.py to refactor autouse pytest.skip() fixtures into pytest_runtest_setup() (#7173)

DenisC1a3e343dc41c
diff --git a/conftest.py b/conftest.py
index 0dd9d1ee0..55b5a28c1 100644
--- a/conftest.py
+++ b/conftest.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import importlib
from pathlib import Path
from typing import TYPE_CHECKING
@@ -66,67 +67,6 @@ def reactor_pytest(request) -> str:
return request.config.getoption("--reactor")
[email protected](autouse=True)
-def only_asyncio(request, reactor_pytest):
- if request.node.get_closest_marker("only_asyncio") and reactor_pytest != "asyncio":
- pytest.skip("This test is only run with --reactor=asyncio")
-
-
[email protected](autouse=True)
-def only_not_asyncio(request, reactor_pytest):
- if (
- request.node.get_closest_marker("only_not_asyncio")
- and reactor_pytest == "asyncio"
- ):
- pytest.skip("This test is only run without --reactor=asyncio")
-
-
[email protected](autouse=True)
-def requires_uvloop(request):
- if not request.node.get_closest_marker("requires_uvloop"):
- return
- try:
- import uvloop # noqa: PLC0415
-
- del uvloop
- except ImportError:
- pytest.skip("uvloop is not installed")
-
-
[email protected](autouse=True)
-def requires_botocore(request):
- if not request.node.get_closest_marker("requires_botocore"):
- return
- try:
- import botocore # noqa: PLC0415
-
- del botocore
- except ImportError:
- pytest.skip("botocore is not installed")
-
-
[email protected](autouse=True)
-def requires_boto3(request):
- if not request.node.get_closest_marker("requires_boto3"):
- return
- try:
- import boto3 # noqa: PLC0415
-
- del boto3
- except ImportError:
- pytest.skip("boto3 is not installed")
-
-
[email protected](autouse=True)
-def requires_mitmproxy(request):
- if not request.node.get_closest_marker("requires_mitmproxy"):
- return
- try:
- import mitmproxy # noqa: F401, PLC0415
- except ImportError:
- pytest.skip("mitmproxy is not installed")
-
-
def pytest_configure(config):
if config.getoption("--reactor") == "asyncio":
# Needed on Windows to switch from proactor to selector for Twisted reactor compatibility.
@@ -134,5 +74,31 @@ def pytest_configure(config):
set_asyncio_event_loop_policy()
+def pytest_runtest_setup(item):
+ # Skip tests based on reactor markers
+ reactor = item.config.getoption("--reactor")
+
+ if item.get_closest_marker("only_asyncio") and reactor != "asyncio":
+ pytest.skip("This test is only run with --reactor=asyncio")
+
+ if item.get_closest_marker("only_not_asyncio") and reactor == "asyncio":
+ pytest.skip("This test is only run without --reactor=asyncio")
+
+ # Skip tests requiring optional dependencies
+ optional_deps = [
+ "uvloop",
+ "botocore",
+ "boto3",
+ "mitmproxy",
+ ]
+
+ for module in optional_deps:
+ if item.get_closest_marker(f"requires_{module}"):
+ try:
+ importlib.import_module(module)
+ except ImportError:
+ pytest.skip(f"{module} is not installed")
+
+
# Generate localhost certificate files, needed by some tests
generate_keys()