Skip to content

Instantly share code, notes, and snippets.

@pablochacin
Last active August 23, 2019 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablochacin/23ce8ddee5cf9c6efb6bcfb3e267f0f8 to your computer and use it in GitHub Desktop.
Save pablochacin/23ce8ddee5cf9c6efb6bcfb3e267f0f8 to your computer and use it in GitHub Desktop.
POC for dynamic pytest fixture scope definition

Introduction

POC for dynamically defining the scope of a fixture based on a cli parameter.

session scope

pytest -s -v --scope=session
========================================= test session starts ==========================================
platform linux -- Python 3.6.5, pytest-5.0.0, py-1.5.2, pluggy-0.12.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /tmp/pytest
collected 5 items                                                                                      

test_1.py::test_1 
In some_resource()

In test_1()
PASSED
test_1.py::test_2 
In test_2()
PASSED
test_1.py::test_alpha_3 
In other_resource()

In test_3()
PASSED
test_1.py::test_alpha_4 
In test_4()
PASSED
test_1.py::test_alpha_5 
In test_5()
PASSED
In other_resource_fin()

In some_resource_fin()


======================================= 5 passed in 0.02 seconds =======================================

Function scope

pytest -s -v --scope=function
========================================= test session starts ==========================================
platform linux -- Python 3.6.5, pytest-5.0.0, py-1.5.2, pluggy-0.12.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /tmp/pytest
collected 5 items                                                                                      

test_1.py::test_1 
In some_resource()

In test_1()
PASSED
test_1.py::test_2 
In test_2()
PASSED
test_1.py::test_alpha_3 
In other_resource()

In test_3()
PASSED
In other_resource_fin()

test_1.py::test_alpha_4 
In other_resource()

In test_4()
PASSED
In other_resource_fin()

test_1.py::test_alpha_5 
In other_resource()

In test_5()
PASSED
In other_resource_fin()

In some_resource_fin()


======================================= 5 passed in 0.02 seconds =======================================
import pytest
def pytest_addoption(parser):
"""
Adds the option pytest option list.
This options can be used to initilize fixtures.
"""
parser.addoption("--scope", action="store", help="cluster scope")
scope = "function"
def pytest_configure(config):
global scope
if config.getoption("scope"):
scope = config.getoption("scope")
@pytest.fixture(scope="session")
def some_resource(request):
print('\nIn some_resource()')
def resource_fin():
print('\nIn some_resource_fin()')
request.addfinalizer(resource_fin)
import pytest
import conftest
@pytest.fixture(scope=conftest.scope)
def other_resource(request):
print('\nIn other_resource()')
def resource_fin():
print('\nIn other_resource_fin()')
request.addfinalizer(resource_fin)
from fixtures import other_resource
def test_1(some_resource):
print('\nIn test_1()')
def test_2(some_resource):
print('\nIn test_2()')
def test_alpha_3(other_resource):
print('\nIn test_3()')
def test_alpha_4(other_resource):
print('\nIn test_4()')
def test_alpha_5(some_resource, other_resource):
print('\nIn test_5()')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment