Skip to content

Instantly share code, notes, and snippets.

@leshikus
Created August 20, 2024 20:48
Show Gist options
  • Save leshikus/2e5dc8b31bc3d6e9972535630e14ec10 to your computer and use it in GitHub Desktop.
Save leshikus/2e5dc8b31bc3d6e9972535630e14ec10 to your computer and use it in GitHub Desktop.
a snipped from pytest_select plugin
def pytest_collection_modifyitems(session, config, items):
seen_test_names = set()
for item in items:
seen_test_names.add(item.name)
seen_test_names.add(item.nodeid)
selection_file_name = config.getoption("deselectfromfile")
if selection_file_name is not None:
selection_file_path = Path(selection_file_name)
with selection_file_path.open("rt", encoding="UTF-8") as selection_file:
test_names = {test_name.strip() for test_name in selection_file}
missing_test_names = test_names - seen_test_names
if missing_test_names:
# If any items remain in `test_names` those tests either don't exist or
# have been deselected by another way - warn user
message = (
f"pytest-select: Missing deselected test names:\n - "
)
message += "\n - ".join(missing_test_names)
if config.getoption("selectfailonmissing"):
raise UsageError(message)
warnings.warn(message, PytestSelectWarning)
for item in items:
if item.name in test_names or item.nodeid in test_names:
item.add_marker(skip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment