My pytest test files are spread across multiple packages, and they share some common fixtures. However, I found that my auto-used session-scoped fixture is ran multiple times.
Here is a basic structure of my project:
.
├── Pipfile
├── Pipfile.lock
├── __init__.py
├── common
│ ├── __init__.py
│ └── conftest.py
├── pkg_a
│ ├── __init__.py
│ ├── conftest.py
│ └── test_a.py
└── pkg_b
├── __init__.py
├── conftest.py
└── test_b.py
And here are the content of each .py file:
==> ./__init__.py <==
==> ./common/__init__.py <==
==> ./common/conftest.py <==
import pytest
@pytest.fixture(scope='session', autouse=True)
def setup():
print 'setting up'
yield
print 'tearing down'
==> ./pkg_a/__init__.py <==
==> ./pkg_a/conftest.py <==
from common.conftest import *
==> ./pkg_a/test_a.py <==
def test():
assert True
==> ./pkg_b/__init__.py <==
==> ./pkg_b/conftest.py <==
from common.conftest import *
==> ./pkg_b/test_b.py <==
def test_b():
assert True
Here is the output of pytest:
➜ pytest -s pkg_a pkg_b
========================== test session starts ==========================
platform darwin -- Python 2.7.15, pytest-3.10.0, py-1.7.0, pluggy-0.8.0
rootdir: /path/to/the/project, inifile:
collected 2 items
pkg_a/test_a.py setting up
.
pkg_b/test_b.py setting up
.tearing down
tearing down
======================= 2 passed in 0.02 seconds ========================
My understanding to the session fixtures is that they will only be ran once for the lifetime of pytest command. But here the setting up and tearing down are printed twice, and they are interleaved.
Is there any way to only execute the fixture only once?
P.S. I know the parameterized session fixture will be execute multiple times. But I don't think my fixture is parameterized.
Aucun commentaire:
Enregistrer un commentaire