About skipping the initialization of modules/script that include no tests #9922
-
Thanks for maintaining this leading python testing library. Currently, as I see, during collection any file matching the collection path and naming would be loaded and initialized as a python script or module, even if it does not contain tests. This means that loading and initialization of all modules and their imported modules occurs on every file matching the search criteria even if the file has no tests, which in my case (detailed below) makes collection slow, and may also have undesirable side effects as I describe below in the context of python script files. Surely this scenario is not among the main ones conceived in the Java tradition which pytest may have drawn from, so it's not really a good feature request. But here is the scenario I have struggled with and still am:
As much as I have checked currently one can avoid the module loading and initialization either by modifying the configuration (however, maintaining an ignore configuration is somewhat error prone, especially when source files are now and then renamed), or by using:
However that kind of skip will also avoid the module/script loading when being run as a python script/module, so it cannot be a fit for the wider scenario. I'm okay if this is deemed a pathological scenario, or outside what the technical test discovery architecture allows. I can say though, that with some test-driven flows of development I wish this flexibility had been incorporated, contrary to the tradition of separating tests code exclusively at the file system level of file names or test directories. Just in case any creative ideas or comments come to mind ... |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
pytest needs to import your module to see if there's test functions in it. There's no way around that (except maybe statically analyzing the code, but that really doesn't seem worth the trouble). Running non-trivial code at import time is usually an anti-pattern, so I would suggest to change your code so that it doesn't need to do that. |
Beta Was this translation helpful? Give feedback.
-
It's a pathological scenario, i would recommend to create external plugins to support that use case for now |
Beta Was this translation helpful? Give feedback.
It's a pathological scenario, i would recommend to create external plugins to support that use case for now