---
title: "Make the test setup refuse production · Agent Wranglers"
url: https://agentwranglers.se/playbook/testing/test-data-and-environments/tests-refuse-production
id: TST-58
level: Required
risk: data
updated: 2026-10-22
build: a2a6bdfed830
tokens: 1024
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Make the test setup refuse production

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Test data and environments](https://agentwranglers.se/playbook/testing/test-data-and-environments.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-22.

- ID: TST-58
- Level: Required
- Chapter: [Test data and environments](https://agentwranglers.se/playbook/testing/test-data-and-environments.md)
- Rule: 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.
- 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.
- Risk: Data leaked or lost
- How: 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](https://agentwranglers.se/playbook/payments/card-security-and-sca/test-and-live-keys.md) and [Never text or push customers from test systems](https://agentwranglers.se/playbook/notifications/channels-and-preferences/test-notification-sinks.md). Staging follows the same split, as [Keep a staging environment with generated data](https://agentwranglers.se/playbook/testing/test-data-and-environments/production-like-staging.md) says.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### Python example

```python
# 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

```js
// 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}`);
  }
}
```

## Sources

- [Django: The test database](https://docs.djangoproject.com/en/5.2/topics/testing/overview/)

## Related rules

- PAY-5 [Keep test and live modes apart](https://agentwranglers.se/playbook/payments/card-security-and-sca/test-and-live-keys.md) in Payments and billing
- NTF-8 [Never contact customers from test systems](https://agentwranglers.se/playbook/notifications/channels-and-preferences/test-notification-sinks.md) in Notifications
- TST-26 [Keep a staging environment with generated data](https://agentwranglers.se/playbook/testing/test-data-and-environments/production-like-staging.md) in Testing

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/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.

- Previous: [Keep a staging environment with generated data](https://agentwranglers.se/playbook/testing/test-data-and-environments/production-like-staging.md)
- [Test data and environments](https://agentwranglers.se/playbook/testing/test-data-and-environments.md)
