Skip to content

Instantly share code, notes, and snippets.

@squito
Created January 31, 2023 20:42
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 squito/d57c71a899c649f62a3e4a8eea030d03 to your computer and use it in GitHub Desktop.
Save squito/d57c71a899c649f62a3e4a8eea030d03 to your computer and use it in GitHub Desktop.
Pytest test looper
#!/usr/bin/env python
## Makes it easy to run tests in a loop
## Just a small bit of automation around something like
## ls quanta_cache.py test/test_quanta_cache.py | entr -r python -m pytest test/test_quanta_cache.py
## but that is just complex enough I would never remember
## If you want to test `jira.py` in a loop, run:
##
## test/test_loop.py --test jira.py
##
import argparse
import os
import subprocess
import sys
import tempfile
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run your tests in a loop forever, like sbt's ~test-only")
parser.add_argument("--test", required=True, help="code to test")
parser.add_argument("--watch-extras", help="any extra files to watch", action="append")
parser.add_argument("-v", "--verbose", help="turn on verbose loggging in pytest", action="store_true")
## TODO add option for `git ls-files`: https://jvns.ca/blog/2020/06/28/entr/
args = parser.parse_args()
test_file = "test/test_" + args.test
files_to_watch = [args.test, test_file]
if args.watch_extras is not None:
files_to_watch = files_to_watch + args.watch_extras
ls_p = subprocess.Popen(["ls"] + files_to_watch, stdout=subprocess.PIPE, bufsize=0)
cmd = ["entr", "-r", "python", "-m", "pytest"]
if args.verbose:
cmd.append("-v")
cmd.append(test_file)
entr_p = subprocess.Popen(cmd, stdin=ls_p.stdout, bufsize=0)
try:
entr_p.wait()
except KeyboardInterrupt:
sys.exit(entr_p.returncode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment