pytest

Guide Python testing with pytest fixtures, mocking, parametrization, and markers.

2|Updated Aug 2, 2023
One-click install
npx skills add https://github.com/ic-facet/gestion-programas-asignatura --skill pytest-ic-facet
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytest
Source: https://github.com/ic-facet/gestion-programas-asignatura/tree/main/.cursor/skills/pytest
Command: npx skills add https://github.com/ic-facet/gestion-programas-asignatura --skill pytest-ic-facet

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers write robust and efficient Python tests by providing guidance on fixtures, mocking, parametrization, and markers, ensuring code quality and reliability.

Core Features & Use Cases

  • Test Structure: Demonstrates basic test writing patterns.
  • Fixtures: Shows how to create reusable test setup and teardown logic.
  • Mocking: Illustrates how to isolate code under test by mocking external dependencies.
  • Parametrization: Enables running the same test with multiple data inputs.
  • Markers: Helps categorize and selectively run tests (e.g., slow, integration).
  • Use Case: When developing a new API endpoint, use pytest fixtures to set up a test database and an API client, then use parametrization to test various valid and invalid input scenarios for the endpoint.

Quick Start

Use the pytest skill to create a fixture that provides a temporary file for testing.

Frequently Asked Questions about pytest

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

FAQPage Schema
How do I create reusable setup and teardown logic for Python testing?

Use pytest fixtures to create reusable test setup and teardown logic. Fixtures manage temporary resources like database connections or files, ensuring your Python test suite remains maintainable and scalable.

Can I run the same Python test with multiple data inputs?

Yes, you can run the same Python test with multiple data inputs using parametrization. Parametrization enables executing the same test function against different data sets, reducing code duplication and ensuring comprehensive coverage of valid and invalid input scenarios.

How do I isolate Python code under test from external dependencies?

Isolate Python code under test by mocking external dependencies with unittest.mock. Mocking allows you to replace real external services or complex modules with controlled dummy objects, ensuring tests run reliably and focus solely on the logic being tested.

What is the best way to categorize and selectively run Python tests?

The best way to categorize and selectively run Python tests is by applying custom markers. Markers help label tests as slow, integration, or specific features, allowing you to filter and execute targeted subsets of your test suite efficiently.

Does pytest support asynchronous testing for Python applications?

Yes, pytest supports asynchronous testing for Python applications. The framework provides comprehensive guidance on advanced patterns including asynchronous testing, facilitating the creation of robust tests for complex async applications.

How do I test an API endpoint with valid and invalid input scenarios?

Use pytest fixtures to set up a test database and API client, then apply parametrization to test various valid and invalid input scenarios for the endpoint. This pattern ensures isolated, comprehensive API endpoint testing.