@@ -29,59 +29,47 @@ def pytest_addoption(parser):
2929 )
3030
3131
32- def pytest_collection_modifyitems (config , items ):
33- """Modify test collection to skip tests not matching the --driver option."""
34- driver_from_cli = config .getoption ("--driver" )
35-
36- if not driver_from_cli :
37- # No driver specified, run all tests
38- return
39-
40- # Determine if the specified driver is sync or async
41- is_async_driver = driver_from_cli in ASYNC_DRIVERS
42- is_sync_driver = driver_from_cli in SYNC_DRIVERS
43-
44- if not is_async_driver and not is_sync_driver :
45- # Invalid driver
46- return
47-
48- # Filter out tests that don't match the specified driver
49- skip_marker = pytest .mark .skip (reason = f"Test uses different driver (--driver={ driver_from_cli } specified)" )
50-
51- for item in items :
52- # Parse the test name to extract driver info
53- # Format is usually: test_name[fixture_name-driver_name]
54- item_id = item .nodeid
55-
56- # Check if the test has a specific driver in its ID
57- # Extract driver name from test ID (e.g., test_name[pgmq_by_dsn-psycopg2])
58- if '[' in item_id and ']' in item_id :
59- # Extract the part between brackets
60- bracket_content = item_id [item_id .find ('[' )+ 1 :item_id .find (']' )]
61-
62- # Check for async fixtures by name (more precise than string matching)
63- is_async_test = any (async_fixture in bracket_content for async_fixture in ASYNC_FIXTURE_NAMES )
64-
65- # Skip async tests if sync driver specified
66- if is_sync_driver and is_async_test :
67- item .add_marker (skip_marker )
68- continue
69-
70- # Skip sync tests if async driver specified
71- if is_async_driver and not is_async_test :
72- item .add_marker (skip_marker )
73- continue
74-
75- # Check if any known driver is in the bracket content
76- # Sort drivers by length (descending) to match longer names first (e.g., psycopg2cffi before psycopg2)
77- sorted_drivers = sorted (SYNC_DRIVERS + ASYNC_DRIVERS , key = len , reverse = True )
78- for driver in sorted_drivers :
79- if f"-{ driver } ]" in item_id or f"-{ driver } -" in bracket_content :
80- # This test is for a specific driver
81- if driver != driver_from_cli :
82- # Skip if it doesn't match the CLI driver
83- item .add_marker (skip_marker )
84- break
32+ def pytest_generate_tests (metafunc ):
33+ """
34+ Dynamically generate test parametrization based on CLI options.
35+
36+ This allows us to parametrize fixtures based on the --driver option.
37+ """
38+ if "pgmq_all_variants" in metafunc .fixturenames :
39+ driver_from_cli = metafunc .config .getoption ("--driver" )
40+
41+ # Define sync and async fixture variants
42+ sync_fixtures = [
43+ 'pgmq_by_dsn' ,
44+ 'pgmq_by_engine' ,
45+ 'pgmq_by_session_maker' ,
46+ 'pgmq_by_dsn_and_engine' ,
47+ 'pgmq_by_dsn_and_session_maker' ,
48+ ]
49+
50+ async_fixtures = [
51+ 'pgmq_by_async_dsn' ,
52+ 'pgmq_by_async_engine' ,
53+ 'pgmq_by_async_session_maker' ,
54+ ]
55+
56+ # Determine which fixtures to use
57+ if not driver_from_cli :
58+ # No driver specified, use all fixtures
59+ fixture_params = sync_fixtures + async_fixtures
60+ elif driver_from_cli in ASYNC_DRIVERS :
61+ # Async driver specified
62+ fixture_params = async_fixtures
63+ else :
64+ # Sync driver specified
65+ fixture_params = sync_fixtures
66+
67+ # Parametrize the test
68+ metafunc .parametrize (
69+ "pgmq_all_variants" ,
70+ fixture_params ,
71+ indirect = True
72+ )
8573
8674
8775@pytest .fixture (scope = "module" )
@@ -113,7 +101,7 @@ def get_sa_db(request):
113101 return os .getenv ("SQLALCHEMY_DB" , "postgres" )
114102
115103
116- @pytest .fixture (scope = "function" , params = SYNC_DRIVERS )
104+ @pytest .fixture (scope = "function" )
117105def get_dsn (
118106 request : FixtureRequest ,
119107 get_sa_host ,
@@ -122,19 +110,20 @@ def get_dsn(
122110 get_sa_password ,
123111 get_sa_db ,
124112):
125- """Get DSN for sync drivers."""
113+ """Get DSN for sync drivers based on CLI option ."""
126114 driver_from_cli = request .config .getoption ("--driver" )
127115
128- # Use CLI driver if specified, otherwise use parametrized driver
116+ # Use CLI driver if specified and it's a sync driver
129117 if driver_from_cli and driver_from_cli in SYNC_DRIVERS :
130118 driver = driver_from_cli
131119 else :
132- driver = request .param
120+ # Default to first sync driver if no CLI option or invalid
121+ driver = SYNC_DRIVERS [0 ]
133122
134123 return f"postgresql+{ driver } ://{ get_sa_user } :{ get_sa_password } @{ get_sa_host } :{ get_sa_port } /{ get_sa_db } "
135124
136125
137- @pytest .fixture (scope = "function" , params = ASYNC_DRIVERS )
126+ @pytest .fixture (scope = "function" )
138127def get_async_dsn (
139128 request : FixtureRequest ,
140129 get_sa_host ,
@@ -143,14 +132,15 @@ def get_async_dsn(
143132 get_sa_password ,
144133 get_sa_db ,
145134):
146- """Get DSN for async drivers."""
135+ """Get DSN for async drivers based on CLI option ."""
147136 driver_from_cli = request .config .getoption ("--driver" )
148137
149- # Use CLI driver if specified, otherwise use parametrized driver
138+ # Use CLI driver if specified and it's an async driver
150139 if driver_from_cli and driver_from_cli in ASYNC_DRIVERS :
151140 driver = driver_from_cli
152141 else :
153- driver = request .param
142+ # Default to first async driver if no CLI option or invalid
143+ driver = ASYNC_DRIVERS [0 ]
154144
155145 return f"postgresql+{ driver } ://{ get_sa_user } :{ get_sa_password } @{ get_sa_host } :{ get_sa_port } /{ get_sa_db } "
156146
0 commit comments