write-tests

Guide writing pytest unit tests for Python code with fixtures and mocking.

561|190|Updated May 16, 2024
One-click install
npx skills add https://github.com/microsoft/semantic-link-labs --skill write-tests-microsoft
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: write-tests
Source: https://github.com/microsoft/semantic-link-labs/tree/main/.claude/skills/write-tests
Command: npx skills add https://github.com/microsoft/semantic-link-labs --skill write-tests-microsoft

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance and examples for writing effective unit tests for Python code, ensuring code quality and reliability.

Core Features & Use Cases

  • Test Framework Guidance: Details on using pytest, including file structure and naming conventions.
  • Test Writing Examples: Demonstrates how to test functions, handle errors, use fixtures, and mock dependencies.
  • Use Case: When developing a new feature in the semantic-link-labs library, use this Skill to learn how to write tests that cover its functionality, edge cases, and potential errors.

Quick Start

Use the write-tests skill to generate a basic pytest test function for a Python function named calculate_sum that takes two arguments.

Frequently Asked Questions about write-tests

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

FAQPage Schema
How do I write Python unit tests using pytest?

To write Python unit tests using pytest, you define test functions in files following pytest naming conventions, using assertions to verify functional correctness. Test structure should cover functions, edge cases, and potential errors.

How do I mock external dependencies in pytest?

To mock external dependencies in pytest, you replace real dependencies with mock objects during test execution. This isolates the code being tested, ensuring functional correctness without relying on external services or state.

How do I use fixtures and parameterization in Python testing?

Using fixtures and parameterization in Python testing allows you to reuse setup code and run the same test across multiple inputs. Fixtures provide a baseline for tests, while parameterization maps inputs to expected outcomes.

Does pytest work well for testing new Python library features?

Yes, pytest works well for testing new Python library features. It provides the necessary structure to write tests covering functionality, edge cases, and error handling, ensuring robust code quality during development.

What is the best way to handle errors in pytest test cases?

The best way to handle errors in pytest test cases is to use assertions to explicitly verify expected exceptions. Structuring tests to catch and assert specific errors ensures your code maintains functional correctness under adverse conditions.