Agent Wranglers

TST-58Required

Make the test setup refuse production

Before the first test runs, check that every database, bucket and outside service address belongs to a test environment. Stop the run when any one does not.

How to do it

Test setup empties tables and loads fixtures. Pointed at production by a stale env file or a copied command, it deletes customer data in seconds. Give test databases a name the check can see, such as the test_ prefix that Django puts on its test databases. Also compare each host with a list of production hosts. Use only test keys for payment and message providers. See Keep test and live modes apart and Never text or push customers from test systems. Staging follows the same split, as Keep a staging environment with generated data says.

Examples

Python example

# conftest.py: runs before any test touches a database
import os
from urllib.parse import urlparse

PRODUCTION_HOSTS = {"db.example.com", "replica.example.com"}

def pytest_sessionstart(session):
    url = urlparse(os.environ["DATABASE_URL"])
    if url.hostname in PRODUCTION_HOSTS or not url.path.lstrip("/").startswith("test_"):
        raise SystemExit(f"Refusing to run tests against {url.hostname}{url.path}")

JavaScript example

// vitest.global-setup.js: runs once before the suite
const PRODUCTION_HOSTS = new Set(['db.example.com', 'replica.example.com']);

export default function refuseProduction() {
  const url = new URL(process.env.DATABASE_URL);
  if (PRODUCTION_HOSTS.has(url.hostname) || !url.pathname.slice(1).startsWith('test_')) {
    throw new Error(`Refusing to run tests against ${url.hostname}${url.pathname}`);
  }
}

How to check

Run the test command with the database address set to a production host. It exits with an error before it connects, and the production row counts are unchanged.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/test-data-and-environments/tests-refuse-production">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.