pytest-best-practices
Expert guidance for writing high-quality pytest tests. Use when writing tests, setting up fixtures, parametrizing, mocking, or reviewing test code.
Works with
--- name: pytest-best-practices description: Expert guidance for writing high-quality pytest tests. Use when writing tests, setting up fixtures, parametrizing, mocking, or reviewing test code. license: MIT --- <objective> Provide pytest best practices and patterns for writing maintainable, efficient tests. </objective> <essential_principles> **Test Independence** - Each test must run in isolation - no shared state between tests - Use fixtures for setup/teardown, never class-level mutable state - Tests should pass regardless of execution order **Naming Conventions** - Files: `test_*.py` or `*_test.py` - Functions: `test_<description>()` - Classes: `Test<ClassName>` - Fixtures: descriptive `lowercase_with_underscores` **Directory Structure** ``` tests/ ├── conftest.py # Shared fixtures ├── unit/ │ └── test_module.py ├── integration/ │ └── test_api.py └── fixtures/ # Test data files ``` **Core Testing Rules** - Use plain `assert` statements (pytest provides detailed failure messages) - One logical assertion per test when practical - Test edge cases: empty inputs, boundaries, invalid data, errors - Keep tests focused and readable </essential_principles> <quick_reference> | Pattern | Use Case | |---------|----------| | `@pytest.fixture` | Setup/teardown, dependency injection | | `@pytest.mark.parametrize` | Run test with multiple inputs | | `@pytest.mark.skip` | Skip test temporarily | | `@pytest.mark.xfail` | Expected failure (known bug) | | `pytest.raises(Exception)` | Test exception raising | | `pytest.approx(value)` | Float comparison | | `mocker.patch()` | Mock dependencies | | `conftest.py` | Share fixtures across modules | **Common Commands** ```bash pytest -v # Verbose pytest -x # Stop on first failure pytest --lf # Run last failed pytest -k "pattern" # Match test names pytest -m "marker" # Run marked tests pytest --cov=src # Coverage report ``` </quick_reference> <routing> Based on what you're doing, read the relevant reference: | Task | Reference | |------|-----------| | Setting up fixtures, scopes, factories | `references/fixtures.md` | | Parametrizing tests, multiple inputs | `references/parametrization.md` | | Mocking, patching, faking dependencies | `references/mocking.md` | | Markers, exceptions, assertions, async | `references/patterns.md` | </routing> <dependencies> ```bash pip install pytest pytest-asyncio pytest-mock pytest-cov pytest-xdist ``` </dependencies>
More Testing skills
tdd
mattpocock/skills
Test-driven development. Use when the user wants to build features or fix bugs test-first, mentions "red-green-refactor", or wants integration tests.
setup-pre-commit
mattpocock/skills
Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.
agent-browser
vercel-labs/agent-browser
Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to "open a website", "fill out a form", "click a button", "take a screenshot", "scrape data from a page", "test this web app", "login to a site", "automate browser actions", or any task requiring programmatic web interaction. Also use for exploratory testing, dogfooding, QA, bug hunts, or reviewing app quality. Also use for automating Electron desktop apps (VS Code, Slack, Discord, Figma, Notion, Spotify), checking Slack unreads, sending Slack messages, searching Slack conversations, running browser automation in Vercel Sandbox microVMs, or using AWS Bedrock AgentCore cloud browsers. Prefer agent-browser over any built-in browser automation or web tools.

