Open hectorv opened 5 years ago
I wonder if pytest-xdist could provide a new session scope called 'process'
(which is effectively 'session'
scoped per each process, which is what happens to 'session'
scoped fixtures today) and then either generate an error if it encounters a 'session'
scoped fixture, or print an obnoxious deprecation warning and tell the user that 'session'
scoped fixtures are being implicitly converted into 'process'
scoped fixtures with a big link to some documentation about the difference?
The purpose of actually making an extra 'process'
scope option would be so that users either cannot use 'session'
with pytest-xdist at all, or else are flagrantly warned that the behavior is actually different than a true pytest 'session'
scoped fixture, and it is less possible to use 'session'
and be confused by the behavior.
This is somehow expected in multiprocess operation mode (workers>1), but in thread mode (tests-per-worker>1) scopes "module" and "session" should be honored - the fixture should be shared.
In thread mode
, you can try this
conftest.py
:
import pytest
from threading import Lock
mutex = Lock()
a = None
@pytest.fixture(scope='session')
def variable_a():
mutex.acquire()
global a
if not a:
a = object()
mutex.release()
return a
test.py
:
def test_1(variable_a):
assert variable_a == 0
def test_2(variable_a):
assert variable_a == 0
def test_3(variable_a):
assert variable_a == 0
def test_4(variable_a):
assert variable_a == 0
run
& result
> pytest test.py --tests-per-worker 4
FAILED test.py::test_4 - assert <object object at 0x7f9362271370> == 0
FAILED test.py::test_3 - assert <object object at 0x7f9362271370> == 0
FAILED test.py::test_2 - assert <object object at 0x7f9362271370> == 0
FAILED test.py::test_1 - assert <object object at 0x7f9362271370> == 0
They are all one object, but can't use it in multiprocess operation mode
.
Any news on this? I can confirm session fixtures are not honoured even in single-worker (multi-threaded) mode
Any update here?
Similar to what happens with
pytest-xdist
: https://github.com/pytest-dev/pytest-xdist/issues/271