kevlened / pytest-parallel

A pytest plugin for parallel and concurrent testing
https://github.com/browsertron/pytest-parallel/issues/104#issuecomment-1293941066
MIT License
313 stars 60 forks source link

session-scope fixtures aren't honored #26

Open hectorv opened 5 years ago

hectorv commented 5 years ago

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

spearsem commented 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.

emdej commented 4 years ago

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.

abcanqi commented 4 years ago

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.

pgp commented 3 years ago

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

ramihsn commented 2 years ago

Any update here?