Skip to content

Unexpected behavior of class and package scoped fixtures #8712

Open

Description

@thatisvivek

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.

Activity

  1. iwanb commented on Jul 20, 2021

    @iwanb
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectly

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions