I need to inspect the list of collected tests (specifically, module attributes) before executing any of them. #11687
Replies: 2 comments 6 replies
-
Hi, i'm not sure a hook is necessary here. you can just create a session fixture which runs by itself; since it's a session fixture it already knows which tests are collected. import runpy
def get_module_from_path(module_path):
# Run the module and get the module dictionary
module_dict = runpy.run_path(module_path)
return module_dict.get("__main__")
@pytest.fixture(scope="session", autouse=True)
def download_files_session(request):
test_modules = [get_module_from_path(item.reportinfo()[0]) for item in request.items]
test_ids = [getattr(test_module, "TEST_ID", None) for test_module in test_modules]
download_files_for_tests(tests_ids) # The part where you download S3 files idk If it's not what you need, i'm sorry I didn't understand your problem |
Beta Was this translation helpful? Give feedback.
-
there is no builtin way to manage preloading of something that should be a module scoped session i would recommend a plugin object that implement the pytest_modifyitems hook and the sessionstart hook either eagerly download or use a concurrency mechanism (however thats possibly more risky the pytest cache object can be used to cache the downloads if they are used immutable |
Beta Was this translation helpful? Give feedback.
-
I've written a fixture that downloads the files from S3 that are necessary to run the test. These files are large and there are lot of them, so we don't want to download everything each time, even if only a few tests are selected, and we don't want them in our
git
repository, either. It does this by using the value ofgetattr(request.module, "TEST_ID", None)
to know where on S3 to get the needed files.Everything is working perfectly, but I'm having an unexpected problem. My S3 access requires MFA, and the test suite takes so long to run that I'm prompted multiple times for an MFA code. Obviously I'm not sitting there watching it run, so this causes big delays in getting through the tests.
What I need to do is download the data for all of (and only) the selected tests before executing any of them.
pytest_collection_modifyitems
hook, but it's unclear to me whether it's able to access the containing module the way my fixture does viarequest
.Beta Was this translation helpful? Give feedback.
All reactions