Skip to content

Instantly share code, notes, and snippets.

@mgaitan
Last active March 16, 2023 18:18
Show Gist options
  • Save mgaitan/ef05a7ca1ef4dde94112e13c6a4c06de to your computer and use it in GitHub Desktop.
Save mgaitan/ef05a7ca1ef4dde94112e13c6a4c06de to your computer and use it in GitHub Desktop.
from pathlib import Path
import pytest
def pytest_addoption(parser):
parser.addoption("--skip-list-file", default=".skip_tests", help="skip listed tests")
def pytest_collection_modifyitems(config, items):
skip_list = Path(config.getoption("--skip-list-file"))
if not skip_list.exists():
return
tests_to_skip = skip_list.read_text().splitlines()
if not tests_to_skip:
# --skiplist not given in cli, therefore move on
return
skip_listed = pytest.mark.skip(reason=f"Listed in {skip_list}")
for item in items:
if item.nodeid in tests_to_skip:
item.add_marker(skip_listed)
import re
import subprocess
import sys
from pathlib import Path
loop = 0
TARGET = re.compile(r"Current target is\: (.*)")
LEAK = re.compile(r"Leak found in\: (.*)")
target = None
while True:
loop += 1
cmd = ["pytest", "-v", "--rootdir=.", "--skip-list-file=.skip_tests", "--leak-finder"] + sys.argv[1:]
print(f"----- Executing loop {loop}")
print(f"running {' '.join(cmd)}")
result = subprocess.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
new_target = re.search(TARGET, result.stdout)
if new_target:
target = new_target.group(1)
leak = re.search(LEAK, result.stdout)
if leak:
leak = leak.group(1)
print("verifying")
print(f"pytest -v {leak} {target}")
# we need to cache-clear so the next loop --leak-finder will start from scract with the failing test skipped
subprocess.run(["pytest", "--cache-clear", "-v", leak, target], text=True)
# skip the target (failing) via file
with Path(".skip_tests").open("a") as skip:
skip.write(f"{target}\n")
# next loop
loop = 0
@mgaitan
Copy link
Author

mgaitan commented Mar 16, 2023

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment