[Pytest-commit] Issue #660: Module scope fixture runs on function scope (hpk42/pytest)

Praveen Shirali issues-reply at bitbucket.org
Thu Jan 15 14:57:44 CET 2015


New issue 660: Module scope fixture runs on function scope
https://bitbucket.org/hpk42/pytest/issue/660/module-scope-fixture-runs-on-function

Praveen Shirali:

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`.




More information about the pytest-commit mailing list