pytest-parallel icon indicating copy to clipboard operation
pytest-parallel copied to clipboard

session-scope fixtures aren't honored

Open hectorv opened this issue 7 years ago • 5 comments

Similar to what happens with pytest-xdist: https://github.com/pytest-dev/pytest-xdist/issues/271

hectorv avatar Nov 29 '18 06:11 hectorv

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.

spearsem avatar Jul 26 '19 14:07 spearsem

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.

emdej avatar Dec 31 '19 12:12 emdej

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.

abcanqi avatar Aug 11 '20 08:08 abcanqi

Any news on this? I can confirm session fixtures are not honoured even in single-worker (multi-threaded) mode

pgp avatar Feb 22 '21 16:02 pgp

Any update here?

ramihsn avatar Jun 16 '22 08:06 ramihsn