You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-functions/durable/durable-functions-unit-testing-python.md
+29-47Lines changed: 29 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,15 +9,13 @@ ms.author: azfuncdf
9
9
10
10
# Unit Testing Durable Functions in Python
11
11
12
-
## Introduction
13
-
Unit testing Durable Functions is essential to ensure the correctness of individual components without relying on an actual Azure environment. By writing effective unit tests, developers can catch errors early, improve code maintainability, and increase confidence in their implementations.
12
+
Unit testing is an important part of modern software development practices. Unit tests verify business logic behavior and protect from introducing unnoticed breaking changes in the future. Durable Functions can easily grow in complexity so introducing unit tests will help to avoid breaking changes. The following sections explain how to unit test the three function types - Orchestration client, orchestrator, and entity functions.
14
13
15
-
This guide provides an overview of unit testing Durable Functions in Python, covering the key components: starter functions, orchestrators, activity functions, and entity functions. It includes best practices and sample test cases to help you write robust and maintainable tests for your Durable Functions.
16
-
17
-
[!NOTE]
18
-
This guide applies only to Durable Functions apps written in the [Python v2 programming model](../functions-reference-python.md).
14
+
> [!NOTE]
15
+
> This guide applies only to Durable Functions apps written in the [Python v2 programming model](../functions-reference-python.md).
19
16
20
17
## Prerequisites
18
+
21
19
The examples in this article require knowledge of the following concepts and frameworks:
22
20
23
21
* Unit testing
@@ -26,17 +24,18 @@ The examples in this article require knowledge of the following concepts and fra
To test Durable Functions, it's crucial to set up a proper test environment. This includes creating a test directory and installing unittest into your Python environment. For more info, see the [Azure Functions Python unit testing overview](../functions-reference-python.md#unit-testing).
30
29
31
-
## Testing Durable Clients
32
-
Durable Client functions initiate orchestrations and external events. To test a client function:
30
+
## Unit testing trigger functions
31
+
32
+
Trigger functions, often referred to as _client_ functions, initiate orchestrations and external events. To test these functions:
33
33
34
34
- Mock the `DurableOrchestrationClient` to simulate orchestration execution and status management.
35
-
-Replace`DurableOrchestrationClient` methods such as `start_new`, `get_status`, or `raise_event` with mock functions that return expected values.
35
+
-Assign`DurableOrchestrationClient` methods such as `start_new`, `get_status`, or `raise_event` with mock functions that return expected values.
36
36
- Invoke the client function directly with a mocked client as well as other necessary inputs such as a `req` (HTTP request object) for HTTP trigger client functions.
37
37
- Use assertions and `unittest.mock` tools to verify expected orchestration start behavior, parameters, and HTTP responses.
38
38
39
-
**Sample Code:**
40
39
```python
41
40
import asyncio
42
41
import unittest
@@ -47,36 +46,35 @@ from function_app import start_orchestrator
Durable Orchestrators manage the execution of multiple activity functions. To test an orchestrator:
69
+
## Unit testing orchestrator functions
70
+
71
+
Orchestrator functions manage the execution of multiple activity functions. To test an orchestrator:
73
72
74
73
- Mock the `DurableOrchestrationContext` to control function execution.
75
74
- Replace `DurableOrchestrationContext` methods needed for orchestrator execution like `call_activity` or `create_timer` with mock functions. These functions should have pre-determined behavior and should typically return Task objects with a `result` property.
76
75
- Call the orchestrator recursively, passing the result of the Task generated by the previous yield statement to the next.
77
76
- Use the results returned from the orchestrator, as well as `unittest.mock` methods to verify the orchestrator result.
78
77
79
-
**Sample Code:**
80
78
```python
81
79
import unittest
82
80
from unittest.mock import Mock, patch, call
@@ -86,10 +84,10 @@ class TestFunction(unittest.TestCase):
Durable Entity functions manage stateful objects with operations. To test an entity function:
106
+
## Unit testing entity functions
107
+
108
+
Entity functions manage stateful objects with operations. To test an entity function:
111
109
112
110
- Mock the `DurableEntityContext` to simulate the entity's internal state and operation inputs.
113
111
- Replace `DurableEntityContext` methods like `get_state`, `set_state`, and `operation_name` with mocks that return controlled values.
114
112
- Invoke the entity function directly with the mocked context.
115
113
- Use assertions to verify state changes and returned values, along with `unittest.mock` utilities.
116
114
117
-
**Sample Code:**
118
115
```python
119
116
import unittest
120
117
from unittest.mock import Mock, patch
@@ -156,27 +153,12 @@ class TestEntityFunction(unittest.TestCase):
156
153
self.assertEqual(result, None)
157
154
```
158
155
159
-
## Testing Activity Functions
160
-
Activity functions require no Durable-specific modifications to be tested. The guidance found in the [Azure Functions Python unit testing overview](../functions-reference-python.md#unit-testing) is sufficient for testing these functions.
161
-
162
-
## Summary
163
-
Testing Durable Functions effectively requires simulating their unique runtime behaviors while keeping tests isolated and deterministic. Here are some best practices to keep in mind:
164
-
165
-
-**Mock the right context objects**: Use `DurableOrchestrationContext`, `DurableOrchestrationClient`, and `DurableEntityContext` mocks to simulate platform behavior.
166
-
-**Replace key methods**: Mock critical methods like `call_activity`, `start_new`, `get_state`, etc., to return controlled values for validation.
167
-
-**Isolate logic**: Keep orchestration, client, and entity logic testable independently to ensure clear separation of concerns.
168
-
-**Validate behavior, not implementation**: Focus on validating the outcome and side effects, rather than internal steps.
169
-
-**Use generator wrappers**: For orchestrators, properly walk through the generator steps to simulate orchestration flow.
170
-
-**Test failure paths**: Include tests for error conditions, timeouts, and unexpected input to ensure robust function behavior.
171
-
172
-
By applying these practices, you can build a comprehensive test suite for your Durable Functions that ensures reliability and simplifies long-term maintenance.
156
+
## Unit testing activity functions
173
157
174
-
---
158
+
Activity functions require no Durable-specific modifications to be tested. The guidance found in the [Azure Functions Python unit testing overview](../functions-reference-python.md#unit-testing) is sufficient for testing these functions.
175
159
176
160
## Related content
177
161
178
-
For deeper insights into Durable Functions in Python, explore these resources:
179
-
180
-
-[Improve throughput performance of Python apps in Azure Functions](../python-scale-performance-reference.md)
0 commit comments