Skip to content

Instantly share code, notes, and snippets.

@hackebrot
Last active February 1, 2016 22:55
Show Gist options
  • Save hackebrot/05f42c78a069bbb16233 to your computer and use it in GitHub Desktop.
Save hackebrot/05f42c78a069bbb16233 to your computer and use it in GitHub Desktop.
pytest magic
def pytest_collection_modifyitems(items, config):
selected_items = []
deselected_items = []
for item in items:
if 'new_fixture' in getattr(item, 'fixturenames', ()):
selected_items.append(item)
else:
deselected_items.append(item)
config.hook.pytest_deselected(items=deselected_items)
items[:] = selected_items
import pytest
@pytest.fixture
def new_fixture():
return 'foobar'
@pytest.fixture
def another_fixture(new_fixture):
return 'foobar'
def test_abc(new_fixture):
assert True
def test_def(another_fixture):
assert False
def test_that_does_not_use_fixture():
assert False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment