I am trying to use pytest-xdist to run my tests faster by running on multiple CPUs. However, when I switch to using multiple CPUs the tests don't run because there is an error when collecting the tests.
More specifically, the error is
AttributeError: module '__main__' has no attribute '__file__'
This is because the scripts being tested import a logging module I wrote. The logging module uses
__main__.__file__
to determine the name of the main application for purposes of adding it to the log output filename.
I can't seem to figure out what pytest is doing differently in multiple CPU mode than in single CPU mode.
So when I execute my tests as
python3 -m pytest
everything runs just fine, but once I switch to
python3 -m pytest -n 4
I get these errors:
____________________ ERROR collecting ***.py
***/logging/log_setup.py:21: in <module>
app_name = os.path.basename(__main__.__file__)[:-3]
E AttributeError: module '__main__' has no attribute '__file__'
The same problem occurs when I try to execute using the Python subprocess method:
python3 -m pytest -d --tx 3*popen//python=python3
EDIT: I am side stepping the issue for now with the following code in my logging module:
if hasattr(__main__, '__file__'):
app_name = os.path.basename(__main__.__file__)[:-3]
else:
app_name = 'unknown_app_name'
Now pytest with multiple CPUs works fine for me.