鱼皮AI导航
鱼皮AI导航
  • 主页
  • 社区
  • AI工具
  • AI知识库
  • AI提示词
  • MCP
  • Skills
  • AI学习资源
  • AI资讯
  • GitHub开源
登录/注册

pytest

提供 Python 测试代码的规范化编写能力,涵盖测试结构设计、依赖注入式固件管理、精准模拟外部服务行为、多组参数驱动验证、标签化分类执行及异步测试支持,适用于构建稳定可维护的自动化测试套件。

测试与安全
访问 GitHub
文档Skills.MD
name
pytest
description
Pytest testing patterns for Python. Trigger: When writing or refactoring pytest tests (fixtures, mocking, parametrize, markers). For Prowler-specific API/SDK testing conventions, also use prowler-test-api or prowler-test-sdk.
license
Apache-2.0
metadata{"author":"prowler-cloud","version":"1.0","scope":["root","sdk","api"],"auto_invoke":"Writing Python tests with pytest"}
allowed-tools
Read, Edit, Write, Glob, Grep, Bash, WebFetch, WebSearch, Task

Basic Test Structure

▼
python
复制代码
import pytest class TestUserService: def test_create_user_success(self): user = create_user(name="John", email="john@test.com") assert user.name == "John" assert user.email == "john@test.com" def test_create_user_invalid_email_fails(self): with pytest.raises(ValueError, match="Invalid email"): create_user(name="John", email="invalid")

Fixtures

▼
python
复制代码
import pytest @pytest.fixture def user(): """Create a test user.""" return User(name="Test User", email="test@example.com") @pytest.fixture def authenticated_client(client, user): """Client with authenticated user.""" client.force_login(user) return client # Fixture with teardown @pytest.fixture def temp_file(): path = Path("/tmp/test_file.txt") path.write_text("test content") yield path # Test runs here path.unlink() # Cleanup after test # Fixture scopes @pytest.fixture(scope="module") # Once per module @pytest.fixture(scope="class") # Once per class @pytest.fixture(scope="session") # Once per test session

conftest.py

▼
python
复制代码
# tests/conftest.py - Shared fixtures import pytest @pytest.fixture def db_session(): session = create_session() yield session session.rollback() @pytest.fixture def api_client(): return TestClient(app)

Mocking

▼
python
复制代码
from unittest.mock import patch, MagicMock class TestPaymentService: def test_process_payment_success(self): with patch("services.payment.stripe_client") as mock_stripe: mock_stripe.charge.return_value = {"id": "ch_123", "status": "succeeded"} result = process_payment(amount=100) assert result["status"] == "succeeded" mock_stripe.charge.assert_called_once_with(amount=100) def test_process_payment_failure(self): with patch("services.payment.stripe_client") as mock_stripe: mock_stripe.charge.side_effect = PaymentError("Card declined") with pytest.raises(PaymentError): process_payment(amount=100) # MagicMock for complex objects def test_with_mock_object(): mock_user = MagicMock() mock_user.id = "user-123" mock_user.name = "Test User" mock_user.is_active = True result = get_user_info(mock_user) assert result["name"] == "Test User"

Parametrize

▼
python
复制代码
@pytest.mark.parametrize("input,expected", [ ("hello", "HELLO"), ("world", "WORLD"), ("pytest", "PYTEST"), ]) def test_uppercase(input, expected): assert input.upper() == expected @pytest.mark.parametrize("email,is_valid", [ ("user@example.com", True), ("invalid-email", False), ("", False), ("user@.com", False), ]) def test_email_validation(email, is_valid): assert validate_email(email) == is_valid

Markers

▼
python
复制代码
# pytest.ini or pyproject.toml [tool.pytest.ini_options] markers = [ "slow: marks tests as slow", "integration: marks integration tests", ] # Usage @pytest.mark.slow def test_large_data_processing(): ... @pytest.mark.integration def test_database_connection(): ... @pytest.mark.skip(reason="Not implemented yet") def test_future_feature(): ... @pytest.mark.skipif(sys.platform == "win32", reason="Unix only") def test_unix_specific(): ... # Run specific markers # pytest -m "not slow" # pytest -m "integration"

Async Tests

▼
python
复制代码
import pytest @pytest.mark.asyncio async def test_async_function(): result = await async_fetch_data() assert result is not None

Commands

▼
bash
复制代码
pytest # Run all tests pytest -v # Verbose output pytest -x # Stop on first failure pytest -k "test_user" # Filter by name pytest -m "not slow" # Filter by marker pytest --cov=src # With coverage pytest -n auto # Parallel (pytest-xdist) pytest --tb=short # Short traceback

References

For general pytest documentation, see:

  • Official Docs: https://docs.pytest.org/en/stable/

For Prowler SDK testing with provider-specific patterns (moto, MagicMock), see:

  • Documentation: references/prowler-testing.md
测试与安全

快捷安装

复制到终端去安装 Skill

npxskillsaddprowler-cloud/prowler--skill"pytest"
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
🔥热门工具
朱雀AI检测朱雀AI检测
朱雀AI检测助手朱雀AI检测助手
LovartLovart
老鱼简历老鱼简历
WritePass官网WritePass官网
面多多面多多
CursorCursor
星月写作星月写作
朱雀AI检测助手朱雀AI检测助手
零感AI零感AI
Skills 推荐
debug
debug 是一个系统化定位软件缺陷根因的技能,适用于用户报告 'fix'、'broken'、'error' 等问题或测试意外失败时。它通过可验证的假设、最小复现、因果链分析和回归测试,确保在修复前准确定位根本原因。
webapp-testing
提供本地 Web 应用的端到端测试能力,支持页面导航、元素交互、表单填写、动态等待、断言验证、截图捕获、浏览器日志收集及网络请求拦截,适用于前端功能验证与 UI 行为调试。
tdd-process
严格遵循测试驱动开发的红绿重构循环,强制执行先写测试、失败要有意义、实现最小化、验证全覆盖的全流程,确保每次代码变更都由可执行的测试用例驱动并经编译、检查与全量验证确认。
smoke-test
自动化创建 Mastra 项目并完成 Studio 全流程冒烟测试,涵盖项目初始化、环境配置、本地服务启动及 Chrome 浏览器中对 Agents、Tools、Workflows、Scorers 等核心模块的功能验证与截图记录。
property-based-testing
通过生成随机输入验证代码在各类边界条件下的行为一致性,适用于序列化、解析、归一化及纯函数等场景,能有效发现隐含缺陷并提升测试覆盖率,尤其适合需要高可靠性的算法与智能合约验证。