pytest-dev / pytest-dev/pytest

Unexpected behavior of class and package scoped fixtures

Open
#8,712 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

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.

  1. Class scoped fixture is getting executed even before the test functions which are not part of the class.
  2. There are two packages in the project. However, package scoped fixture executed only once.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the fixture definitions in conftest.py and the tests in pack1/test_a.py, pack1/test_b.py, and pack2/test_c.py; run the reported pytest -v -s command. Compare the observed class and package fixture lifecycles with the documented scopes, then determine whether the implementation or documentation needs correction. Done means the behavior and documentation agree for both packages and class boundaries.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.