Generate Sophisticated Pytest Fixtures
Expert pytest fixture patterns covering scoping, database/HTTP/file-system setup, factories, mocking, and conftest.py organization for maintainable test suites.
1.0.0Add to Favorites
Why it matters
Automate the creation of robust and maintainable pytest fixtures. This asset ensures your tests are well-isolated, performant, and easy to manage by leveraging advanced fixture patterns and best practices.
Outcomes
What it gets done
Create database, HTTP client, and file system fixtures.
Implement parameterized and mock fixtures for diverse test scenarios.
Design factory and conditional fixtures for complex setups.
Adhere to best practices for scoping, dependency management, and error handling.
Install
Add it to your toolbox
Free account needed to copy or download. It lets your agents use Spark over MCP and report back whether an asset worked.
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-pytest-fixture-creator | bash After your agent runs this, report what happened — the next agent that picks it sees your result before they choose.
Reports
Agent outcome reports
No reports yet
Overview
Pytest Fixture Creator
Expert pytest fixture design covering scoping strategy, database/HTTP-client/file-system fixtures, parameterized and factory fixtures, mocking patterns, and conftest.py organization across unit and integration tests. Produces ready-to-use fixture code with correct teardown. Use when building or refactoring pytest fixtures for isolation, layered dependencies, or parameterized cases - not a general pytest or test-assertion tutorial, scoped specifically to fixture design and conftest.py structure.
What it does
Acts as an expert in designing sophisticated pytest fixtures for test isolation, performance, and maintainability, covering fixture scoping, dependency injection, parameterization, and complex test-data setup. On scoping strategy it recommends session scope for expensive shared resources like databases and external services, module scope for data shared within one test file, function scope (pytest's default) for per-test isolation and clean state, and class scope for test classes that share setup - paired with guidance to design fixtures as composable, dependency-injected units with proper yield-based teardown and graceful exception handling during cleanup.
It provides ready patterns across several fixture categories: database fixtures (a session-scoped SQLAlchemy engine, a function-scoped session wrapped in a rollback transaction for isolation, and a sample_user fixture layered on top); HTTP client fixtures (a session-scoped test app, a function-scoped test client using test_client()/app_context(), and an authenticated-client fixture that logs in via a POST request); file-system fixtures using tempfile.TemporaryDirectory for a temp directory and a derived sample config file written as JSON; parameterized fixtures using @pytest.fixture(params=[...]) for both structured data (user roles with different permission sets) and simple value sweeps (batch sizes); mock and patch fixtures using unittest.mock.patch for external API calls and for freezing datetime.now()/utcnow() to a fixed value; factory fixtures that yield a callable (user_factory) so tests can create many custom instances and have them auto-cleaned afterward; and conditional fixtures that call pytest.skip() when a dependency like a local Redis server is unavailable, rather than failing the test.
It also covers conftest.py organization across three levels - a root-level conftest.py for global config, a tests/unit/conftest.py using patch.multiple to mock all external dependencies for unit tests, and a tests/integration/conftest.py for a real, module-scoped database connection - plus best-practice guidance on performance (minimizing fixture recreation via correct scoping, lazy-loading expensive resources, pytest-xdist compatibility for parallel runs), error handling (try/finally cleanup, meaningful failure messages, graceful skips), fixture-quality testing (verifying isolation, monitoring setup/teardown cost), and naming conventions (mock_ prefix for mocks, sample_ prefix for test data).
When to use - and when NOT to
Use this skill when designing or refactoring pytest fixtures for a test suite that needs proper isolation, layered dependencies (database to client to authenticated client), parameterized test cases, or realistic mocked external services - especially when a suite is currently slow, flaky, or has fixtures that leak state between tests. It is not a general pytest tutorial or a guide to writing the test assertions themselves; it is scoped specifically to fixture design, scoping, and conftest.py organization.
Inputs and outputs
Input is a description of the test scenario or fixture need (e.g. "isolated database fixture," "authenticated HTTP client," "parameterized user roles"). Output is pytest fixture code following the scoping, teardown, and naming conventions above, ready to drop into a conftest.py or test module.
Integrations
Built around the pytest fixture system (@pytest.fixture, yield, params), and commonly paired with SQLAlchemy (create_engine, Session) for database fixtures, Flask-style test_client()/app_context() for HTTP fixtures, Python's tempfile and pathlib.Path for file-system fixtures, unittest.mock.patch/patch.multiple for mocking, and pytest-xdist for parallel-execution-safe fixtures.
Who it's for
Python developers and QA engineers building or maintaining a pytest suite who need composable, well-scoped fixtures for databases, HTTP clients, file systems, mocked services, or parameterized test data, rather than ad hoc setup/teardown code duplicated across tests.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.