python-testing-patterns
تنفيذ اختبارات Python باستخدام أنماط pytest
كتابة اختبارات للكود Python يمكن أن تكون مستهلكة للوقت وغير متسقة بدون أنماط واضحة. توفر هذه المهارة أنماط pytest جاهزة للاستخدام للfixtures والمحاكاة والكود غير المتزامن وسير عمل TDD لمساعدتك في بناء مجموعات اختبارات موثوقة بسرعة.
下载技能 ZIP
在 Claude 中上传
前往 设置 → 功能 → 技能 → 上传技能
开启并开始使用
测试它
正在使用“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
安全审计
安全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.
风险因素
🌐 网络访问 (17)
📁 文件系统访问 (1)
⚙️ 外部命令 (37)
🔑 环境变量 (23)
质量评分
你能构建什么
إعداد مجموعة اختبارات
إنشاء بنية اختبار جديدة مع 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.
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.
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 أو التكوين