Skip to content

Instantly share code, notes, and snippets.

@en0
Created December 4, 2024 21:18
Show Gist options
  • Save en0/f3aa16ac1b613f9e9e599d2488f771ce to your computer and use it in GitHub Desktop.
Save en0/f3aa16ac1b613f9e9e599d2488f771ce to your computer and use it in GitHub Desktop.
unittest
from argparse import ArgumentParser
from os import walk
from os.path import join as join_path
from sys import stderr
with open("list_of_test_files.txt", "r") as fd:
test_files = [l.rstrip('\n') for l in fd]
def write_file(p, content):
with open(p, "w") as fd:
fd.writelines(content)
def read_file(p):
with open(p, 'r') as fd:
return list(fd)
def skip_all_tests(p):
content = read_file(p)
if content[0] != "import pytest; pytestmark = pytest.mark.skip('IMWORKINGONIT')\n":
content.insert(0, "import pytest; pytestmark = pytest.mark.skip('IMWORKINGONIT')\n")
write_file(p, content)
def unskip_all_tests(p):
content = read_file(p)
if content[0] == "import pytest; pytestmark = pytest.mark.skip('IMWORKINGONIT')\n":
write_file(p, content[1:])
def find_all_test_files(root):
for path, subdirs, files in walk(root):
for name in files:
if not name.endswith(".py") or not name.startswith("test_"):
continue
yield join_path(path, name)
if __name__ == "__main__":
ap = ArgumentParser()
ap.add_argument('--enable', action="store_true", help="Enable tests")
ap.add_argument('--disable', action="store_true", help="Disable tests")
opts = ap.parse_args()
if opts.disable == opts.enable:
ap.print_help(stderr)
ap.exit(1)
elif opts.disable:
fn = skip_all_tests
else:
fn = unskip_all_tests
for p in find_all_test_files("tests"):
fn(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment