$npx -y skills add wshobson/agents --skill temporal-python-testingTest 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.
| 1 | # Temporal Python Testing Strategies |
| 2 | |
| 3 | Comprehensive testing approaches for Temporal workflows using pytest, progressive disclosure resources for specific testing scenarios. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - **Unit testing workflows** - Fast tests with time-skipping |
| 8 | - **Integration testing** - Workflows with mocked activities |
| 9 | - **Replay testing** - Validate determinism against production histories |
| 10 | - **Local development** - Set up Temporal server and pytest |
| 11 | - **CI/CD integration** - Automated testing pipelines |
| 12 | - **Coverage strategies** - Achieve ≥80% test coverage |
| 13 | |
| 14 | ## Testing Philosophy |
| 15 | |
| 16 | **Recommended Approach** (Source: docs.temporal.io/develop/python/testing-suite): |
| 17 | |
| 18 | - Write majority as integration tests |
| 19 | - Use pytest with async fixtures |
| 20 | - Time-skipping enables fast feedback (month-long workflows → seconds) |
| 21 | - Mock activities to isolate workflow logic |
| 22 | - Validate determinism with replay testing |
| 23 | |
| 24 | **Three Test Types**: |
| 25 | |
| 26 | 1. **Unit**: Workflows with time-skipping, activities with ActivityEnvironment |
| 27 | 2. **Integration**: Workers with mocked activities |
| 28 | 3. **End-to-end**: Full Temporal server with real activities (use sparingly) |
| 29 | |
| 30 | ## Available Resources |
| 31 | |
| 32 | This skill provides detailed guidance through progressive disclosure. Load specific resources based on your testing needs: |
| 33 | |
| 34 | ### Unit Testing Resources |
| 35 | |
| 36 | **File**: `resources/unit-testing.md` |
| 37 | **When to load**: Testing individual workflows or activities in isolation |
| 38 | **Contains**: |
| 39 | |
| 40 | - WorkflowEnvironment with time-skipping |
| 41 | - ActivityEnvironment for activity testing |
| 42 | - Fast execution of long-running workflows |
| 43 | - Manual time advancement patterns |
| 44 | - pytest fixtures and patterns |
| 45 | |
| 46 | ### Integration Testing Resources |
| 47 | |
| 48 | **File**: `resources/integration-testing.md` |
| 49 | **When to load**: Testing workflows with mocked external dependencies |
| 50 | **Contains**: |
| 51 | |
| 52 | - Activity mocking strategies |
| 53 | - Error injection patterns |
| 54 | - Multi-activity workflow testing |
| 55 | - Signal and query testing |
| 56 | - Coverage strategies |
| 57 | |
| 58 | ### Replay Testing Resources |
| 59 | |
| 60 | **File**: `resources/replay-testing.md` |
| 61 | **When to load**: Validating determinism or deploying workflow changes |
| 62 | **Contains**: |
| 63 | |
| 64 | - Determinism validation |
| 65 | - Production history replay |
| 66 | - CI/CD integration patterns |
| 67 | - Version compatibility testing |
| 68 | |
| 69 | ### Local Development Resources |
| 70 | |
| 71 | **File**: `resources/local-setup.md` |
| 72 | **When to load**: Setting up development environment |
| 73 | **Contains**: |
| 74 | |
| 75 | - Docker Compose configuration |
| 76 | - pytest setup and configuration |
| 77 | - Coverage tool integration |
| 78 | - Development workflow |
| 79 | |
| 80 | ## Quick Start Guide |
| 81 | |
| 82 | ### Basic Workflow Test |
| 83 | |
| 84 | ```python |
| 85 | import pytest |
| 86 | from temporalio.testing import WorkflowEnvironment |
| 87 | from temporalio.worker import Worker |
| 88 | |
| 89 | @pytest.fixture |
| 90 | async def workflow_env(): |
| 91 | env = await WorkflowEnvironment.start_time_skipping() |
| 92 | yield env |
| 93 | await env.shutdown() |
| 94 | |
| 95 | @pytest.mark.asyncio |
| 96 | async def test_workflow(workflow_env): |
| 97 | async with Worker( |
| 98 | workflow_env.client, |
| 99 | task_queue="test-queue", |
| 100 | workflows=[YourWorkflow], |
| 101 | activities=[your_activity], |
| 102 | ): |
| 103 | result = await workflow_env.client.execute_workflow( |
| 104 | YourWorkflow.run, |
| 105 | args, |
| 106 | id="test-wf-id", |
| 107 | task_queue="test-queue", |
| 108 | ) |
| 109 | assert result == expected |
| 110 | ``` |
| 111 | |
| 112 | ### Basic Activity Test |
| 113 | |
| 114 | ```python |
| 115 | from temporalio.testing import ActivityEnvironment |
| 116 | |
| 117 | async def test_activity(): |
| 118 | env = ActivityEnvironment() |
| 119 | result = await env.run(your_activity, "test-input") |
| 120 | assert result == expected_output |
| 121 | ``` |
| 122 | |
| 123 | ## Coverage Targets |
| 124 | |
| 125 | **Recommended Coverage** (Source: docs.temporal.io best practices): |
| 126 | |
| 127 | - **Workflows**: ≥80% logic coverage |
| 128 | - **Activities**: ≥80% logic coverage |
| 129 | - **Integration**: Critical paths with mocked activities |
| 130 | - **Replay**: All workflow versions before deployment |
| 131 | |
| 132 | ## Key Testing Principles |
| 133 | |
| 134 | 1. **Time-Skipping** - Month-long workflows test in seconds |
| 135 | 2. **Mock Activities** - Isolate workflow logic from external dependencies |
| 136 | 3. **Replay Testing** - Validate determinism before deployment |
| 137 | 4. **High Coverage** - ≥80% target for production workflows |
| 138 | 5. **Fast Feedback** - Unit tests run in milliseconds |
| 139 | |
| 140 | ## How to Use Resources |
| 141 | |
| 142 | **Load specific resource when needed**: |
| 143 | |
| 144 | - "Show me unit testing patterns" → Load `resources/unit-testing.md` |
| 145 | - "How do I mock activities?" → Load `resources/integration-testing.md` |
| 146 | - "Setup local Temporal server" → Load `resources/local-setup.md` |
| 147 | - "Validate determinism" → Load `resources/replay-testing.md` |
| 148 | |
| 149 | ## Additional References |
| 150 | |
| 151 | - Python SDK Testing: docs.temporal.io/develop/python/testing-suite |
| 152 | - Testing Patterns: github.com/temporalio/temporal/blob/main/docs/development/testing.md |
| 153 | - Python Samples: github.com/temporalio/samples-python |