技能 python-testing-patterns
🧪

python-testing-patterns

安全 🌐 网络访问📁 文件系统访问⚙️ 外部命令🔑 环境变量

تنفيذ اختبارات Python باستخدام أنماط pytest

也可从以下获取: sickn33,wshobson

كتابة اختبارات للكود Python يمكن أن تكون مستهلكة للوقت وغير متسقة بدون أنماط واضحة. توفر هذه المهارة أنماط pytest جاهزة للاستخدام للfixtures والمحاكاة والكود غير المتزامن وسير عمل TDD لمساعدتك في بناء مجموعات اختبارات موثوقة بسرعة.

支持: Claude Codex Code(CC)
📊 69 充足
1

下载技能 ZIP

2

在 Claude 中上传

前往 设置 → 功能 → 技能 → 上传技能

3

开启并开始使用

测试它

正在使用“python-testing-patterns”。 Write a test for user authentication that checks login success and failure cases

预期结果:

  • Test login success with valid credentials
  • Test login failure with invalid password
  • Test account lockout after multiple failed attempts
  • Mock the database query to isolate authentication logic

正在使用“python-testing-patterns”。 Create a pytest fixture for testing file operations with temporary directories

预期结果:

  • Use pytest tmp_path fixture for temporary directory
  • Create test files with known content
  • Verify file creation and deletion
  • Clean up files after test completion

正在使用“python-testing-patterns”。 Set up property-based testing for a string manipulation function

预期结果:

  • Use hypothesis.given with string strategies
  • Test properties like reversibility
  • Test length preservation across random inputs
  • Generate hundreds of test cases automatically

安全审计

安全
v5 • 1/16/2026

This is a documentation-only skill containing educational markdown content about Python testing patterns. The file contains example code snippets for teaching purposes but no executable code that performs network calls, file system access, environment variable manipulation, or command execution. All 'findings' from the static analyzer are false positives caused by the analyzer misinterpreting markdown code fence delimiters as Ruby backticks, and test placeholder strings as actual secrets.

2
已扫描文件
1,085
分析行数
4
发现项
5
审计总数
审计者: claude 查看审计历史 →

质量评分

38
架构
100
可维护性
87
内容
20
社区
100
安全
83
规范符合性

你能构建什么

إعداد مجموعة اختبارات

إنشاء بنية اختبار جديدة مع fixtures والمحاكاة والتغطية لمشاريع Python.

كتابة اختبارات التكامل

بناء اختبارات التكامل لواجهات API وقواعد البيانات والخدمات الخارجية باستخدام أنماط pytest.

تكوين اختبارات CI/CD

إعداد خطوط اختبار آلية مع تقارير التغطية وعلامات الاختبار.

试试这些提示

اختبار الوحدة الأساسي
Write a pytest unit test for a function called calculate_total that takes a list of prices and returns the sum with tax. Include test cases for normal input, empty list, and single item.
إعداد Fixture
Create a pytest fixture for database testing. The fixture should connect to an in-memory SQLite database, create tables, yield a session, then clean up after the test.
محاكاة API الخارجية
Write tests for an API client that fetches user data. Use unittest.mock patch to mock the requests.get call. Test both success and 404 error cases.
الاختبار غير المتزامن
Write async tests for a function that fetches data from multiple URLs concurrently using asyncio.gather. Show how to use pytest-asyncio mark and fixtures with async setup.

最佳实践

  • استخدم أسماء اختبارات وصفية تشرح ما يتم اختباره والنتيجة المتوقعة
  • احتفظ بالاختبارات مستقلة بدون حالة مشتركة بين حالات الاختبار
  • اختبر الحالات الحدية وظروف الأخطاء، وليس المسارات السعيدة فقط

避免

  • تجنب تأكيدات متعددة في اختبار واحد عندما يمكن أن تكون اختبارات منفصلة
  • لا تجعل الاختبارات تعتمد على ترتيب التنفيذ أو الحالة المشتركة
  • تجنب القيم الثابتة التي يجب أن تأتي من fixtures أو التكوين

常见问题

ما إصدارات Python المدعومة؟
تعمل الأنماط مع Python 3.7+ و pytest 7.0+. تتطلب الأنماط غير المتزامنة pytest-asyncio.
ما الحد الأقصى لتعقيد الاختبار؟
تدعم الأنماط اختبارات الوحدة البسيطة لاختبارات التكامل المعقدة مع multiple fixtures و mocks.
هل يمكن لهذا التكامل مع كود الاختبار الموجود؟
نعم، الأنماط متوافقة مع إعدادات pytest الحالية ويمكن إضافتها بشكل تدريجي.
هل يتم التعامل مع بيانات الاختبار بشكل آمن؟
تستخدم الأنماط بيانات في الذاكرة وملفات مؤقتة. لا يتم تخزين أو نقل بيانات حساسة.
لماذا تفشل اختباراتي مع أخطاء الاستيراد؟
تأكد من تثبيت pytest، تبدأ ملفات الاختبار بـ test_، ومسار Python يشمل جذر مشروعك.
كيف يقارن هذا بإطار unittest؟
pytest أكثر حداثة مع fixtures و parametrize وتأكيدات أفضل. تستخدم الأنماط هنا ميزات pytest غير المتوفرة في unittest.