Skip to content

Commit 545fbcc

Browse files
committed
Update tests from PR feedback
1 parent 7e10d5e commit 545fbcc

File tree

10 files changed

+223
-3
lines changed

10 files changed

+223
-3
lines changed

samples-v2/blueprint/tests/readme.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Durable Functions Sample – Unit Tests (Python)
2+
3+
This directory contains a simple **unit test** for the sample [Durable Azure Functions](https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-overview) written in Python. This test demonstrates how to validate the logic of the orchestrator function in isolation using mocks.
4+
5+
## Prerequisites
6+
7+
- Python
8+
- [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local) (for running functions locally)
9+
- [pytest](https://docs.pytest.org) for test execution
10+
- VS Code with the **Python** and **Azure Functions** extensions (optional but recommended)
11+
12+
---
13+
14+
## Running Tests from the Command Line
15+
16+
1. Open a terminal or command prompt.
17+
2. Navigate to the project root (where your `requirements.txt` is).
18+
3. Create and activate a virtual environment:
19+
20+
```bash
21+
python -m venv .venv
22+
.venv\Scripts\activate # On Windows
23+
source .venv/bin/activate # On macOS/Linux
24+
```
25+
Install dependencies:
26+
27+
```bash
28+
pip install -r requirements.txt
29+
```
30+
31+
Run tests:
32+
33+
```bash
34+
pytest
35+
```
36+
37+
## Running Tests in Visual Studio Code
38+
1. Open the project folder in VS Code.
39+
2. Make sure the Python extension is installed.
40+
3. Open the Command Palette (Ctrl+Shift+P), then select:
41+
```
42+
Python: Configure Tests
43+
```
44+
4. Choose pytest as the test framework.
45+
5. Point to the tests/ folder when prompted.
46+
6. Once configured, run tests from the Test Explorer panel or inline with the test code.
47+
48+
Notes
49+
- Tests use mocks to simulate Durable Functions' context objects.
50+
- These are unit tests only; no real Azure services are called.
51+
- For integration tests, consider starting the host with func start.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Activity functions require no special implementation aside from standard Azure Functions
2+
# unit testing for Python. As such, no test is implemented here.
3+
# For more information about testing Azure Functions in Python, see the official documentation:
4+
# https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#unit-testing
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import asyncio
2+
import unittest
3+
import azure.functions as func
4+
from unittest.mock import AsyncMock, Mock, patch
5+
6+
from durable_blueprints import start_orchestrator
7+
8+
class TestFunction(unittest.TestCase):
9+
@patch('azure.durable_functions.DurableOrchestrationClient')
10+
def test_HttpStart(self, client):
11+
# Get the original method definition as seen in the function_app.py file
12+
func_call = start_orchestrator.build().get_user_function().client_function
13+
14+
req = func.HttpRequest(method='GET',
15+
body=b'{}',
16+
url='/api/my_second_function',
17+
route_params={"functionName": "E2_BackupSiteContent"})
18+
19+
client.start_new = AsyncMock(return_value="instance_id")
20+
client.create_check_status_response = Mock(return_value="check_status_response")
21+
22+
# Create a generator using the method and mocked context
23+
result = asyncio.run(func_call(req, client))
24+
25+
client.start_new.assert_called_once_with("E2_BackupSiteContent", client_input={})
26+
client.create_check_status_response.assert_called_once_with(req, "instance_id")
27+
self.assertEqual(result, "check_status_response")
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Durable Functions Sample – Unit Tests (Python)
2+
3+
This directory contains a simple **unit test** for the sample [Durable Azure Functions](https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-overview) written in Python. This test demonstrates how to validate the logic of the orchestrator function in isolation using mocks.
4+
5+
## Prerequisites
6+
7+
- Python
8+
- [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local) (for running functions locally)
9+
- [pytest](https://docs.pytest.org) for test execution
10+
- VS Code with the **Python** and **Azure Functions** extensions (optional but recommended)
11+
12+
---
13+
14+
## Running Tests from the Command Line
15+
16+
1. Open a terminal or command prompt.
17+
2. Navigate to the project root (where your `requirements.txt` is).
18+
3. Create and activate a virtual environment:
19+
20+
```bash
21+
python -m venv .venv
22+
.venv\Scripts\activate # On Windows
23+
source .venv/bin/activate # On macOS/Linux
24+
```
25+
Install dependencies:
26+
27+
```bash
28+
pip install -r requirements.txt
29+
```
30+
31+
Run tests:
32+
33+
```bash
34+
pytest
35+
```
36+
37+
## Running Tests in Visual Studio Code
38+
1. Open the project folder in VS Code.
39+
2. Make sure the Python extension is installed.
40+
3. Open the Command Palette (Ctrl+Shift+P), then select:
41+
```
42+
Python: Configure Tests
43+
```
44+
4. Choose pytest as the test framework.
45+
5. Point to the tests/ folder when prompted.
46+
6. Once configured, run tests from the Test Explorer panel or inline with the test code.
47+
48+
Notes
49+
- Tests use mocks to simulate Durable Functions' context objects.
50+
- These are unit tests only; no real Azure services are called.
51+
- For integration tests, consider starting the host with func start.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
# Stub file - test this function with standard Azure Functions Python testing tools.
1+
# Activity functions require no special implementation aside from standard Azure Functions
2+
# unit testing for Python. As such, no test is implemented here.
3+
# For more information about testing Azure Functions in Python, see the official documentation:
4+
# https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#unit-testing
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
# Stub file - test this function with standard Azure Functions Python testing tools.
1+
# Activity functions require no special implementation aside from standard Azure Functions
2+
# unit testing for Python. As such, no test is implemented here.
3+
# For more information about testing Azure Functions in Python, see the official documentation:
4+
# https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#unit-testing

samples-v2/fan_in_fan_out/tests/test_HttpStart.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class TestFunction(unittest.TestCase):
99
@patch('azure.durable_functions.DurableOrchestrationClient')
1010
def test_HttpStart(self, client):
1111
# Get the original method definition as seen in the function_app.py file
12-
# func_call = chaining_orchestrator.build().get_user_function_unmodified()
1312
func_call = HttpStart.build().get_user_function().client_function
1413

1514
req = func.HttpRequest(method='GET',
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Durable Functions Sample – Unit Tests (Python)
2+
3+
This directory contains a simple **unit test** for the sample [Durable Azure Functions](https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-overview) written in Python. This test demonstrates how to validate the logic of the orchestrator function in isolation using mocks.
4+
5+
## Prerequisites
6+
7+
- Python
8+
- [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local) (for running functions locally)
9+
- [pytest](https://docs.pytest.org) for test execution
10+
- VS Code with the **Python** and **Azure Functions** extensions (optional but recommended)
11+
12+
---
13+
14+
## Running Tests from the Command Line
15+
16+
1. Open a terminal or command prompt.
17+
2. Navigate to the project root (where your `requirements.txt` is).
18+
3. Create and activate a virtual environment:
19+
20+
```bash
21+
python -m venv .venv
22+
.venv\Scripts\activate # On Windows
23+
source .venv/bin/activate # On macOS/Linux
24+
```
25+
Install dependencies:
26+
27+
```bash
28+
pip install -r requirements.txt
29+
```
30+
31+
Run tests:
32+
33+
```bash
34+
pytest
35+
```
36+
37+
## Running Tests in Visual Studio Code
38+
1. Open the project folder in VS Code.
39+
2. Make sure the Python extension is installed.
40+
3. Open the Command Palette (Ctrl+Shift+P), then select:
41+
```
42+
Python: Configure Tests
43+
```
44+
4. Choose pytest as the test framework.
45+
5. Point to the tests/ folder when prompted.
46+
6. Once configured, run tests from the Test Explorer panel or inline with the test code.
47+
48+
Notes
49+
- Tests use mocks to simulate Durable Functions' context objects.
50+
- These are unit tests only; no real Azure services are called.
51+
- For integration tests, consider starting the host with func start.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import asyncio
2+
import unittest
3+
import azure.functions as func
4+
from unittest.mock import AsyncMock, Mock, patch
5+
6+
from function_app import http_start
7+
8+
class TestFunction(unittest.TestCase):
9+
@patch('azure.durable_functions.DurableOrchestrationClient')
10+
def test_HttpStart(self, client):
11+
# Get the original method definition as seen in the function_app.py file
12+
func_call = http_start.build().get_user_function().client_function
13+
14+
req = func.HttpRequest(method='GET',
15+
body=b'{}',
16+
url='/api/my_second_function',
17+
route_params={"functionName": "E2_BackupSiteContent"})
18+
19+
client.start_new = AsyncMock(return_value="instance_id")
20+
client.create_check_status_response = Mock(return_value="check_status_response")
21+
22+
# Create a generator using the method and mocked context
23+
result = asyncio.run(func_call(req, client))
24+
25+
client.start_new.assert_called_once_with("E2_BackupSiteContent", client_input={})
26+
client.create_check_status_response.assert_called_once_with(req, "instance_id")
27+
self.assertEqual(result, "check_status_response")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Activity functions require no special implementation aside from standard Azure Functions
2+
# unit testing for Python. As such, no test is implemented here.
3+
# For more information about testing Azure Functions in Python, see the official documentation:
4+
# https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#unit-testing

0 commit comments

Comments
 (0)