Skip to content

Module scope fixture runs on function scope #660

Closed
@pytestbot

Description

@pytestbot

Originally reported by: Praveen Shirali (BitBucket: praveenshirali, GitHub: praveenshirali)


I came across the following situation which I have simplified into the example below.
If a Module Fixture depends on a Function Fixture and a test depends on both, but has (Function Fixture, Module Fixture) in its argument order, the Module Fixture is reduced to run as a Function Fixture

Tested on py.test 2.6.4

#!python

import pytest

class SomeClass(object): pass

@pytest.fixture
def function_fixture(request):
    def fin():
        print "I am a function finalizer"
    request.addfinalizer(fin)
    print "\n"
    print "I am a function fixture"
    return SomeClass()

@pytest.fixture(scope="module")
def module_fixture(request, function_fixture):
    def fin():
        print "I am a module finalizer"
    request.addfinalizer(fin)
    print "I am a module fixture"
    return SomeClass()

def test_one(function_fixture, module_fixture):
    print "Test One"
    print "Module Fixture: {}".format(module_fixture)

def test_two(function_fixture, module_fixture):
    print "Test Two"
    print "Module Fixture: {}".format(module_fixture)

Output: (-s --verbose)

test.py::test_one

I am a function fixture
I am a module fixture
Test One
Module Fixture: <test.SomeClass object at 0x102f037d0>
PASSEDI am a module finalizer
I am a function finalizer

test.py::test_two

I am a function fixture
I am a module fixture
Test Two
Module Fixture: <test.SomeClass object at 0x102f48fd0>
PASSEDI am a module finalizer
I am a function finalizer

Notice that the Module Fixture code has executed twice and the addresses of the SomeClass instances are different.

If module_fixture did not depend on function_fixture, the module_fixture would run only once. Test function calling (module_fixture, function_fixture) results in a ScopeMismatchError.


Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions