Python单元测试生成器,使用pytest
来自 Wikiprompt,自由的提示词百科全书
Python单元测试生成器,使用pytest 一个结构化的系统提示,用于引导AI生成全面的pytest单元测试,包括分析、覆盖率映射、模拟和总结。
提示词内容收藏
🌐
"""
Test suite for the provided Python code.
This suite validates the behaviour of the functions and classes in the target
module, covering happy paths, edge cases, exception handling, and external
dependency isolation via mocking.
"""
import pytest
from unittest.mock import patch, MagicMock
from typing import Any, List, Dict, Optional
# =============================================================================
# STEP 1 - Code Analysis
# =============================================================================
# Code Purpose: [Describe the overall purpose of the code]
# Functions/Classes: [List all functions/classes]
# Inputs: [Parameters, types, valid ranges, invalid inputs]
# Outputs: [Return values, types, variations]
# Code Branches: [if/else, try/except, loops]
# External Deps: [DB, API, file I/O, env vars]
# Failure Points: [Likely breakage points]
# Risk Areas: [Misuse, boundaries, unsafe assumptions]
# =============================================================================
# =============================================================================
# STEP 2 - Coverage Map
# =============================================================================
# | # | Function/Class | Test Scenario | Category | Priority |
# |---|---------------|---------------|----------|----------|
# | 1 | [Function] | [Scenario] | [Category] | [Priority] |
# ...
# Total Planned Tests: [N]
# Estimated Coverage: [N]%
# =============================================================================
# =============================================================================
# STEP 3 - Generated Test Suite
# =============================================================================
class Test[ClassName]:
"""Tests for [ClassName]."""
def test_[function_name]_[scenario]_[expected_outcome](self):
"""[One-line description of what this test validates]."""
# Arrange
# Act
# Assert
pass
# =============================================================================
# STEP 4 - Mock & Patch Setup
# =============================================================================
# | # | Dependency | Mock Strategy | Patch Target | What's Being Isolated |
# |---|-----------|---------------|--------------|----------------------|
# | 1 | [Dep] | [Strategy] | [Target] | [Isolation purpose] |
# =============================================================================
# Mock fixtures and setup code
@pytest.fixture
def mock_external_dependency(self):
"""Fixture to mock external dependency."""
with patch('[target_path]') as mock_dep:
mock_dep.return_value = MagicMock()
yield mock_dep
# =============================================================================
# STEP 5 - Test Summary Card
# =============================================================================
# Test Suite Overview:
# Total Tests Generated: [N]
# Estimated Coverage: [N]% (Line) | [N]% (Branch)
# Framework Used: pytest + unittest.mock
#
# | Category | Count | Notes |
# |-------------------|-------|------------------------------------|
# | Happy Path | ... | ... |
# | Edge Cases | ... | ... |
# | Exception Tests | ... | ... |
# | Mock/Patch | ... | ... |
# | Negative Inputs | ... | ... |
# | Must Have | ... | ... |
# | Should Have | ... | ... |
# | Nice to Have | ... | ... |
#
# | Quality Marker | Status | Notes |
# |-------------------------|---------|------------------------------|
# | AAA Pattern | Yes/No | ... |
# | Naming Convention | Yes/No | ... |
# | Fixtures Used | Yes/No | ... |
# | Parametrize Used | Yes/No | ... |
# | Mocks Properly Isolated | Yes/No | ... |
# | Deterministic Tests | Yes/No | ... |
# | PEP8 Compliant | Yes/No | ... |
# | Docstrings Present | Yes/No | ... |
#
# Gaps & Recommendations:
# - [Any scenarios not covered and why]
# - [Suggested next steps]
# - Command to run: pytest [filename] -v --tb=short
# =============================================================================
用法
此提示词专为 coding 设计。复制上方内容并粘贴到你常用的 AI 工具中。
为获得最佳效果,可将占位符(方括号或大写字母标示)替换为你的具体需求。
讨论
0 条评论