optimize-pytest

Measure and optimize slow pytest suites by ranking cumulative startup, fixture, and teardown costs.

7|5|Updated Jul 22, 2025
One-click install
npx skills add https://github.com/nebius/nebius-ps-services --skill optimize-pytest-nebius
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimize-pytest
Source: https://github.com/nebius/nebius-ps-services/tree/main/skills/optimize-pytest
Command: npx skills add https://github.com/nebius/nebius-ps-services --skill optimize-pytest-nebius

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Slow pytest suites delay developer feedback, but blind optimization often breaks test isolation or hides failures. This Skill diagnoses where time actually goes—startup, collection, fixture setup, test calls, and teardown—and applies only evidence-backed changes that preserve test selection and outcomes. ## Core Features & Use Cases - Phase-Based Measurement: Separates startup, collection, setup, call, and teardown costs using safe baseline commands, repeated sampling, and median/spread reporting. - Cumulative Cost Ranking: Ranks contributors by per-invocation cost multiplied by invocation count, so a 50ms fixture used by 5,000 tests is prioritized over one slow test. - Evidence-Gated Escalation: Treats pytest-xdist parallelism, pytest-testmon affected-test selection, CI sharding, coverage changes, and Pants adoption as escalations requiring measured justification. - Use Case: A developer whose CI test job takes 25 minutes uses this Skill to profile the suite, discovers an autouse database fixture rebuilding schema per test, switches to transaction rollback, and proves a like-for-like speedup with identical outcome counts. ## Quick Start Ask the agent to measure why your pytest suite is slow and recommend safe optimizations, for example: "Use $optimize-pytest to diagnose why the tests in my project take so long and suggest fixes."

Frequently Asked Questions about optimize-pytest

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

FAQPage Schema
How do I find out why my pytest suite is slow?

Run a timed collection baseline with --collect-only, then use --durations to attribute setup, call, and teardown costs per test. Rank contributors by per-invocation cost multiplied by invocation count rather than only looking at the single slowest test.

How to speed up pytest fixtures without breaking test isolation?

Widen fixture scope only after proving immutable sharing or deterministic per-test reset, such as database transactions that roll back each test. Separate expensive resource creation from mutable state reset before changing scope.

When should I use pytest-xdist for parallel test runs?

Adopt pytest-xdist only after serial measurements show execution architecture is the bottleneck. Benchmark bounded worker counts first, verify worker-specific resources like databases and ports, and avoid putting -n auto in global addopts until parallel safety is proven.

Does pytest-testmon replace running the full test suite?

No. Testmon selects tests affected by changed Python code for fast feedback, but it does not track static assets or external services. Keep a broad or full suite as the final correctness gate, especially after changes to conftest files, plugins, or shared libraries.

When should I not use this pytest optimization skill?

Do not use it for failing, hanging, or flaky tests whose cause is not established as performance, for Python project scaffolding, for generic code review, or for profiling production code. Those cases route to troubleshooting, scaffolding, or review workflows instead.