Last active
August 19, 2021 07:44
-
-
Save cb109/117fab8187c66cb5c6041339f69a008b to your computer and use it in GitHub Desktop.
Run and hold pytest fixtures until cancelled from commandline. Useful to manually debug/test a specific database state etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pytest_addoption(parser): | |
parser.addoption( | |
"--only-fixtures", | |
action="store", | |
help=( | |
"Execute the given fixture(s) and hold that state until " | |
"manually cancelled (Ctrl+C)." | |
), | |
) | |
def pytest_collection_modifyitems(items, config): | |
only_fixtures = config.getoption("--only-fixtures") | |
if only_fixtures is None: | |
return | |
try: | |
test_idx = [ | |
i for i, item in enumerate(items) if item.keywords.get("only_fixtures") | |
][0] | |
except IndexError: | |
print( | |
" ERROR: Cannot find blocking dummy test marked with " | |
"@pytest.mark.only_fixtures: Make sure to add it where " | |
"it can see the fixtures you are trying to run" | |
) | |
raise | |
dummy_test = items.pop(test_idx) | |
# Deselect all tests except the dummy. | |
config.hook.pytest_deselected(items=items) | |
fixture_names = [name.strip() for name in only_fixtures.split(",")] | |
dummy_test.name = dummy_test.name + "__" + "_".join(fixture_names) | |
dummy_test.fixturenames += fixture_names | |
items[:] = [dummy_test] | |
return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import pytest | |
@pytest.mark.only_fixtures | |
def test_only_fixtures(): | |
"""This test will be populated and run when '--only-fixtures' is given. | |
Make sure to place it where it can see all fixtures you are trying to run. | |
""" | |
print("Now holding fixture(s) state (ctrl+c to cancel) ...") | |
while True: | |
time.sleep(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Make sure you have added the blocking test with the | |
# @pytest.mark.only_fixtures decorator. Then run: | |
pytest --only-fixtures=foo,bar,whatever | |
# Ctrl+C when done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment