pytest

Write and organize Python tests with pytest fixtures, mocking, and parametrization.

1|Updated Aug 16, 2025
One-click install
npx skills add https://github.com/professor-moody/cloud-tools --skill pytest-professor-moody
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytest
Source: https://github.com/professor-moody/cloud-tools/tree/main/repos/prowler/skills/pytest
Command: npx skills add https://github.com/professor-moody/cloud-tools --skill pytest-professor-moody

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill streamlines the process of writing robust and efficient tests for Python applications using the pytest framework, ensuring code quality and reliability.

Core Features & Use Cases

  • Test Structure: Demonstrates basic test classes, methods, and assertions.
  • Fixtures: Explains how to use fixtures for setup/teardown and dependency injection, including different scopes.
  • Mocking: Shows how to use unittest.mock and MagicMock for isolating code under test.
  • Parametrization: Illustrates how to run tests with multiple data sets using @pytest.mark.parametrize.
  • Markers: Covers custom markers for test categorization and conditional skipping.
  • Async Tests: Provides examples for testing asynchronous functions.
  • Command-line Usage: Lists common pytest commands for running and filtering tests.
  • Use Case: When developing a new API endpoint in Python, use this Skill to quickly set up comprehensive unit and integration tests using fixtures, mocking, and parametrization to cover various success and failure scenarios.

Quick Start

Use the pytest skill to generate a basic test class for a Python function named calculate_sum that takes two arguments and returns their sum.

Frequently Asked Questions about pytest

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

FAQPage Schema
How do I write Python unit tests using pytest fixtures for setup and teardown?

Python unit tests use pytest fixtures to manage setup and teardown logic. Fixtures provide dependency injection and can be scoped to functions, classes, or sessions, ensuring maintainable test suites by isolating initialization code from test methods.

How do I parametrize Python tests to run them with multiple data sets?

Python tests are parametrized using the `@pytest.mark.parametrize` decorator. This mechanism allows a single test function to execute multiple times against different input data sets, maximizing coverage and reducing code duplication in test suites.

How does mocking work in pytest to isolate code under test?

Mocking in pytest uses `unittest.mock` and `MagicMock` to replace external dependencies. This isolates the code under test, allowing synchronous and asynchronous functions to be verified independently of their actual external service or database interactions.

Can I test asynchronous Python functions with pytest?

pytest supports testing asynchronous Python functions directly. The framework provides specific examples and command-line execution patterns to validate async codebases, ensuring asynchronous operations behave as expected without requiring separate testing tools.

How do I use custom markers in pytest to categorize and skip tests?

Custom markers in pytest categorize tests for targeted command-line execution and conditional skipping. By applying markers, you can filter specific test subsets, such as integration tests, and skip certain tests based on runtime conditions or environment configurations.

What is the best way to structure Python test classes and assertions in pytest?

Structuring Python tests in pytest involves defining basic test classes and methods with built-in assertions. This approach organizes related test cases cohesively, providing a clear hierarchy that simplifies maintenance and test suite navigation.