Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session-scope fixtures aren't honored #26

Open
hectorv opened this issue Nov 29, 2018 · 5 comments
Open

session-scope fixtures aren't honored #26

hectorv opened this issue Nov 29, 2018 · 5 comments
Labels
bug Something isn't working

Comments

@hectorv
Copy link

hectorv commented Nov 29, 2018

Similar to what happens with pytest-xdist: pytest-dev/pytest-xdist#271

@kevlened kevlened added the bug Something isn't working label Dec 19, 2018
@spearsem
Copy link

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
Copy link

emdej commented Dec 31, 2019

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
Copy link

abcanqi commented Aug 11, 2020

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
Copy link

pgp commented Feb 22, 2021

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

@ramihsn
Copy link

ramihsn commented Jun 16, 2022

Any update here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants