test-isolation-patterns

Diagnose pytest failures caused by shared global state and apply fixture-based isolation patterns.

Updated Mar 30, 2026
One-click install
npx skills add https://github.com/ZaxbyHub/ragappv3 --skill test-isolation-patterns-zaxbyhub
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: test-isolation-patterns
Source: https://github.com/ZaxbyHub/ragappv3/tree/main/.opencode/skills/test-isolation-patterns
Command: npx skills add https://github.com/ZaxbyHub/ragappv3 --skill test-isolation-patterns-zaxbyhub

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Tests that pass in isolation but fail in combined pytest sessions are usually victims of shared global state—module-level connection pools, user caches, rate limiters, and CSRF tokens that leak between tests. This Skill documents the observed pollution paths in this repository and provides concrete fixture patterns to make tests reliable in any execution order. ## Core Features & Use Cases - Pollution Path Catalog: Documents four concrete leak sources—_pool_cache in app/models/database.py, _ACTIVE_USER_CACHE in app/api/deps.py, the limiter singleton, and CSRF token state. - Mitigation Patterns: Provides four ready-to-use patterns including autouse reset fixtures, per-test pool creation with dependency overrides, function-scoped fixtures, and rate limiter resets. - Diagnostic Workflow: Gives a step-by-step procedure for reproducing order-dependent failures by running tests in isolation versus combined sessions and grepping for shared globals. - Use Case: When test_vaults.py passes alone but fails in the full suite, use the autouse fixture pattern to clear _pool_cache and _ACTIVE_USER_CACHE between tests. ## Quick Start Ask the assistant to diagnose why a test passes alone but fails in the full pytest suite and recommend an isolation fixture.

Frequently Asked Questions about test-isolation-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why do pytest tests pass alone but fail when run together?▼

Tests fail in combined sessions when they share untracked global state such as module-level connection pools, caches, or singletons. A previous test leaves stale entries that the next test inherits, causing unexpected behavior.

How do I reset module-level state between pytest tests?▼

Use an autouse fixture in conftest.py that yields, then clears shared state in teardown. For example, close all pools in `_pool_cache`, clear the dict, and reset `_ACTIVE_USER_CACHE` after every test.

How do I isolate a singleton rate limiter in pytest?▼

Create an autouse fixture that calls `limiter.reset()` before and after each test. This prevents request bursts in one test from exhausting the quota available to subsequent tests.

How do I find which test is polluting global state?▼

Run the failing test in isolation, then run it combined with a suspected polluting test. Grep the codebase for shared globals like `_pool_cache`, `_ACTIVE_USER_CACHE`, or `limiter` to identify leak sources.

What are common pytest isolation anti-patterns to avoid?▼

Avoid mutable module-level dicts without reset hooks, singletons without explicit teardown, class-level attributes shared across instances, and global registries like dependency_overrides that persist between tests.