temporal-python-testing
它能做什么
这个工具可以帮你测试运行在 Temporal 平台上的程序(称为“工作流”)。它允许模拟时间流逝来加快测试速度,也可以把代码的特定部分单独拿出来测试。
适用场景
- ✓当你想测试“工作流”是否正常工作时。
- ✓当你想测试依赖系统其他部分的“工作流”时。
- ✓当你想确保“工作流”在不同情况下表现一致时。
技能链接
https://github.com/wshobson/agents/blob/main/plugins/backend-development/skills/temporal-python-testing/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我怎样模拟时间快速流逝,来测试我的工作流?”
每周 5 个新技能,1 分钟读完。
工作原理
它使用 pytest 库来组织测试。为了加快时间,它会“跳过”时间,而不是真的等待。为了单独测试某些部分,它会用假的版本替换依赖项(称为“模拟对象”,即 mocks)。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Temporal Python Testing Strategies
Comprehensive testing approaches for Temporal workflows using pytest, progressive disclosure resources for specific testing scenarios.
When to Use This Skill
- Unit testing workflows - Fast tests with time-skipping
- Integration testing - Workflows with mocked activities
- Replay testing - Validate determinism against production histories
- Local development - Set up Temporal server and pytest
- CI/CD integration - Automated testing pipelines
- Coverage strategies - Achieve ≥80% test coverage
Testing Philosophy
Recommended Approach (Source: docs.temporal.io/develop/python/testing-suite):
- Write majority as integration tests
- Use pytest with async fixtures
- Time-skipping enables fast feedback (month-long workflows → seconds)
- Mock activities to isolate workflow logic
- Validate determinism with replay testing
Three Test Types:
- Unit: Workflows with time-skipping, activities with ActivityEnvironment
- Integration: Workers with mocked activities
- End-to-end: Full Temporal server with real activities (use sparingly)
Available Resources
This skill provides detailed guidance through progressive disclosure. Load specific resources based on your testing needs:
Unit Testing Resources
File: resources/unit-testing.md
When to load: Testing individual workflows or activities in isolation
Contains:
- WorkflowEnvironment with time-skipping
- ActivityEnvironment for activity testing
- Fast execution of long-running workflows
- Manual time advancement patterns
- pytest fixtures and patterns
Integration Testing Resources
File: resources/integration-testing.md
When to load: Testing workflows with mocked external dependencies
Contains:
- Activity mocking strategies
- Error injection patterns
- Multi-activity workflow testing
- Signal and query testing
- Coverage strategies
Replay Testing Resources
File: resources/replay-testing.md
When to load: Validating determinism or deploying workflow changes
Contains:
- Determinism validation
- Production history replay
- CI/CD integration patterns
- Version compatibility testing
Local Development Resources
File: resources/local-setup.md
When to load: Setting up development environment
Contains:
- Docker Compose configuration
- pytest setup and configuration
- Coverage tool integration
- Development workflow
Quick Start Guide
Basic Workflow Test
import pytest
from temporalio.testing import WorkflowEnvironment
from temporalio.worker import Worker
@pytest.fixture
async def workflow_env():
env = await WorkflowEnvironment.start_time_skipping()
yield env
await env.shutdown()
@pytest.mark.asyncio
async def test_workflow(workflow_env):
async with Worker(
workflow_env.client,
task_queue="test-queue",
workflows=[YourWorkflow],
activities=[your_activity],
):
result = await workflow_env.client.execute_workflow(
YourWorkflow.run,
args,
id="test-wf-id",
task_queue="test-queue",
)
assert result == expected
Basic Activity Test
from temporalio.testing import ActivityEnvironment
async def test_activity():
env = ActivityEnvironment()
result = await env.run(your_activity, "test-input")
assert result == expected_output
Coverage Targets
Recommended Coverage (Source: docs.temporal.io best practices):
- Workflows: ≥80% logic coverage
- Activities: ≥80% logic coverage
- Integration: Critical paths with mocked activities
- Replay: All workflow versions before deployment
Key Testing Principles
- Time-Skipping - Month-long workflows test in seconds
- Mock Activities - Isolate workflow logic from external dependencies
- Replay Testing - Validate determinism before deployment
- High Coverage - ≥80% target for production workflows
- Fast Feedback - Unit tests run in milliseconds
How to Use Resources
Load specific resource when needed:
- "Show me unit testing patterns" → Load
resources/unit-testing.md - "How do I mock activities?" → Load
resources/integration-testing.md - "Setup local Temporal server" → Load
resources/local-setup.md - "Validate determinism" → Load
resources/replay-testing.md
Additional References
- Python SDK Testing: docs.temporal.io/develop/python/testing-suite
…
常见问题
什么是“时间跳跃”?
这是一种让测试运行得更快的技术,它能在几秒钟内模拟出几天或几个月过去的效果,无需等待真实时间流逝。
什么是“模拟活动”?
这些是你的“工作流”所调用的代码部分(即活动)的假版本。这样就可以只专注测试“工作流”本身的逻辑,而不需要那些其他部分真正运行起来。
什么是“重放测试”?
这种测试会利用“工作流”以往的运行历史,让它重新执行一遍。这有助于确保“工作流”保持一致,不会意外改变行为。
详情
作者的原始描述
Test Temporal workflows with pytest, time-skipping, and mocking strategies. Covers unit testing, integration testing, replay testing, and local development setup. Use when implementing Temporal workflow tests or debugging test failures.
repo: wshobson/agents
热度
- GitHub 星标
- 41k
- 排名
- #6642
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …