Repository navigation
Unexpected behavior of class and package scoped fixtures #8712
Copy link
Copy link
Open
Labels
topic: fixturesanything involving fixtures directly or indirectlyanything involving fixtures directly or indirectly
Description
Class and package scoped fixtures are not behaving as the way it is documented
Pytest - 6.2.4
Pytest_Demo
│ conftest.py
├───pack1
│ test_a.py
│ test_b.py
│ __init__.py
└───pack2
test_c.py
__init__.py
conftest.py
import pytest
@pytest.fixture(scope='session', autouse=True)
def ses_fix():
print('session fixture start')
yield
print('session fixture end')
@pytest.fixture(scope='package', autouse=True)
def pak_fix():
print('package fixture start')
yield
print('package fixture end')
@pytest.fixture(scope='module', autouse=True)
def mod_fix():
print('module fixture start')
yield
print('module fixture end')
@pytest.fixture(scope='class', autouse=True)
def cls_fix():
print('class fixture start')
yield
print('class fixture end')
@pytest.fixture(scope='function', autouse=True)
def func_fix():
print('function fixture start')
yield
print('function fixture end')
test_a.py
class TestA:
def test_a1(self):
assert True
def test_a2(self):
assert True
def test_a3(self):
assert True
test_b.py
def test_b1():
assert True
def test_b2():
assert True
def test_b3():
assert True
test_c.py
def test_c1():
assert True
def test_c2():
assert True
def test_c3():
assert True
pytest -v -s produces below output
pack1/test_a.py::TestA::test_a1 session fixture start
package fixture start
module fixture start
class fixture start
function fixture start
PASSEDfunction fixture end
pack1/test_a.py::TestA::test_a2 function fixture start
PASSEDfunction fixture end
pack1/test_a.py::TestA::test_a3 function fixture start
PASSEDfunction fixture end
class fixture end
module fixture end
pack1/test_b.py::test_b1 module fixture start
class fixture start
function fixture start
PASSEDfunction fixture end
class fixture end
pack1/test_b.py::test_b2 class fixture start
function fixture start
PASSEDfunction fixture end
class fixture end
pack1/test_b.py::test_b3 class fixture start
function fixture start
PASSEDfunction fixture end
class fixture end
module fixture end
pack2/test_c.py::test_c1 module fixture start
class fixture start
function fixture start
PASSEDfunction fixture end
class fixture end
pack2/test_c.py::test_c2 class fixture start
function fixture start
PASSEDfunction fixture end
class fixture end
pack2/test_c.py::test_c3 class fixture start
function fixture start
PASSEDfunction fixture end
class fixture end
module fixture end
package fixture end
session fixture end
I am observing weird behavior of class and package scoped fixtures.
- Class scoped fixture is getting executed even before the test functions which are not part of the class.
- There are two packages in the project. However, package scoped fixture executed only once.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
topic: fixturesanything involving fixtures directly or indirectlyanything involving fixtures directly or indirectly
Activity
iwanb commentedon Jul 20, 2021
For 1., that is expected, if you use a class-scoped fixture on function tests, it will work the same as a function-scoped fixture. Conceptually each test has a class scope, which consists of only itself for function tests, and the class for method tests.