pytest
Best practices for writing and organizing tests with pytest including fixtures, parametrize, and plugins.
Works with
--- name: pytest description: Best practices for writing and organizing tests with pytest including fixtures, parametrize, and plugins. license: MIT --- # Skill: pytest Best practices for writing and organizing tests with pytest including fixtures, parametrize, and plugins. ## When to Use Apply this skill when writing and organizing tests with pytest — fixtures, parametrize, markers, plugins, and test structure. ## Test Organization - Place tests in a `tests/` directory mirroring the source structure. - Name test files `test_<module>.py` and test functions `test_<behavior>()`. - Group related tests in classes only when they share fixtures/setup. ## Fixtures - Define fixtures at the narrowest scope needed (`function` > `class` > `module` > `session`). - Use `conftest.py` for shared fixtures; put it at the appropriate directory level. - Prefer factory fixtures over complex fixture inheritance. - Use `yield` fixtures for setup/teardown; prefer `tmp_path` over `tempfile`. ## Parametrize - Use `@pytest.mark.parametrize` for data-driven tests with multiple inputs. - Give test IDs (`ids=...`) for readable test output. - Combine `parametrize` with fixtures for cross-product testing. ## Assertions - Use plain `assert` statements — pytest rewrites them for clear failure messages. - Use `pytest.raises(ExceptionType, match=...)` for exception testing. - Use `pytest.approx()` for floating-point comparisons. ## Plugins - Common plugins: `pytest-cov`, `pytest-mock`, `pytest-asyncio`, `pytest-xdist`, `pytest-timeout`. - Use `pytest-mock`'s `mocker` fixture over raw `unittest.mock.patch`. ## Pitfalls - Don't use `session`-scoped fixtures for mutable state. - Don't assert on implementation details — test observable behavior. - Avoid test interdependence; each test should be runnable in isolation.
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.

