Open ogrisel opened 2 days ago
I bumped into pytest-parallel that has been archived and is no longer maintained (latest release in October 2021 and see https://github.com/kevlened/pytest-parallel/issues/104#issuecomment-1293941066), but seems to run the fixture and the test function in the same thread. The following command runs fine:
pytest --tests-per-worker 2 test_simple.py
See their plugin code.
Full disclosure, I had to edit the plugin code to avoid an AttributeError
(probably something has changed in pytest since):
self._log = print # py.log.Producer('pytest-parallel')
As discussed in the linked scikit-learn issue, it's possible to convert such thread-sensitive fixtures to regular function decorators and the problems go away.
So we have a workaround.
Note that the above workaround is not always valid. For instance, the standard tmpdir
fixture does no longer ensure working in an isolated temporary directory for a parametrized test:
@pytest.mark.parametrize("value", range(10))
def test_stuff_in_isolated(value, tmpdir):
subfolder = tmpdir.mkdir("subfoler") # fails with py.error.EEXIST: [File exists]...
# Do test stuff in "subfolder" and "value".
Currently, pytest fixtures seem to always be executed in the main thread. This prevents initializing thread-local configuration to run a particular test in isolation of concurrently running tests with different thread-local settings.
See the reproducer below:
resulting in:
Would it be possible to execute function-scope fixtures on the same thread as the test function?
Originally discussed in the context of testing scikit-learn in free threading mode: https://github.com/scikit-learn/scikit-learn/issues/30007