Inspired by Adam Johnson's test for pending migrations, and of course in conversation with my own love of weird tests, I offer a similar concept: a test for finding stray print statements in your codebase, with a ratchet array for stuff to ignore.

import glob

PATH = "**/*.py"

irrelevant_paths = (
    "/commands/",
    "node_modules",
)

def test_no_print_statements() -> None:
    all_files = glob.glob(PATH, recursive=True)
    relevant_files = [
        filename for filename in all_files
        if not any(
            irrelevant_path in filename
            for irrelevant_path in irrelevant_paths
        )
    ]
    files_with_print = [
        filename for filename in relevant_files
        if "print(" in open(filename).read()
    ]
    assert not files_with_print, f"Print statements found in {files_with_print}"

I anticipate a concern being "this is slow! you're opening thousands of files!", to which I reply: this test is faster than any other test you have that touches a database. That being said, I'm sure there are edge cases or ways to improve it, so please let me know!

Lightning bolt
About the author

I'm Justin Duke — a software engineer, writer, and founder. I currently work as the CEO of Buttondown, the best way to start and grow your newsletter, and as a partner at Third South Capital.

Lightning bolt
Greatest hits

Lightning bolt
Elsewhere

Lightning bolt
Don't miss the next essay

Get a monthly roundup of everything I've written: no ads, no nonsense.