Skip to content

Module scope fixture runs on function scope #660

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

Closed
pytestbot opened this issue Jan 15, 2015 · 4 comments
Closed

Module scope fixture runs on function scope #660

pytestbot opened this issue Jan 15, 2015 · 4 comments
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

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.


@pytestbot
Copy link
Contributor Author

Original comment by Earl Berg (BitBucket: earlbeezer, GitHub: earlbeezer):


I ran into this as well. Do you know a workaround to make the module fixture run only once (per module)?

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


Actually it's a bug in error reporting as your example is not valid. It is not allowed to use a "module scoped" fixture from a "function scoped" one.

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


fix issue660: properly report fixture scope mismatches independent
from fixture argument ordering.

@pytestbot
Copy link
Contributor Author

Original comment by Floris Bruynooghe (BitBucket: flub, GitHub: flub):


Merged in issue660 (pull request #268)

fix issue660

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

1 participant