@@ -115,6 +115,12 @@ def pytest_collection_modifyitems(items, config):
115115 ]
116116
117117 for item in items :
118+ if config .getoption ("--doctest-modules" ) or config .getoption (
119+ "--doctest-cython" , default = False
120+ ):
121+ # autouse=True for the add_doctest_imports can lead to expensive teardowns
122+ # since doctest_namespace is a session fixture
123+ item .add_marker (pytest .mark .usefixtures ("add_doctest_imports" ))
118124 # mark all tests in the pandas/tests/frame directory with "arraymanager"
119125 if "/frame/" in item .nodeid :
120126 item .add_marker (pytest .mark .arraymanager )
@@ -187,6 +193,15 @@ def pytest_collection_modifyitems(items, config):
187193 )
188194
189195
196+ @pytest .fixture
197+ def add_doctest_imports (doctest_namespace ):
198+ """
199+ Make `np` and `pd` names available for doctests.
200+ """
201+ doctest_namespace ["np" ] = np
202+ doctest_namespace ["pd" ] = pd
203+
204+
190205# ----------------------------------------------------------------
191206# Autouse fixtures
192207# ----------------------------------------------------------------
@@ -198,15 +213,6 @@ def configure_tests():
198213 pd .set_option ("chained_assignment" , "raise" )
199214
200215
201- @pytest .fixture (autouse = True )
202- def add_imports (doctest_namespace ):
203- """
204- Make `np` and `pd` names available for doctests.
205- """
206- doctest_namespace ["np" ] = np
207- doctest_namespace ["pd" ] = pd
208-
209-
210216# ----------------------------------------------------------------
211217# Common arguments
212218# ----------------------------------------------------------------
0 commit comments