python-testing

Guide pytest testing strategies with fixtures, parametrization, mocking, and async tests.

Updated Apr 11, 2023
One-click install
npx skills add https://github.com/salverius-tech/dotfiles --skill python-testing-salverius-tech
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-testing
Source: https://github.com/salverius-tech/dotfiles/tree/main/home/dot_claude/skills/python-testing
Command: npx skills add https://github.com/salverius-tech/dotfiles --skill python-testing-salverius-tech

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance and best practices for writing robust and efficient tests for Python applications using pytest, covering advanced features and common pitfalls.

Core Features & Use Cases

  • Pytest Fixtures: Learn about fixture scopes, setup/teardown, and dependency injection.
  • Parametrization: Efficiently test multiple inputs and scenarios.
  • Async Testing: Write and test asynchronous Python code.
  • Mocking: Isolate components and simulate external dependencies.
  • Exception Handling: Assert that specific exceptions are raised.
  • Configuration: Understand pyproject.toml for pytest setup.
  • Use Case: When developing a new API endpoint, use this Skill to learn how to set up fixtures for database connections, mock external API calls, and write parametrized tests to cover various request scenarios and expected responses.

Quick Start

Activate this skill when you need to write or refactor pytest tests for your Python project.

Frequently Asked Questions about python-testing

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

FAQPage Schema
How do I write parametrized tests in pytest to cover multiple input scenarios?

Parametrized tests in pytest allow you to run a single test function against multiple inputs and expected outputs. By using the @pytest.mark.parametrize decorator, you efficiently test various scenarios without duplicating test code.

What is the best way to mock external API calls during Python testing?

Mocking external API calls during Python testing isolates your components by simulating external dependencies. Using pytest with mocking techniques allows you to assert behavior and validate responses without requiring live network connections or actual external services.

How do pytest fixtures work for managing database connections and setup?

Pytest fixtures provide a powerful way to manage setup and teardown logic, including database connections. They support dependency injection and various scopes, ensuring your test environment initializes cleanly and efficiently before test execution.

Can I test asynchronous Python code using pytest?

Yes, you can test asynchronous Python code using pytest. The framework provides support for asyncio, enabling you to write and execute tests for asynchronous functions and workflows to ensure your non-blocking code operates reliably.

How do I configure pytest settings using pyproject.toml?

Configuring pytest via pyproject.toml centralizes your testing setup within your project metadata. You define test paths, markers, and execution options in this file to standardize your Python testing workflow across development environments.

How do I assert that my Python code raises specific exceptions in pytest?

Exception handling in pytest uses the pytest.raises context manager to assert that specific exceptions are raised during execution. This ensures your code fails gracefully and correctly triggers expected errors under defined conditions.