user3282758
user3282758

Reputation: 1471

scope="module" does not seem to be working in pytest

I am working with pytest fixtures. My test module is as follows :

import pytest

@pytest.yield_fixture
@pytest.fixture(scope="module")
def jlt() :
    print("setup executed")
    yield None
    print("tearing up")

def test_one(jlt) :
    id = 123
    assert id == 123

def test_two(jlt) :
    id = 456
    assert id == 456

I am executing this as follows :

py.test -v --capture=no test_jlt.py

The output is :

platform linux2 -- Python 2.7.12, pytest-2.8.7, py-1.4.31, pluggy-0.3.1    -- /usr/bin/python
cachedir: ../../.cache
rootdir: /home/vandana/unix Home/pythonLearn, inifile: pytest.ini
collected 2 items 

test_jlt.py::test_one setup executed
PASSEDtearing up

test_jlt.py::test_two setup executed
PASSEDtearing up

The scope="module" does not seem to be working. The fixture is getting executed for each function and not just once for the entire module. I do not know what should be done

Upvotes: 0

Views: 573

Answers (1)

The Compiler
The Compiler

Reputation: 11939

@pytest.yield_fixture replaces @pytest.fixture, so you should use @pytest.yield_fixture(scope="module") instead.

Note that with pytest 3.x you can simply use @pytest.fixture and use yield inside the fixture, which simplifies things a bit.

Upvotes: 2

Related Questions